Test Overview
This test checks if holding down the SHIFT key while typing in an input box results in uppercase text. It verifies that keyboard actions like keyDown and keyUp work correctly.
This test checks if holding down the SHIFT key while typing in an input box results in uppercase text. It verifies that keyboard actions like keyDown and keyUp work correctly.
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; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; public class KeyboardActionsTest { WebDriver driver; Actions actions; @BeforeEach public void setUp() { driver = new ChromeDriver(); actions = new Actions(driver); driver.get("https://example.com/keyboard-input"); } @Test public void testShiftKeyDownAndUp() { WebElement inputBox = driver.findElement(By.id("input-text")); inputBox.click(); actions.keyDown(Keys.SHIFT) .sendKeys("hello") .keyUp(Keys.SHIFT) .perform(); String enteredText = inputBox.getAttribute("value"); assertEquals("HELLO", enteredText); } @AfterEach public void tearDown() { driver.quit(); } }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts and Chrome browser opens | Browser window is open and ready | — | PASS |
| 2 | Navigates to https://example.com/keyboard-input | Page with input box loaded | Page loaded successfully | PASS |
| 3 | Finds input box element by id 'input-text' | Input box is visible and interactable | Element located | PASS |
| 4 | Clicks on the input box to focus | Input box is focused and ready for typing | — | PASS |
| 5 | Performs keyDown SHIFT, types 'hello', then keyUp SHIFT | Input box contains typed text | Keyboard actions executed correctly | PASS |
| 6 | Gets the value attribute from input box | Input box value is 'HELLO' | Assert that input value equals 'HELLO' | PASS |
| 7 | Test ends and browser closes | Browser closed | — | PASS |
keyDown method do in Selenium keyboard actions?keyDownkeyDown method simulates pressing and holding a key on the keyboard.keyUpkeyUp releases the key, so it is not the same as keyDown.keyDown = press and hold key [OK]keyDown, not keyUp or others.actions.keyDown(Keys.SHIFT).perform(); correctly presses and holds SHIFT key.Actions actions = new Actions(driver);
actions.keyDown(Keys.CONTROL).sendKeys("a").keyUp(Keys.CONTROL).perform();Actions actions = new Actions(driver);
actions.keyUp(Keys.SHIFT).sendKeys("hello").perform();keyUp releases a key that must have been pressed before with keyDown.keyUp(Keys.SHIFT) without pressing SHIFT first, which is incorrect.keyDown and keyUp?Pass the test and this pattern moves unknown