Wednesday, 21 May 2014

Selenium WebDriver: Dropdown selection

Selenium has Select class designed to interact with drop down lists. Using it, we can easily choose option by displayed text, index or value.

Example:

Consider a dropdown list as shown below:


Now we just need to pass WebElement into Select Object as shown below

Select dropdown = new Select(driver.findElement(By.id("months")));

To select option say 'February', we can use any one of the method:
  • dropdown.selectByVisibleText("February");
  • dropdown.selectByValue("Feb");
  • dropdown.selectByIndex(2);

I have created a generic class Dropdown with all 3 methods shown above, we just need to call it.

  • Dropdown.selectByVisibleText(driver, By.id("months"), "February");
  • Dropdown.selectByValue(driver, By.id("months"), "Feb");
  • Dropdown.selectByIndex(driver, By.id("months"), 2);




Thursday, 8 May 2014

Selenium WebDriver – Handle Window Focus

Method and Description:

 

Example:

  • Below example opens a page of w3schools.com
  • And then clicks on ‘Try it yourself »’ link, which opens a new window.
  • Switch to new window, perform some actions on it and then switch back to previous window

Selenium WebDriver – Open a link in a new Window


We can use Action class to open a link in a new window.

Method and Description:


  • contextClick(WebElement onElement) - Performs a context-click at middle of the given element.
  • perform()- A convenience method for performing the actions without calling build() first.
  • sendKeys(java.lang.CharSequence... keysToSend) - Sends keys to the active element.

 

Example:

Below example opens w3schools.com
And click on “Learn HTML” link to open in new window.