Selecting Dropdown/Multiple Select Box
It is just an ordinary operation like selecting any other type of element on a webpage. You can choose it by ID, Name, Css & Xpath etc. But to perform any action on this element it is required to import ‘import org.openqa.selenium.support.ui.Select' package and to use it we need to create a new select object of class select.
Select oSelection = new Select(driver.findElement(By.id(id)));
Note: Select class only works for elements with <select> tags
Selecting an option using ‘selectByVisibleText’
It is very easy to choose or select an option given under any dropdowns and multiple selection boxes with selectByVisibleText method.
Select oSelection = new Select(driver.findElement(By.id(id)));
oSelection.selectByVisibleText(text);
Selecting an option using ‘selectByIndex’
It is almost the same as selectByVisibleText but the only difference here is that we provide the index number of the option here rather the option text.
Select oSelection = new Select(driver.findElement(By.id(id)));
oSelection.selectByIndex(index);
Selecting an option using ‘selectByValue’
It is again the same what we have discussed earlier, the only difference in this is that we need to provide the value of the option rather the option text.
Select oSelection = new Select(driver.findElement(By.id(id)));
oSelection.selectByValue(value);
Note: The value of an option and the text of the option may not be always same and there can be a possibility that the value is not assigned to Select webelement. If the value is given in the Select tag then only you can use the selectByValue method.
Getting the Size of Select item
Sometimes you may like to count the element in the dropdown and multiple select box, so that you can use the loop on Select element.
Select oSelection = new Select(driver.findElement(By.id(id)));
List<WebElement> oSize = oSelection.getOptions();
int iListSize = oSize.size();
Printing the Options Once you get the size of the Select element then it is easy to print the Text of the options.
Select oSelection = new Select(driver.findElement(By.id(id)));
List oSize = oSelection.getOptions();
int iListSize = oSize.size();
for(int i =0; i>iListSize ; i++){
String sValue = oSelection.getOptions().get(i).getText();
System.out.println(sValue);
}
All of the above methods work on both Dropdown and Multiple select box.
Deselect methods
This only works on Multiple selection boxes. If in case you want to deselect any selected option and that can be done with either deselectAll(), deselectByIndex, deselectByValue and deselectByVisibletext.
Select oSelection = new Select(driver.findElement(By.id(id)));
oSelection.deselectAll();
oSelection.deselectByIndex(index);
oSelection.deselectByValue(value);
oSelection.deselectByVisibleText(text);
Multiple selection method
This one also just works on Multiple selection boxes and not on regular List boxes or dropdowns. There is no additional logic behind selecting multiple options of Select element. All you need to do is to fire select commands on multiple elements one by one that’s it.
Select oSelection = new Select(driver.findElement(By.id(id)));
oSelection.selectByIndex(index)
oSelection.selectByIndex(index)
// Or
oSelection.selectByVisibleText(text)
oSelection.selectByVisibleText(text)
// Or
oSelection.selectByValue(value)
oSelection.selectByValue(value)
Practice Exercise -1 (Drop Down Box/List)
1) Launch new Browser
2) Open “http://www.toolsqa.com/automation-practice-form/”
3) Select ‘Continents’ Drop down ( Use Id to identify the element )
4) Select option ‘Europe’ (Use selectByIndex)
5) Select option ‘Africa’ now (Use selectByVisibleText)
6) Print all the options for the selected drop down and select one option of your choice
7) Close the browser
Solution
package practiceTestCases;
import java.util.List;
import java.util.concurrent.TimeUnit;
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.Select;
public class PracticeDropDown {
private static WebDriver driver = null;
public static void main(String[] args) throws InterruptedException {
// Create a new instance of the Firefox driver
driver = new FirefoxDriver();
// Put an Implicit wait, this means that any search for elements on the page could take the time the implicit wait is set for before throwing exception
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// Launch the URL
driver.get("http://www.toolsqa.com/automation-practice-form");
// Step 3: Select 'Continents' Drop down ( Use Id to identify the element )
// Find Select element of "Single selection" using ID locator.
Select oSelection = new Select(driver.findElement(By.id("continents")));
// Step 4:) Select option 'Europe' (Use selectByIndex)
oSelection.selectByVisibleText("Europe");
// Using sleep command so that changes can be noticed
Thread.sleep(2000);
// Step 5: Select option 'Africa' now (Use selectByVisibleText)
oSelection.selectByIndex(2);
Thread.sleep(2000);
// Step 6: Print all the options for the selected drop down and select one option of your choice
// Get the size of the Select element
List oSize = oSelection.getOptions();
int iListSize = oSize.size();
// Setting up the loop to print all the options
for(int i =0; i < iListSize ; i++){
// Storing the value of the option
String sValue = oSelection.getOptions().get(i).getText();
// Printing the stored value
System.out.println(sValue);
// Putting a check on each option that if any of the option is equal to 'Africa" then select it
if(sValue.equals("Africa")){
oSelection.selectByIndex(i);
break;
}
}
// Kill the browser
driver.close();
}
}
It is just an ordinary operation like selecting any other type of element on a webpage. You can choose it by ID, Name, Css & Xpath etc. But to perform any action on this element it is required to import ‘import org.openqa.selenium.support.ui.Select' package and to use it we need to create a new select object of class select.
Select oSelection = new Select(driver.findElement(By.id(id)));
Note: Select class only works for elements with <select> tags
Selecting an option using ‘selectByVisibleText’
It is very easy to choose or select an option given under any dropdowns and multiple selection boxes with selectByVisibleText method.
Select oSelection = new Select(driver.findElement(By.id(id)));
oSelection.selectByVisibleText(text);
Selecting an option using ‘selectByIndex’
It is almost the same as selectByVisibleText but the only difference here is that we provide the index number of the option here rather the option text.
Select oSelection = new Select(driver.findElement(By.id(id)));
oSelection.selectByIndex(index);
Selecting an option using ‘selectByValue’
It is again the same what we have discussed earlier, the only difference in this is that we need to provide the value of the option rather the option text.
Select oSelection = new Select(driver.findElement(By.id(id)));
oSelection.selectByValue(value);
Note: The value of an option and the text of the option may not be always same and there can be a possibility that the value is not assigned to Select webelement. If the value is given in the Select tag then only you can use the selectByValue method.
Getting the Size of Select item
Sometimes you may like to count the element in the dropdown and multiple select box, so that you can use the loop on Select element.
Select oSelection = new Select(driver.findElement(By.id(id)));
List<WebElement> oSize = oSelection.getOptions();
int iListSize = oSize.size();
Printing the Options Once you get the size of the Select element then it is easy to print the Text of the options.
Select oSelection = new Select(driver.findElement(By.id(id)));
List oSize = oSelection.getOptions();
int iListSize = oSize.size();
for(int i =0; i>iListSize ; i++){
String sValue = oSelection.getOptions().get(i).getText();
System.out.println(sValue);
}
All of the above methods work on both Dropdown and Multiple select box.
Deselect methods
This only works on Multiple selection boxes. If in case you want to deselect any selected option and that can be done with either deselectAll(), deselectByIndex, deselectByValue and deselectByVisibletext.
Select oSelection = new Select(driver.findElement(By.id(id)));
oSelection.deselectAll();
oSelection.deselectByIndex(index);
oSelection.deselectByValue(value);
oSelection.deselectByVisibleText(text);
Multiple selection method
This one also just works on Multiple selection boxes and not on regular List boxes or dropdowns. There is no additional logic behind selecting multiple options of Select element. All you need to do is to fire select commands on multiple elements one by one that’s it.
Select oSelection = new Select(driver.findElement(By.id(id)));
oSelection.selectByIndex(index)
oSelection.selectByIndex(index)
// Or
oSelection.selectByVisibleText(text)
oSelection.selectByVisibleText(text)
// Or
oSelection.selectByValue(value)
oSelection.selectByValue(value)
Practice Exercise -1 (Drop Down Box/List)
1) Launch new Browser
2) Open “http://www.toolsqa.com/automation-practice-form/”
3) Select ‘Continents’ Drop down ( Use Id to identify the element )
4) Select option ‘Europe’ (Use selectByIndex)
5) Select option ‘Africa’ now (Use selectByVisibleText)
6) Print all the options for the selected drop down and select one option of your choice
7) Close the browser
Solution
package practiceTestCases;
import java.util.List;
import java.util.concurrent.TimeUnit;
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.Select;
public class PracticeDropDown {
private static WebDriver driver = null;
public static void main(String[] args) throws InterruptedException {
// Create a new instance of the Firefox driver
driver = new FirefoxDriver();
// Put an Implicit wait, this means that any search for elements on the page could take the time the implicit wait is set for before throwing exception
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// Launch the URL
driver.get("http://www.toolsqa.com/automation-practice-form");
// Step 3: Select 'Continents' Drop down ( Use Id to identify the element )
// Find Select element of "Single selection" using ID locator.
Select oSelection = new Select(driver.findElement(By.id("continents")));
// Step 4:) Select option 'Europe' (Use selectByIndex)
oSelection.selectByVisibleText("Europe");
// Using sleep command so that changes can be noticed
Thread.sleep(2000);
// Step 5: Select option 'Africa' now (Use selectByVisibleText)
oSelection.selectByIndex(2);
Thread.sleep(2000);
// Step 6: Print all the options for the selected drop down and select one option of your choice
// Get the size of the Select element
List oSize = oSelection.getOptions();
int iListSize = oSize.size();
// Setting up the loop to print all the options
for(int i =0; i < iListSize ; i++){
// Storing the value of the option
String sValue = oSelection.getOptions().get(i).getText();
// Printing the stored value
System.out.println(sValue);
// Putting a check on each option that if any of the option is equal to 'Africa" then select it
if(sValue.equals("Africa")){
oSelection.selectByIndex(i);
break;
}
}
// Kill the browser
driver.close();
}
}
No comments:
Post a Comment