Keyboard actions (keyDown, keyUp) in Selenium Java - Build an Automation Script
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.
Now add data-driven testing with 3 different input strings: 'abc', 'def', 'ghi' to verify uppercase conversion.
Practice
keyDown method do in Selenium keyboard actions?Solution
Step 1: Understand the purpose of
ThekeyDownkeyDownmethod simulates pressing and holding a key on the keyboard.Step 2: Differentiate from
keyUpkeyUpreleases the key, so it is not the same askeyDown.Final Answer:
Presses and holds down a specified key -> Option CQuick Check:
keyDown= press and hold key [OK]
- Confusing keyDown with keyUp
- Thinking keyDown types text
- Assuming keyDown clicks elements
Solution
Step 1: Recall the correct method name
The method to press and hold a key iskeyDown, notkeyUpor others.Step 2: Check method chaining and parameters
The syntaxactions.keyDown(Keys.SHIFT).perform();correctly presses and holds SHIFT key.Final Answer:
actions.keyDown(Keys.SHIFT).perform(); -> Option AQuick Check:
keyDown + perform() = press and hold key [OK]
- Using keyUp instead of keyDown to press key
- Using non-existent methods like pressKey or holdKey
- Forgetting to call perform() to execute action
Actions actions = new Actions(driver);
actions.keyDown(Keys.CONTROL).sendKeys("a").keyUp(Keys.CONTROL).perform();Solution
Step 1: Analyze the keyDown and sendKeys sequence
The code presses and holds CONTROL, then sends 'a', then releases CONTROL.Step 2: Understand the effect of CONTROL + 'a'
CONTROL + 'a' is the common shortcut to select all text in many applications.Final Answer:
Selects all text on the active element -> Option DQuick Check:
CONTROL + a = select all [OK]
- Thinking it types 'a' without CONTROL
- Assuming method order causes error
- Ignoring keyUp releases key after sendKeys
Actions actions = new Actions(driver);
actions.keyUp(Keys.SHIFT).sendKeys("hello").perform();Solution
Step 1: Understand keyUp usage
keyUpreleases a key that must have been pressed before withkeyDown.Step 2: Check the code sequence
The code callskeyUp(Keys.SHIFT)without pressing SHIFT first, which is incorrect.Final Answer:
keyUp is called without a preceding keyDown for SHIFT -> Option BQuick Check:
keyUp needs prior keyDown [OK]
- Calling keyUp without keyDown first
- Assuming sendKeys can't follow keyUp
- Forgetting perform() call
keyDown and keyUp?Solution
Step 1: Understand how to type uppercase with SHIFT
Holding SHIFT and typing lowercase letters results in uppercase letters.Step 2: Analyze each option
actions.keyDown(Keys.SHIFT).sendKeys("hello").keyUp(Keys.SHIFT).perform(); holds SHIFT, types "hello" (lowercase), then releases SHIFT, producing "HELLO". actions.sendKeys("HELLO").perform(); types "HELLO" directly but does not use keyDown/keyUp. actions.keyDown(Keys.SHIFT).sendKeys("HELLO").keyUp(Keys.SHIFT).perform(); holds SHIFT and types "HELLO" which results in double shift effect (may cause unexpected input). actions.keyUp(Keys.SHIFT).sendKeys("hello").keyDown(Keys.SHIFT).perform(); releases SHIFT before typing, which is incorrect.Final Answer:
actions.keyDown(Keys.SHIFT).sendKeys("hello").keyUp(Keys.SHIFT).perform(); -> Option AQuick Check:
Hold SHIFT + lowercase = uppercase letters [OK]
- Typing uppercase letters while holding SHIFT
- Releasing SHIFT before typing
- Not using keyDown/keyUp for SHIFT
Ready to prove it?
Pass the test and this pattern moves unknown
