Monday, February 11, 2013

WebDriver : use of Iterator to retrieve all the elements present in same class

Webdriver  program will help to use of Iterator to retrieve all the elements present in same class 
calling the main function again .


package hQF;
import static org.testng.AssertJUnit.assertTrue;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.Keys;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import com.opera.core.systems.scope.protos.EcmascriptProtos.Object;

public class HAH1
{
public static void   main(String[] args)throws Exception
{
final String var = "prog_bg";
// final var means variable var's value never been change 
WebDriver driver = new FirefoxDriver();
driver.get("http://hweh.com");
Thread.sleep(1000);
driver.findElement(By.xpath("//input[@value='REFI']")).click();
// Select the values from dropdown field 
Select Droplist = new Select ( driver.findElement(By.name("propertyStateCode")));
Droplist.selectByVisibleText("California");
driver.findElement(By.className("MFC_submit")).click() ;
Thread.sleep(2000);
// Implicitly tell the browser to wait for 10 second. so that object is rendered. please use this command then Thread.sleep
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
// If  id='prog_bg' is present in source code then execute teh following code  
if(driver.findElements(By.xpath("//div[@id='prog_bg']")).size()!=0)
{
try{
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
driver.findElement(By.xpath("//div[@id='propertyvalue-div']/div[2]/button")).click();
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
}
catch(Exception e){};

try{
driver.findElement(By.tagName("body")).getText().contains("Total Mortgage Balance");
Thread.sleep(100);
driver.findElement(By.xpath("//div[@id='mortgagebalance-div']/div[2]/button")).click();
System.out.println("Total Mortgage Balance is present");
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
}
catch(Exception e){
System.out.println("Total Mortgage Balance is NOT present");
}
try {
driver.findElement(By.tagName("body")).getText().contains("Approximately, how much cash");
driver.findElement(By.xpath("//div[@id='cashout-div']/div[2]/button")).click();
System.out.println("Approximately, how much cash would you like to receive at closing? is  present");
}
catch(Exception e){
System.out.println("Approximately, how much cash would you like to receive at closing? is NOT present");
}
try {
driver.findElement(By.tagName("body")).getText().contains("Bankruptcy Status");
driver.findElement(By.xpath("//div[@id='bankruptcyflag-div']/div[2]/button")).click();
System.out.println("Bankruptcy Status: is presend");
}
catch (Exception e){
System.out.println("Bankruptcy Status: is NOT present");
}
try{
driver.findElement(By.tagName("body")).getText().contains("Are you or your spouse on active duty");
driver.findElement(By.xpath("//div[@id='militaryeligibility-div']/div[2]/button")).click();
System.out.println("Are you or your spouse on active duty or a veteran of the U.S. military?" );
}
catch (Exception e){
System.out.println("window Are you or your spouse on active duty or a veteran of the U.S. military? NOT present" );
}
try{
// write the text "test@qs.com" in the field whose id = "email"
driver.findElement(By.id("email")).sendKeys("test@qs.com");
driver.findElement(By.xpath("//div[@id='email-div']/div[2]/button")).click();
}
catch (Exception e){}
driver.findElement(By.xpath("//input[@id='first_name']")).sendKeys("test");
driver.findElement(By.xpath("//input[@id='last_name']")).sendKeys("test");
driver.findElement(By.xpath("//input[@id='address']")).sendKeys("test");
driver.findElement(By.xpath("//input[@id='city']")).sendKeys("test");
driver.findElement((By.xpath("//input[@id='zip_code']"))).sendKeys("94154");
driver.findElement(By.xpath("//input[@id='phone_number_1']")).sendKeys("941");
driver.findElement(By.xpath("//input[@id='phone_number_2']")).sendKeys("321");
driver.findElement(By.xpath("//input[@id='phone_number_3']")).sendKeys("3212");
driver.findElement(By.xpath("//input[@id='get_lenders']")).click();
Thread.sleep(300);
driver.findElement(By.tagName("body")).getText().contains("Get Better, more Custom Results.Please answer ");
System.out.println("Window Get Better, more Custom Results.Please answer these last 4 questions' is rendered ");
driver.findElement(By.id("submitButton")).click();
Thread.sleep(300);
// Iterator function collect all teh element having class = "company-name" in ClientsName list (array) 
List<WebElement> ClientsName = driver.findElements(By.className("company-name"));
// Create a iterator function 
Iterator it = ClientsName.iterator();
//Displayed the total number of client present 
System.out.println("Total count of matched Client found  " +ClientsName.size());
// Executed the loop till a number of client present 
while (it.hasNext())
{
//Displayed all the clients name one by one
System.out.println("Name of clients are  :" +it.next());
}
driver.findElement(By.tagName("body")).getText().contains("Get your customized mortgage");
System.out.println("Window 'Get your customized mortgage' is rendered and client listing is displayed");
}
else
{
System.out.println("check console is going to one question at a time ");
driver.close();
// call the main function again
main(args);
}

}

}