0
0
Selenium Javatesting~15 mins

Keyboard actions (keyDown, keyUp) in Selenium Java - Build an Automation Script

Choose your learning style9 modes available
Test keyboard actions keyDown and keyUp on input field
Preconditions (2)
Step 1: Open the browser and navigate to http://example.com/keyboard-test
Step 2: Locate the input field with id 'input-box'
Step 3: Click on the input field to focus
Step 4: Press and hold the SHIFT key (keyDown)
Step 5: Type the letters 'abc' while SHIFT is held down
Step 6: Release the SHIFT key (keyUp)
Step 7: Verify that the input field contains 'ABC'
✅ Expected Result: The input field should contain the uppercase text 'ABC' after typing with SHIFT key held down.
Automation Requirements - Selenium WebDriver with Java
Assertions Needed:
Verify the input field text equals 'ABC'
Best Practices:
Use Actions class for keyboard events
Use explicit waits to ensure elements are interactable
Use By.id locator for the input field
Close the browser after test execution
Automated Solution
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.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.time.Duration;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class KeyboardActionsTest {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
        WebDriver driver = new ChromeDriver();
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
        try {
            driver.get("http://example.com/keyboard-test");

            WebElement inputBox = wait.until(ExpectedConditions.elementToBeClickable(By.id("input-box")));
            inputBox.click();

            Actions actions = new Actions(driver);
            actions.keyDown(inputBox, org.openqa.selenium.Keys.SHIFT)
                   .sendKeys(inputBox, "abc")
                   .keyUp(inputBox, org.openqa.selenium.Keys.SHIFT)
                   .perform();

            String enteredText = inputBox.getAttribute("value");
            assertEquals("ABC", enteredText, "Input text should be uppercase 'ABC'");
        } finally {
            driver.quit();
        }
    }
}

This test script uses Selenium WebDriver with Java to automate keyboard actions.

First, it opens the browser and navigates to the test page.

It waits explicitly for the input field with id 'input-box' to be clickable, then clicks it to focus.

Using the Actions class, it presses and holds the SHIFT key (keyDown), types 'abc', then releases the SHIFT key (keyUp).

This simulates typing uppercase letters because SHIFT is held down.

Finally, it retrieves the input field's value and asserts it equals 'ABC'.

The browser closes after the test to clean up.

Common Mistakes - 4 Pitfalls
Using Thread.sleep instead of explicit waits
Not focusing the input field before sending keys
{'mistake': 'Using sendKeys without keyDown and keyUp for SHIFT', 'why_bad': "Typing 'abc' without SHIFT keyDown will input lowercase letters, not uppercase.", 'correct_approach': 'Use Actions.keyDown(Keys.SHIFT) before sendKeys and keyUp(Keys.SHIFT) after to simulate holding SHIFT.'}
Using incorrect locator strategies like XPath with absolute paths
Bonus Challenge

Now add data-driven testing with 3 different input strings: 'abc', 'def', 'ghi' to verify uppercase conversion.

Show Hint