0
0
Selenium Javatesting~20 mins

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

Choose your learning style9 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.