0
0
Selenium Javatesting~10 mins

Select by value, visible text, index in Selenium Java - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test opens a webpage with a dropdown menu and selects options using three different methods: by value, by visible text, and by index. It verifies that the correct option is selected each time.

Test Code - JUnit
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.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class DropdownSelectTest {
    WebDriver driver;

    @BeforeEach
    public void setUp() {
        driver = new ChromeDriver();
        driver.get("https://example.com/dropdown");
    }

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

        // Select by value
        dropdown.selectByValue("option2");
        assertEquals("Option 2", dropdown.getFirstSelectedOption().getText());

        // Select by visible text
        dropdown.selectByVisibleText("Option 3");
        assertEquals("Option 3", dropdown.getFirstSelectedOption().getText());

        // Select by index
        dropdown.selectByIndex(1); // index starts at 0
        assertEquals("Option 2", dropdown.getFirstSelectedOption().getText());
    }

    @AfterEach
    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 loaded-PASS
3Finds dropdown element by id 'dropdownMenu'Dropdown element is located on the page-PASS
4Selects option with value 'option2'Dropdown option 'Option 2' is selectedVerify selected option text is 'Option 2'PASS
5Selects option with visible text 'Option 3'Dropdown option 'Option 3' is selectedVerify selected option text is 'Option 3'PASS
6Selects option by index 1Dropdown option at index 1 ('Option 2') is selectedVerify selected option text is 'Option 2'PASS
7Test ends and browser closesBrowser window closed-PASS
Failure Scenario
Failing Condition: Dropdown element not found or option value/text/index does not exist
Execution Trace Quiz - 3 Questions
Test your understanding
Which method selects an option by the exact text shown to the user?
AselectByVisibleText
BselectByValue
CselectByIndex
DselectById
Key Result
Always verify that the dropdown element and options exist before selecting. Use all three selection methods to cover different test needs and ensure robust test coverage.