0
0
Selenium Javatesting~10 mins

Select class for dropdowns in Selenium Java - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test opens a webpage with a dropdown menu, selects an option by visible text using Selenium's Select class, and verifies the correct option is selected.

Test Code - JUnit with Selenium WebDriver
Selenium Java
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
import org.junit.Assert;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class DropdownSelectTest {
    WebDriver driver;

    @Before
    public void setUp() {
        // System property for ChromeDriver should be set before initializing ChromeDriver
        // System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
        driver = new ChromeDriver();
        driver.get("https://example.com/dropdown");
    }

    @Test
    public void testSelectDropdownOption() {
        WebElement dropdownElement = driver.findElement(By.id("dropdownMenu"));
        Select dropdown = new Select(dropdownElement);
        dropdown.selectByVisibleText("Option 2");

        WebElement selectedOption = dropdown.getFirstSelectedOption();
        String selectedText = selectedOption.getText();
        Assert.assertEquals("Option 2", selectedText);
    }

    @After
    public void tearDown() {
        driver.quit();
    }
}
Execution Trace - 7 Steps
StepActionSystem StateAssertionResult
1Test starts and Chrome browser opensBrowser window is open, ready to navigate-PASS
2Navigates to https://example.com/dropdownPage with dropdown menu loadedPage title or URL confirms correct pagePASS
3Finds dropdown element by id 'dropdownMenu'Dropdown element is located on the pageElement is present and visiblePASS
4Creates Select object for the dropdown elementSelect class instance ready to interact with dropdown-PASS
5Selects option with visible text 'Option 2'Dropdown option 'Option 2' is selected-PASS
6Gets the first selected option textSelected option text retrieved as 'Option 2'Assert that selected option text equals 'Option 2'PASS
7Test ends and browser closesBrowser window closed-PASS
Failure Scenario
Failing Condition: Dropdown element with id 'dropdownMenu' is not found on the page
Execution Trace Quiz - 3 Questions
Test your understanding
Which Selenium class is used to interact with dropdown menus?
AWebDriverWait
BSelect
CActions
DJavascriptExecutor
Key Result
Use Selenium's Select class to handle dropdown menus easily and verify selections by checking the selected option's visible text.