0
0
Selenium Javatesting~10 mins

Click actions in Selenium Java - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test opens a web page, finds a button by its ID, clicks it, and verifies that a confirmation message appears.

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.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
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 ClickActionTest {
    private WebDriver driver;
    private WebDriverWait wait;

    @BeforeEach
    public void setUp() {
        driver = new ChromeDriver();
        wait = new WebDriverWait(driver, 10);
    }

    @Test
    public void testButtonClickShowsMessage() {
        driver.get("https://example.com/click-test");
        WebElement button = wait.until(ExpectedConditions.elementToBeClickable(By.id("click-button")));
        button.click();
        WebElement message = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("confirmation-message")));
        assertEquals("Button clicked!", message.getText());
    }

    @AfterEach
    public void tearDown() {
        if (driver != null) {
            driver.quit();
        }
    }
}
Execution Trace - 7 Steps
StepActionSystem StateAssertionResult
1Test starts and Chrome browser opensChrome browser window is open and ready-PASS
2Navigates to URL https://example.com/click-testPage loads with a button having id 'click-button'-PASS
3Waits until button with id 'click-button' is clickable and finds itButton is visible and enabled for clickingChecks button is clickablePASS
4Clicks the buttonButton is clicked, triggering page action-PASS
5Waits until confirmation message with id 'confirmation-message' is visibleConfirmation message appears on the pageChecks message is visiblePASS
6Verifies the confirmation message text equals 'Button clicked!'Message text is displayed as expectedassertEquals("Button clicked!", message.getText())PASS
7Test ends and browser closesBrowser window is closed-PASS
Failure Scenario
Failing Condition: Button with id 'click-button' is not found or not clickable
Execution Trace Quiz - 3 Questions
Test your understanding
What does the test verify after clicking the button?
AThat a confirmation message with text 'Button clicked!' appears
BThat the button disappears from the page
CThat the page URL changes
DThat an alert popup appears
Key Result
Always wait explicitly for elements to be clickable before clicking to avoid flaky tests.