_java
TL;DR - Show Me The Code
package com.elemental.selenium;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Assertions;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.By;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
public class DropdownTest {
WebDriver driver;
@BeforeEach
public void beforeEach() {
driver = new FirefoxDriver();
}
@AfterEach
public void afterEach() {
if (driver != null) {
driver.quit();
}
}
@Test
public void dropdownTest() {
driver.get("https://the-internet.herokuapp.com/dropdown");
WebElement dropdownList = driver.findElement(By.id("dropdown"));
List<WebElement> options = dropdownList.findElements(By.tagName("option"));
for (int i = 0; i < options.size(); i++) {
if (options.get(i).getText().equals("Option 1")) {
options.get(i).click();
}
}
String selectedOption = "";
for (int i = 0; i < options.size(); i++) {
if (options.get(i).isSelected()) {
selectedOption = options.get(i).getText();
}
}
Assertions.assertEquals("Option 1", selectedOption);
}
@Test
public void dropdownTestRedux() {
driver.get("https://the-internet.herokuapp.com/dropdown");
Select selectList = new Select(driver.findElement(By.id("dropdown")));
selectList.selectByVisibleText("Option 1");
// You could also use select.selectByValue("1");
Assertions.assertEquals("Option 1", selectList.getFirstSelectedOption().getText());
}
}
Code Walkthrough
Importing Libraries, Setup and Teardown
First let's import our requisite classes (for annotations (e.g., org.junit.After
, etc.), driving the
browser with Selenium (e.g., org.openqa.selenium.WebDriver
, etc.), and matchers for our assertions
(e.g., org.junit.jupiter.api.Assertions
, etc.)), and start our class with some setup and teardown
methods.
Next, we'll start our class off with some setup and teardown methods.
Example 1
Our first example is dropdownTest
, after visiting the example application we find the dropdown list by
its ID and store it in a variable. We then find each clickable element in the dropdown list (e.g., each option
)
with findElements
(note the plural).
Grabbing all options with findElements
returns a collection that we iterate over. When the text matches
what we want, we click on it.
We finish the test by performing a check to see that our selection was made correctly. This is done by iterating over the dropdown options collection one more time. This time we're getting the text of the item that was selected, storing it in a variable, and making an assertion against it.
While this works, there is a more accessible way to do this.
Example 2
Our second example is dropdownTestRedux
, and similar to the first example, we are finding the dropdown list by
its ID. But instead of iterating over its option elements and clicking based on a conditional we are leveraging
a built-in helper function of Selenium. With Select
and it's selectBy
methods
(e.g., selectByVisibleText
) we're able to easily choose the item we want.
We then ask the selectList
what option was selected by using getFirstSelectedOption
and perform our
assertion against it's text.
As an aside, in addition to selecting by text you can also select by value.
select.selectByValue("1");
Executing the Test
Before executing the test, we need to make sure the required dependencies are declared on the pom.xml
file.
Toggle to see the pom.xml
file.
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.elemental.selenium</groupId>
<artifactId>tips</artifactId>
<version>1.0.0</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.24.0</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.11.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Finally, we can run the test by executing mvn test
from the command-line.