The following sample test class shows how to select an option of a list in 3 different ways:
public class DropdownTests {
private static final String OPTION_2 = "Option 2";
private static final String URL =
"https://the-internet.herokuapp.com/dropdown";
private ChromeDriver driver;
private By dropdownId = By.id("dropdown");
@Before
public void setUp() {
driver = new ChromeDriver();
driver.manage().window().maximize();
}
@After
public void tearDown() {
driver.quit();
}
@Test
public void canSelectDropDownOptionByIndex() {
driver.get(URL);
Assert.assertTrue(driver.getCurrentUrl().equals(URL));
WebElement element = driver.findElement(dropdownId);
Select dropdown = new Select(element);
dropdown.selectByIndex(2);
WebElement selectedOption = dropdown.getFirstSelectedOption();
Assert.assertEquals(OPTION_2, selectedOption.getText());
}
@Test
public void canSelectDropDownOptionByVisibleText() {
driver.get(URL);
Assert.assertTrue(driver.getCurrentUrl().equals(URL));
WebElement element = driver.findElement(dropdownId);
Select dropdown = new Select(element);
dropdown.selectByVisibleText(OPTION_2);
WebElement selectedOption = dropdown.getFirstSelectedOption();
Assert.assertEquals(OPTION_2, selectedOption.getText());
}
@Test
public void canSelectDropDownOptionByValue() {
driver.get(URL);
Assert.assertTrue(driver.getCurrentUrl().equals(URL));
WebElement element = driver.findElement(dropdownId);
Select dropdown = new Select(element);
dropdown.selectByValue("2");
WebElement selectedOption = dropdown.getFirstSelectedOption();
Assert.assertEquals(OPTION_2, selectedOption.getText());
}
}
All tests do the same things:
open the page
assert that the page is displayed
find the dropdown web element
create a select object from the dropdown web element
select an option
assert that the option is actually selected
The only difference is how the option is selected.
First test selects the option by id, second by visible text, third by value.
Which way is the best?
Selecting an option by visible text is not great for 2 reasons.
First, it is quite possible that the visible text of an option changes. When this happens, the test has to be changed as well.
Second, if the site supports multiple languages, you will have different text for the same option for different languages. This implies that the same test may not work for multiple languages.
Selecting an option by index is also not good.
Because the order of the options may change. Or new options may be added.
If your test assumes that an option is the first and then a new option is added before it, the test will not work any longer.
This leaves us with the best case: selecting by value.
The value attribute for each option identifies uniquely an option.
It also has the same value for different languages, even if the visible text is different.
Have a look at this select element in English:
<select id="Sort" name="Sort">
<option value="bestMatch">Best Match</option>
<option value="priceLowToHigh">Price Low-High</option>
<option value="priceHighToLow">Price High-Low</option>
<option value="highestRated">Highest Rated</option>
</select>
And in French:
<select id="Trier" name="Trier">
<option selected="" value="bestMatch">Meilleure correspondance</option>
<option value="priceLowToHigh">Prix Ascendant</option>
<option value="priceHighToLow">Prix Descendant</option>
<option value="highestRated"</option>
</select>
Even if the visible text is different, the value for each option is the same.
So, select list options by value only.
Also,
If you enjoy this almost-free site, please share it on social media and with your friends. Thank you.
One more thing.
If you buy a yearly-subscription to this site, you will get not only all published posts but also
a package of 3 ebooks on Selenium and Java ($35 value)
2 free hours of talking in Zoom about testing/test automation/interviewing/finding a job