Single Post

Header

Monday, February 8, 2016

Mouse Over and Click on Sub Menu link using Selenium WebDriver

Mouse Over and Click on Sub Menu link using Selenium WebDriver

import java.util.concurrent.TimeUnit;

import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;

public class MouseOverAndClickSubMenu {


@Test
public void mouseOverAndClickSubMenu() throws InterruptedException {
WebDriver driver = new FirefoxDriver();
driver.get("http://flex.apache.org/");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
WebElement menu = driver.findElement(By.xpath(".//*[@id='nav']/li[2]/a"));
WebElement subMenu = driver.findElement(By.xpath(".//*[@id='nav']/li[2]/ul/li[5]/a"));
Actions act = new Actions(driver);
act.moveToElement(menu).perform();     // Mouse over an element
Thread.sleep(2000);
act.click(subMenu).perform();   // Click on a sub menu link
Thread.sleep(3000);
driver.quit();
}


}

Monday, January 25, 2016

How to select first value from the suggested auto complete list using Selenium WebDriver

Select first value from the suggested auto complete list using Selenium WebDriver

import java.util.List;

import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class SelectFirstAutoCompleteValue {

@Test
public void selectFirstAutoCompleteValue() throws InterruptedException {
WebDriver driver = new FirefoxDriver();
driver.get("http://www.aa.com/homePage.do?locale=en_US");
driver.manage().window().maximize();
driver.findElement(By.id("reservationFlightSearchForm.originAirport")).clear();
Thread.sleep(2000);
driver.findElement(By.id("reservationFlightSearchForm.originAirport")).sendKeys("kan");
Thread.sleep(2000);
WebElement we1 = driver.findElement(By.cssSelector("#ui-id-3.ui-autocomplete"));
List<WebElement> list1 = we1.findElements(By.cssSelector(".ui-corner-all"));
Thread.sleep(2000);
list1.get(0).click();
Thread.sleep(2000);
driver.findElement(By.id("reservationFlightSearchForm.destinationAirport")).sendKeys("san");
Thread.sleep(2000);
WebElement we2 = driver.findElement(By.cssSelector("#ui-id-4.ui-autocomplete"));
List<WebElement> list2 = we2.findElements(By.cssSelector(".ui-corner-all"));
Thread.sleep(2000);
list2.get(0).click();
Thread.sleep(2000);
driver.close();
}

}

Tuesday, January 12, 2016

Difference Between Implicit and Explicit waits in Selenium WebDriver

Difference Between Implicit and Explicit waits in Selenium WebDriver

Implicit Wait 

Implicit Wait is basically the internal timeout for the WebDriver for WebElement we are searching to be available. In this case, we are telling WebDriver that it should wait for certain timeout in case of specified element not available on the UI (DOM). Implicit wait is set to all the WebElement we are using in our script. It will keep on searching for the element again and again for certain time period and if it is not found at last then it throws ‘NoSuchElementException’. The default implicit timeout is set to zero.

Explicit Wait 

In Explicit Wait, we write certain type of code which is used like wait and used as per our convenience. It reaches its timeout period until certain condition is satisfied. It is a custom kind of code for particular WebElement for waiting for particular timeout. Explicit Wait is intelligent kind of wait which provide us better option that Implicit Wait. But Explicit Wait can be applied for particular specified elements.

There are many classes by WebDriver like ‘WebDriverWait’, ‘ExpectedCondition’ which are used for implementing Explicit wait.

These days for implementing this we frequently use ‘Thread.Sleeep()’ in our code, which wait exactly for the same time period we provide in brackets. But this is not the good idea to implement Explicit wait.

package SeleniumTestNG;

import static org.junit.Assert.*;

import org.junit.Assert;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.util.concurrent.TimeUnit;

public class ImplicitAndExplicitWaits {

  @Test
  public void implicitWait() {
    WebDriver driver = new FirefoxDriver();
    driver.manage().timeouts().implicitlyWait(8, TimeUnit.SECONDS);  // wait upto 8 seconds for finding any type of element on the web page 
    driver.get("https://www.redbus.in/");
    driver.manage().window().maximize(); // this type of code might not work for implicit wait
    driver.findElement(By.id("txtSource")).sendKeys("Bangalore");
    driver.findElement(By.id("txtDestination")).sendKeys("Guntur");
    driver.findElement(By.id("txtOnwardCalendar")).click();
    int n = driver.findElements(By.cssSelector("#rbcal_txtOnwardCalendar .monthTable")).size();
    System.out.println("the months are "+n);
    WebElement nextMonth = driver.findElements(By.cssSelector("#rbcal_txtOnwardCalendar .monthTable")).get(1);
    nextMonth.findElements(By.cssSelector(".wd.day")).get(0).click();
    driver.findElement(By.id("searchBtn")).click();
    System.out.println("the number of buses are"+driver.findElements(By.cssSelector(".viewSeatsBtn")).size());  // Mainly the implicit wait applies here to identify the particular button on the next web page.
    assertEquals(true,driver.findElements(By.cssSelector(".viewSeatsBtn")).size()!=0);
    driver.quit();
  }

  @Test
  public void withoutImplicitOrExplicitWait() {
    WebDriver driver = new FirefoxDriver();
  //  driver.manage().timeouts().implicitlyWait(8, TimeUnit.SECONDS);
    driver.get("https://www.redbus.in/");
  // driver.manage().window().maximize(); // this type of code might not work for implicit wait
    driver.findElement(By.id("txtSource")).sendKeys("Bangalore");
    driver.findElement(By.id("txtDestination")).sendKeys("Guntur");
    driver.findElement(By.id("txtOnwardCalendar")).click();
    int n = driver.findElements(By.cssSelector("#rbcal_txtOnwardCalendar .monthTable")).size();
    System.out.println("the months are "+n);
    WebElement nextMonth = driver.findElements(By.cssSelector("#rbcal_txtOnwardCalendar .monthTable")).get(1);
    nextMonth.findElements(By.cssSelector(".wd.day")).get(0).click();
    driver.findElement(By.id("searchBtn")).click();
    assertEquals(true,driver.findElements(By.cssSelector(".viewSeatsBtn")).size()!=0); 
    // it would fail in the above line since the view seats buttons are not visible immediately since they are in the next web page
    driver.quit();
  }

  @Test
  public void explicitWait() {
    WebDriver driver = new FirefoxDriver();
    driver.get("https://www.redbus.in/");
  // driver.manage().window().maximize(); // this type of code might not work for implicit wait
    driver.findElement(By.id("txtSource")).sendKeys("Bangalore");
    driver.findElement(By.id("txtDestination")).sendKeys("Guntur");
    driver.findElement(By.id("txtOnwardCalendar")).click();
    int n = driver.findElements(By.cssSelector("#rbcal_txtOnwardCalendar .monthTable")).size();
    System.out.println("the months are "+n);
    WebElement nextMonth = driver.findElements(By.cssSelector("#rbcal_txtOnwardCalendar .monthTable")).get(1);
    nextMonth.findElements(By.cssSelector(".wd.day")).get(0).click();
    driver.findElement(By.id("searchBtn")).click();
    WebDriverWait wait = new WebDriverWait(driver, 10);
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".gtm_availableseatsort"))); // Look for the particular element using this condition
    System.out.println("the number of buses are "+driver.findElements(By.cssSelector(".viewSeatsBtn")).size());
    assertEquals(true,driver.findElements(By.cssSelector(".viewSeatsBtn")).size()!=0);
    driver.quit();
  }


}