Tuesday 4 August 2015

How to work SELECT Methods with Dropdowns in Selenium Webdriver using java.

Select class provides useful methods for interacting with select options. User can do operations on a select dropdown and also de-select operation using the below methods. User can also get the text of the values in the dropdown.
HTML Code:
<html>
<head>
<title>Selenium WebDriver- Drop Down Interaction</title>
</head>
<body>
<select id="language">
<option value="java">Java</option>
<option value="c">C</option>
<option value="perl">Perl</option>
<option value="php">PHP</option>
<option value="python">Python</option>
<option value="javascript">JavaScript</option>
</select>
</body>
</html>

Method Name: selectByVisibleText
Syntax: selectByVisibleText(String text)
Description: Select all options that display text matching the argument.. It will not look for any index or value, it will try to match the VisibleText (which will display in dropdown)
Example:
 Select dropdown = new Select(driver.findElement(By.id("language")));
  dropdown.selectByVisibleText("Python");
 
Method Name: selectByValue
Syntax: selectByValue(String value)
Description: Select all options that have a value matching the argument.
Example:
 Select dropdown = new Select(driver.findElement(By.id("language")));
 dropdown.selectByValue("php");

Method Name: selectByIndex
Syntax: selectByIndex(int index)
Description: Select the option at the given index.
Example:
 Select dropdown = new Select(driver.findElement(By.id("language")));
 dropdown.selectByIndex(3);

Method Name: deselectByVisibleText 
Syntax: deselectByVisibleText(String text)
Description: Deselect all options that display text matching the argument.
Example:
 Select dropdown = new Select(driver.findElement(By.id("language")));
  dropdown.deselectByVisibleText("Python");

Method Name:  deselectByValue 
Syntax: deselectByValue(String text)
Description: Deselect all options that have a value matching the argument.
Example:
 Select dropdown = new Select(driver.findElement(By.id("language")));
 dropdown.deselectByValue("php");

Method Name: deselectByIndex 
Syntax: deselectByIndex(int index)
Description: Deselect the option at the given index.
Example:
 Select dropdown = new Select(driver.findElement(By.id("language")));
 dropdown.deselectByIndex(3);

Method Name: deselectAll
Syntax: deselectAll()
Description: To Clear all selected entries.
Example:
 Select dropdown = new Select(driver.findElement(By.id("language")));
 dropdown.deselectAll();

Method Name: getOptions
Syntax: List<WebElement> getOptions()
Description: Get all options belonging to this select tag
Example:
Select dropdown = new Select(driver.findElement(By.id("language")));
List<WebElement> allOptions = dropdown.getOptions();
for (WebElement webElement : allOptions){
System.out.println(webElement.getText());}

Method Name: getAllSelectedOptions
Syntax: List<WebElement> getAllSelectedOptions()
Description: Return all selected options belonging to this select tag
Example:
Select dropdown = new Select(driver.findElement(By.id("language")));
List<WebElement> allOptions = dropdown.getAllSelectedOptions();
for (WebElement webElement : allOptions){
System.out.println(webElement.getText());}

Method Name: getFirstSelectedOption
Syntax: WebElement getFirstSelectedOption()
Description: Get the first selected option in this select tag (or the currently selected option in a normal select)
Example:
Select dropdown = new Select(driver.findElement(By.id("language")));
WebElement firstOption = dropdown.getFirstSelectedOption();System.out.println(firstOption.getText());

Method Name: isMultiple
Syntax: boolean isMultiple()
Description:  Whether this select element support selecting multiple options at the same time? This is done by checking the value of the "multiple" attribute
Example:
Select dropdown = new Select(driver.findElement(By.id("language")));
boolean multiple= dropdown.isMultiple();

No comments:

Post a Comment