0
0
Selenium Javatesting~10 mins

Click and hold in Selenium Java - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test opens a webpage, finds a button, clicks and holds it, then verifies the button's text changes to show it is held.

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.interactions.Actions;
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 ClickAndHoldTest {
    WebDriver driver;
    Actions actions;

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

    @Test
    public void testClickAndHoldButton() {
        driver.get("https://example.com/click-and-hold");
        WebElement button = driver.findElement(By.id("hold-button"));
        actions.clickAndHold(button).perform();
        String buttonText = button.getText();
        assertEquals("Holding", buttonText);
    }

    @AfterEach
    public void tearDown() {
        driver.quit();
    }
}
Execution Trace - 7 Steps
StepActionSystem StateAssertionResult
1Test startsTest environment initialized, ChromeDriver ready-PASS
2Browser opens ChromeChrome browser window opened, blank page-PASS
3Navigates to https://example.com/click-and-holdPage with a button having id 'hold-button' loaded-PASS
4Finds element by id 'hold-button'Button element located on the pageElement is present and interactablePASS
5Clicks and holds the button using Actions.clickAndHold().perform()Button visually pressed and held down-PASS
6Gets button text after click and holdButton text changed to 'Holding'Button text equals 'Holding'PASS
7Test ends and browser closesBrowser closed, resources released-PASS
Failure Scenario
Failing Condition: Button with id 'hold-button' is not found or text does not change to 'Holding' after click and hold
Execution Trace Quiz - 3 Questions
Test your understanding
What Selenium method is used to perform the click and hold action?
Aactions.doubleClick(element).perform()
Bactions.clickAndHold(element).perform()
Celement.click()
Dactions.release(element).perform()
Key Result
Use Selenium Actions class for complex mouse interactions like click and hold, and always verify UI changes after the action.