Skip to main content

_selenide

TL;DR - Show Me The Code

5-select-from-a-dropdown/code/selenide/src/test/java/com/elemental/selenium/DropdownTest.java
package com.elemental.selenium;

import com.codeborne.selenide.SelenideElement;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static com.codeborne.selenide.CollectionCondition.texts;
import static com.codeborne.selenide.Condition.text;
import static com.codeborne.selenide.Selenide.$;
import static com.codeborne.selenide.Selenide.open;

public class DropdownTest {
private final SelenideElement dropdownList = $("#dropdown");

@BeforeEach
void openAUT() {
open("https://the-internet.herokuapp.com/dropdown");
}

@Test
void checkAvailableOptions() {
dropdownList.getOptions()
.shouldHave(texts("Please select an option", "Option 1", "Option 2"));
}

@Test
void canSelectOptionByText() {
dropdownList.selectOption("Option 1");
dropdownList.getSelectedOption().shouldHave(text("Option 1"));
}

@Test
void canSelectOptionByValue() {
dropdownList.selectOptionByValue("2");
dropdownList.getSelectedOption().shouldHave(text("Option 2"));
}
}

Code Walkthrough

Importing Libraries

First let's import our requisite classes:

  • for annotations (e.g., org.junit.jupiter.api.Test),
  • opening the browser with Selenide (e.g., com.codeborne.selenide.Selenide.open),
  • matchers for assertions (e.g., com.codeborne.selenide.Condition.text)

Next, we'll start our test.

Example 1: checkAvailableOptions

Our first example checkAvailableOptions shows how we can find the dropdown list by its ID and verify that it contains all the expected options.

Example 2: canSelectOptionByText

Our second example canSelectOptionByText shows how we can select an option by text:

dropdownList.selectOption("Option 1");

We then verify that the expected option is actually selected:

dropdownList.getSelectedOption().shouldHave(text("Option 1"));

Example 3: canSelectOptionByValue

Our third example canSelectOptionByValue shows how we can select an option by text:

dropdownList.selectOptionByValue("2");

We then verify that the expected option is actually selected:

dropdownList.getSelectedOption().shouldHave(text("Option 2"));

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.
5-select-from-a-dropdown/code/selenide/pom.xml
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.elemental.selenium</groupId>
<artifactId>tips-selenide-work-with-dropdown</artifactId>
<version>1.0.0</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>com.codeborne</groupId>
<artifactId>selenide</artifactId>
<version>7.5.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.11.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.26.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>2.0.16</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

Finally, we can run the test by executing mvn test from the command-line.