Selenium and Java

Tuesday, 14 April 2015

Selenium - Web table in Selenium Webdriver

Web table is a different type of web element and we can not directly get all data in one variable.
To access web table data we need to locate web table using findelement method and then identified all the tr tags using findelements method.
Code for the same as below.

WebDriver driver =  new InternetExplorerDriver();
WebElement table = driver.findElement(By.id("tableid));
List<WebElement> tablerows = table.findElements(By.tagName("tr")); // To get all Rows

int rowcount  =  tablerows.size();  \\To get count of total rows

for (WebElement tablerow : tablerows )
{
tablerowindex = 0;
     List <WebElement> tablecolumns = tablerow.findelements(By.tagName("td"));
     int columncount = tablecolumns.size();
     for (WebElement tablecolumn : tablecolumns)
     {




      }



Selenium - Multiple windows in Selenium Webdriver

Selenium can handle multiple windows using inbuilt method getWindowHandles().
Code for the same as below.

WebDriver driver =  new InternetExplorerDriver();
Set<String> allWindows =  driver,getWindowHandles();

for (String allWindow : allWindows ) {
driver.switchTo().window(allWindow);

if (driver.getTitle().equalsIgnoreCase("VIP Database")){
              driver.manage().window().maximize(); 
              driver.findElement(By.cssSelector("button")).click();
          }
}

Selenium - Implicit wait and Explicit wait in Selenium Webdriver

Difference between Implicit wait and Explicit Wait...


Implicit Wait Explicit Wait
Implicit wait used for all the element search Explicit wait is specific to the element and is more flexible compare to Implicit wait. 
Implicit wait is time based. where we can command selenium WebDriver to wait for specific time. e.g 30 sec or 1 min and if till given time element not found than throws NoSuchElementexception. Explicit wait is condition based with time. In this we can command selenium WebDriver to wait for any condition and if condition not satisfied till given time then throws exception  
By default Implicit wait is 0.No default value
We need to provide two parameter to define implicit wait, Time to wait and UnitExplicit wait is programmatic.
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
WebDriverWait wait = new WebDriverWait(driver, 10);

WebElement element = wait.until(ExpectedConditions. elementToBeClickable(By.id("txtuser")));

One drawback of implicit wait is it affects performance of the automation execution as it waits for all the element for given time. And we need to give time of the slowest element to load.alertIsPresent ()
elementSelectionStateToBe :
elementToBeClickable
elementToBeSelected
envisibilityOfElementLocated 
and many more...


Monday, 13 April 2015

Selenium - Alert, Confirmation message or error message in Selenium WebDriver

Handling of Alert, Confirmation Message or error message while execution is very important for smooth execution.

Alerts:

public class alertWindow {


public static void main(String[] args) {

WebDriver driver = new FirefoxDriver();
WebElement button , textarea = null;
String parentWindowHandle = null;
Wait<WebDriver> wait = null;
Alert alert =null;

driver.get(System.getProperty("user.dir")+ "\\Sample\\Prompt.html");
driver.manage().window().maximize();
//driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
wait = new WebDriverWait(driver, 10);
parentWindowHandle = driver.getWindowHandle();
button = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("button")));
button.click();

if (alert != null)
System.out.println("Alert Not found");
else
System.out.println("Alert found");

alert = driver.switchTo().alert();
System.out.println("Alert Text " + alert.getText());

alert.sendKeys("Mohit Jain");
alert.accept();

textarea = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("demo")));
System.out.println(textarea.getText());

button.click();
alert.dismiss();
textarea = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("demo")));
System.out.println(textarea.getText());

driver.quit();

}

}

Confirmation :  

public class confirmationMesg {

/**
* @param args
*/
public static void main(String[] args) {

WebDriver driver = new FirefoxDriver();
WebElement button ,firstname, textarea = null;
String parentWindowHandle = null;
Set<String> allWindows;
Wait<WebDriver> wait = null;
Alert alert =null;
String windowtitle =null;

driver.navigate().to("http://www.ranorex.com/web-testing-examples/vip/");
driver.manage().window().maximize();
firstname =driver.findElement(By.id("FirstName"));
//firstname.sendKeys("Mohit");
button = driver.findElement(By.id("Save"));
parentWindowHandle = driver.getWindowHandle();
button.click();

allWindows = driver.getWindowHandles();
/*
for (String allWindow : allWindows ) {

driver.switchTo().window(allWindow);
if (driver.getTitle().equalsIgnoreCase("VIP Database")){

driver.manage().window().maximize();
driver.findElement(By.cssSelector("button")).click();

}



}
driver.switchTo().window(parentWindowHandle);
firstname.sendKeys("Mohit");*/

for (String allWindow : allWindows ) {

driver.switchTo().window(allWindow);
driver.manage().window().maximize();
if (driver.getTitle().equalsIgnoreCase("VIP Database")){
//driver.close();
driver.quit();
}

}
}