Skip to content
CodeKruPro

Keyboard - Actions Class Key Events

57/70

Keyboard Actions with Actions Class

The Actions class provides more granular keyboard control:

  • keyDown(Keys.SHIFT) - press and hold a key
  • keyUp(Keys.SHIFT) - release a key
  • sendKeys("text") - type text

Task:

  1. Navigate to https://testkru.com/Elements/TextFields
  2. Find the firstName field
  3. In one Actions chain: click the field so it has focus, hold Keys.SHIFT with keyDown(), type the lower-case word "hello", release Shift with keyUp(), and finish with perform()
  4. Read the field's value and print it after the label Typed:

Holding Shift turns the lower-case letters into capitals, so the field ends up holding HELLO.

Expected output:

Typed: HELLO

About this lesson

keyDown and keyUp in Selenium Java: hold a key with the Actions class

Learn Selenium JavaAdvancedKeyboard Actions

How it works

keyDown(Keys.SHIFT) presses a key and keeps it down; keyUp(Keys.SHIFT) releases it. Everything typed in between is typed with the key held, so holding Shift while sending lower-case letters gives upper-case text, exactly as on a real keyboard.

Actions send keys to whatever has focus, not to an element you name. Without focus, the letters go to the page body and the field stays empty. click(field) at the start of the chain, keyDown(field, Keys.SHIFT) or sendKeys(field, text) all focus the field first.

Releasing matters. If the chain ends without keyUp(), Shift stays down: in this runner later Actions typing comes out in capitals, and so does the next WebElement.sendKeys().

About the assignment

The starter gives you the first-name field and an Actions object. You type a word with Shift held so it comes out in capitals, then read the field's value.

What you will do

  1. Start the chain by focusing the field, for example with click() on it.
  2. Add keyDown(Keys.SHIFT), then sendKeys() with the lower-case word from the task, then keyUp(Keys.SHIFT).
  3. Finish the chain with perform().
  4. Read getAttribute("value") and print it after the label Typed:.

Common mistakes

  • Not focusing the field

    The letters go to the page body, and the field's value is an empty string.

  • Leaving out keyUp()

    Shift stays held, so the next text you type, through Actions or the next sendKeys() call, comes out in capitals.

  • Typing the word in capitals

    The point is that the held key changes the text. Sending upper-case letters skips the lesson, though the output would look the same.

In a real test suite

Held keys power Shift-click range selection in lists, Ctrl-click multi-selection, and keyboard shortcuts in editors and spreadsheets. Chains such as keyDown(Keys.CONTROL).click(first).click(second).keyUp(Keys.CONTROL) select several items in one gesture.

Lesson 56 sent a key chord in one sendKeys(). Lesson 58 starts on JavascriptExecutor, which runs JavaScript in the page.

To try one of these calls on any page without a task, open the Online Selenium Java Compiler.

Starter code

The editor above opens with this code. Fill in the TODO lines, then run and submit it.

Main.java
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;

public class Main {
    public static void main(String[] args) throws Exception {
        WebDriver driver = new ChromeDriver();
        driver.get("https://testkru.com/Elements/TextFields");
        
        WebElement field = driver.findElement(By.id("firstName"));
        Actions actions = new Actions(driver);
        
        // TODO: Click the field, hold Shift, type "hello", release Shift, then perform the chain
        // TODO: Print the field's value after the label "Typed: "
        
        driver.quit();
    }
}