0
0
Selenium Javatesting~10 mins

Checkbox handling in Selenium Java - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test opens a webpage with a checkbox, clicks the checkbox to select it, and verifies that the checkbox 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.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class CheckboxTest {
    private WebDriver driver;

    @BeforeEach
    public void setUp() {
        driver = new ChromeDriver();
    }

    @Test
    public void testCheckboxSelection() {
        driver.get("https://example.com/checkboxpage");
        WebElement checkbox = driver.findElement(By.id("subscribeCheckbox"));
        if (!checkbox.isSelected()) {
            checkbox.click();
        }
        assertTrue(checkbox.isSelected(), "Checkbox should be selected after clicking.");
    }

    @AfterEach
    public void tearDown() {
        driver.quit();
    }
}
Execution Trace - 6 Steps
StepActionSystem StateAssertionResult
1Test starts and Chrome browser opensChrome browser window is open and ready-PASS
2Navigates to https://example.com/checkboxpagePage with checkbox loaded in browser-PASS
3Finds checkbox element by id 'subscribeCheckbox'Checkbox element is located on the page-PASS
4Checks if checkbox is not selected, then clicks itCheckbox is now selected-PASS
5Asserts checkbox is selectedCheckbox remains selectedassertTrue(checkbox.isSelected()) verifies checkbox is selectedPASS
6Test ends and browser closesBrowser window closed-PASS
Failure Scenario
Failing Condition: Checkbox element with id 'subscribeCheckbox' is not found or not clickable
Execution Trace Quiz - 3 Questions
Test your understanding
What does the test verify after clicking the checkbox?
AThat the checkbox is visible
BThat the checkbox is selected
CThat the checkbox is disabled
DThat the checkbox is unchecked
Key Result
Always verify the checkbox state before clicking to avoid unnecessary clicks and ensure your locator is correct and the page is fully loaded before interacting.