0
0
Selenium Javatesting~5 mins

Keyboard actions (keyDown, keyUp) in Selenium Java

Choose your learning style9 modes available
Introduction

Keyboard actions let you simulate pressing and releasing keys on the keyboard. This helps test how a web page reacts to keyboard input.

When you want to test keyboard shortcuts like Ctrl+C or Ctrl+V on a webpage.
When you need to hold down a key while clicking or typing, like Shift for selecting text.
When testing form inputs that react to key presses, such as pressing Enter to submit.
When automating games or apps that require keyboard controls.
When verifying accessibility features that depend on keyboard navigation.
Syntax
Selenium Java
Actions actions = new Actions(driver);
actions.keyDown(Keys.KEY).perform();
actions.keyUp(Keys.KEY).perform();

Use keyDown to press and hold a key.

Use keyUp to release the key.

Examples
This types "HELLO" by holding the Shift key while typing.
Selenium Java
actions.keyDown(Keys.SHIFT).sendKeys("hello").keyUp(Keys.SHIFT).perform();
This simulates Ctrl+A to select all text.
Selenium Java
actions.keyDown(Keys.CONTROL).sendKeys("a").keyUp(Keys.CONTROL).perform();
This holds down Alt key, then releases it later.
Selenium Java
actions.keyDown(Keys.ALT).perform();
// do something while Alt is held
actions.keyUp(Keys.ALT).perform();
Sample Program

This test opens Google, clicks the search box, holds Shift while typing 'hello' to get uppercase 'HELLO', then checks if the input value matches.

Selenium Java
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;

public class KeyboardActionsTest {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
        WebDriver driver = new ChromeDriver();
        try {
            driver.get("https://www.google.com");
            WebElement searchBox = driver.findElement(By.name("q"));
            Actions actions = new Actions(driver);

            // Click the search box
            searchBox.click();

            // Hold SHIFT, type 'hello', release SHIFT
            actions.keyDown(Keys.SHIFT).sendKeys("hello").keyUp(Keys.SHIFT).perform();

            // Verify the input value is 'HELLO'
            String value = searchBox.getAttribute("value");
            if ("HELLO".equals(value)) {
                System.out.println("Test Passed: Text is uppercase as expected.");
            } else {
                System.out.println("Test Failed: Text is '" + value + "' instead of 'HELLO'.");
            }
        } finally {
            driver.quit();
        }
    }
}
OutputSuccess
Important Notes

Always call perform() to execute the keyboard actions.

Use Keys enum for special keys like SHIFT, CONTROL, ALT, ENTER.

Remember to release keys with keyUp to avoid stuck keys in tests.

Summary

Keyboard actions simulate pressing and releasing keys in Selenium.

Use keyDown to press and hold a key, keyUp to release it.

This helps test keyboard shortcuts and input behavior on web pages.