Saturday, August 25, 2012

How to select and deselect element from a drop down list in selenium webdriver


We need to select element from a drop down list in our test automation. Selenium webdriver has given us a class called “Select”.
There are three ways you can select and deselect an element. You can select element by value, by index and by visible text.

By value:
For selecting a element,
WebElement element = driver.findElement(locator);
Select selectData=new Select(element);
selectData.deselectAll();
selectData.selectByValue(value);

Here
locator is the element locator. it may be tagname, xpath, id etc.

selectData is the Select object.

value is the value of the data. you can take help from firebug to detect this element.
For deselecting an element,
WebElement element = driver.findElement(locator);
Select selectData=new Select(element);
selectData.deselectByValue(value);

By Index:
For selecting an element,
WebElement element = driver.findElement(locator);
Select selectData=new Select(element);
selectData.deselectAll();
selectData.selectByIndex(index);

Here index index of the list.

For deselecting an element,
WebElement element = driver.findElement(locator);
Select selectData=new Select(element);
selectData.deselectByIndex(index);

By Visible Text:
Here we select an element by using the visible data in the list. For example, we have a dropdown box that contain two element “Male” and “Female” as a visible element in the list. We use this two text to select.
For selecting an element,
WebElement element = driver.findElement(locator);
Select selectData=new Select(element);
selectData.deselectAll();
selectData.selectByVisibleText(visibleText);

Here visible text is the text found at drop down list.

For deselecting an element,
WebElement element = driver.findElement(locator);
Select selectData=new Select(element);
selectData.deselectByVisibleText(visibleText);

N.B : All codes are used here in Java.

How to switch from one window to another window in Selenium Webdriver

 

Sometimes we need to switch from one window to another window in our automation. Selenium webdriver has given us a nice command.

driver.switchTo().window(destination window);



Here argument is the title of the window. I found it very successful.


For example, if title of the window is “Olympic”, then command will be in Java,

driver.switchTo().window("Olympic"); 

We can get the title of the window by using firebug. 

How to maximize Browser window in Selenium Webdriver

 

Sometimes we need to maximize our browser window at the time of automation. Selenium webdriver have given us a nice command to execute such instruction.

We have a webdriver object named “driver” and we need to make our browser window maximize. The command will be

driver.manage().window().maximize(); //Java code

Cypress: How to handle browser-based authentication pop up dialog in Cypress

Five years ago I have written a blog on how to handle browser-based authentication for selenium webdriver.   Now it is for cypress. Cypress...