Bird
Raised Fist0
Selenium Javatesting~20 mins

Keyboard actions (keyDown, keyUp) in Selenium Java - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Keyboard Actions Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Selenium Java code snippet?
Consider the following Selenium Java code that performs keyboard actions on a web element. What will be the final text input in the field after execution?
Selenium Java
WebElement input = driver.findElement(By.id("username"));
Actions actions = new Actions(driver);
actions.keyDown(Keys.SHIFT).sendKeys(input, "hello").keyUp(Keys.SHIFT).perform();
A"hello"
B"HELLO"
C"Hello"
D"hELLO"
Attempts:
2 left
💡 Hint
Remember what holding down the SHIFT key does to letters when typing.
❓ assertion
intermediate
2:00remaining
Which assertion correctly verifies that SHIFT key was released after typing?
You want to assert that after performing keyDown(Keys.SHIFT) and sendKeys, the SHIFT key is no longer pressed. Which assertion is valid in Selenium Java?
Selenium Java
Actions actions = new Actions(driver);
actions.keyDown(Keys.SHIFT).sendKeys("test").keyUp(Keys.SHIFT).perform();
AassertFalse(actions.isShiftDown());
BassertTrue(actions.isShiftDown());
CassertEquals(false, actions.isShiftDown());
DassertNull(actions.getShiftState());
Attempts:
2 left
💡 Hint
Check if the SHIFT key is pressed or not after keyUp.
🔧 Debug
advanced
2:00remaining
Why does this Selenium Java code throw an exception?
Examine the code below. It throws an exception at runtime. What is the cause?
Selenium Java
WebElement input = driver.findElement(By.id("inputField"));
Actions actions = new Actions(driver);
actions.keyDown(Keys.CONTROL).sendKeys("a").perform();
actions.keyUp(Keys.CONTROL).perform();
AkeyUp(Keys.CONTROL) must be called before perform()
BsendKeys("a") is invalid without keyUp
CkeyDown and keyUp must be chained in the same Actions before perform()
Dinput element is not focused before sending keys
Attempts:
2 left
💡 Hint
Check how keyDown and keyUp are chained in Actions.
🧠 Conceptual
advanced
2:00remaining
What is the main difference between keyDown and sendKeys in Selenium Actions?
Choose the best explanation of how keyDown and sendKeys differ in Selenium Actions.
AkeyDown presses and holds a key, sendKeys types characters or keys
BkeyDown types characters, sendKeys presses and holds keys
CkeyDown releases a key, sendKeys presses a key
DkeyDown and sendKeys perform the same action
Attempts:
2 left
💡 Hint
Think about holding keys vs typing characters.
❓ framework
expert
3:00remaining
In a Selenium Java test framework, how to best implement a reusable method for typing uppercase text using keyboard actions?
You want a reusable method that types a given string in uppercase by holding SHIFT using Selenium Actions. Which method implementation is correct and efficient?
Selenium Java
public void typeUppercase(WebElement element, String text) {
    Actions actions = new Actions(driver);
    // fill in
}
Aactions.keyDown(Keys.SHIFT).sendKeys(element, text.toUpperCase()).keyUp(Keys.SHIFT).perform();
Bactions.keyDown(Keys.SHIFT).sendKeys(text).keyUp(Keys.SHIFT).perform();
Cactions.sendKeys(element, text.toUpperCase()).perform();
Dactions.keyDown(Keys.SHIFT).sendKeys(element, text).keyUp(Keys.SHIFT).perform();
Attempts:
2 left
💡 Hint
SHIFT key changes letter case during typing; no need to convert string manually.

Practice

(1/5)
1. What does the keyDown method do in Selenium keyboard actions?
easy
A. Types text into an input field
B. Releases a specified key
C. Presses and holds down a specified key
D. Clicks on a web element

Solution

  1. Step 1: Understand the purpose of keyDown

    The keyDown method simulates pressing and holding a key on the keyboard.
  2. Step 2: Differentiate from keyUp

    keyUp releases the key, so it is not the same as keyDown.
  3. Final Answer:

    Presses and holds down a specified key -> Option C
  4. Quick Check:

    keyDown = press and hold key [OK]
Hint: Remember: keyDown = press and hold key [OK]
Common Mistakes:
  • Confusing keyDown with keyUp
  • Thinking keyDown types text
  • Assuming keyDown clicks elements
2. Which of the following is the correct syntax to press and hold the SHIFT key using Selenium Actions in Java?
easy
A. actions.keyDown(Keys.SHIFT).perform();
B. actions.keyUp(Keys.SHIFT).perform();
C. actions.pressKey(Keys.SHIFT).perform();
D. actions.holdKey(Keys.SHIFT).perform();

Solution

  1. Step 1: Recall the correct method name

    The method to press and hold a key is keyDown, not keyUp or others.
  2. Step 2: Check method chaining and parameters

    The syntax actions.keyDown(Keys.SHIFT).perform(); correctly presses and holds SHIFT key.
  3. Final Answer:

    actions.keyDown(Keys.SHIFT).perform(); -> Option A
  4. Quick Check:

    keyDown + perform() = press and hold key [OK]
Hint: Use keyDown(Keys.KEY).perform() to hold keys [OK]
Common Mistakes:
  • Using keyUp instead of keyDown to press key
  • Using non-existent methods like pressKey or holdKey
  • Forgetting to call perform() to execute action
3. What will be the effect of the following Selenium Java code snippet?
Actions actions = new Actions(driver);
actions.keyDown(Keys.CONTROL).sendKeys("a").keyUp(Keys.CONTROL).perform();
medium
A. Releases the CONTROL key before pressing 'a'
B. Types the letter 'a' without any modifier
C. Throws a runtime exception due to wrong method order
D. Selects all text on the active element

Solution

  1. Step 1: Analyze the keyDown and sendKeys sequence

    The code presses and holds CONTROL, then sends 'a', then releases CONTROL.
  2. Step 2: Understand the effect of CONTROL + 'a'

    CONTROL + 'a' is the common shortcut to select all text in many applications.
  3. Final Answer:

    Selects all text on the active element -> Option D
  4. Quick Check:

    CONTROL + a = select all [OK]
Hint: CONTROL + a usually means select all text [OK]
Common Mistakes:
  • Thinking it types 'a' without CONTROL
  • Assuming method order causes error
  • Ignoring keyUp releases key after sendKeys
4. Identify the error in this Selenium Java code snippet:
Actions actions = new Actions(driver);
actions.keyUp(Keys.SHIFT).sendKeys("hello").perform();
medium
A. sendKeys cannot be chained after keyUp
B. keyUp is called without a preceding keyDown for SHIFT
C. perform() is missing at the end
D. Keys.SHIFT is not a valid key constant

Solution

  1. Step 1: Understand keyUp usage

    keyUp releases a key that must have been pressed before with keyDown.
  2. Step 2: Check the code sequence

    The code calls keyUp(Keys.SHIFT) without pressing SHIFT first, which is incorrect.
  3. Final Answer:

    keyUp is called without a preceding keyDown for SHIFT -> Option B
  4. Quick Check:

    keyUp needs prior keyDown [OK]
Hint: Always press keyDown before keyUp for same key [OK]
Common Mistakes:
  • Calling keyUp without keyDown first
  • Assuming sendKeys can't follow keyUp
  • Forgetting perform() call
5. You want to simulate typing uppercase text "HELLO" in an input box using Selenium Actions. Which code snippet correctly achieves this using keyDown and keyUp?
hard
A. actions.keyDown(Keys.SHIFT).sendKeys("hello").keyUp(Keys.SHIFT).perform();
B. actions.sendKeys("HELLO").perform();
C. actions.keyDown(Keys.SHIFT).sendKeys("HELLO").keyUp(Keys.SHIFT).perform();
D. actions.keyUp(Keys.SHIFT).sendKeys("hello").keyDown(Keys.SHIFT).perform();

Solution

  1. Step 1: Understand how to type uppercase with SHIFT

    Holding SHIFT and typing lowercase letters results in uppercase letters.
  2. 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.
  3. Final Answer:

    actions.keyDown(Keys.SHIFT).sendKeys("hello").keyUp(Keys.SHIFT).perform(); -> Option A
  4. Quick Check:

    Hold SHIFT + lowercase = uppercase letters [OK]
Hint: Hold SHIFT and type lowercase to get uppercase [OK]
Common Mistakes:
  • Typing uppercase letters while holding SHIFT
  • Releasing SHIFT before typing
  • Not using keyDown/keyUp for SHIFT
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard