Test Overview
This test opens a web page with a search input box, types the text "Selenium testing" into it using sendKeys, and verifies that the input box contains the typed text.
This test opens a web page with a search input box, types the text "Selenium testing" into it using sendKeys, and verifies that the input box contains the typed text.
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.junit.Assert; import org.junit.After; import org.junit.Before; import org.junit.Test; public class TypingTextTest { private WebDriver driver; @Before public void setUp() { driver = new ChromeDriver(); } @Test public void testTypingText() { driver.get("https://example.com/search"); WebElement searchInput = driver.findElement(By.id("search-box")); searchInput.sendKeys("Selenium testing"); String enteredText = searchInput.getAttribute("value"); Assert.assertEquals("Selenium testing", enteredText); } @After public void tearDown() { driver.quit(); } }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts and Chrome browser opens | Chrome browser window is open and ready | — | PASS |
| 2 | Navigates to URL https://example.com/search | Browser displays the search page with input box having id 'search-box' | — | PASS |
| 3 | Finds the input element by id 'search-box' | Input element is located and ready for interaction | — | PASS |
| 4 | Types text 'Selenium testing' into the input box using sendKeys | Input box now contains the text 'Selenium testing' | — | PASS |
| 5 | Retrieves the value attribute of the input box | Value attribute is 'Selenium testing' | Assert that the input box value equals 'Selenium testing' | PASS |
| 6 | Test ends and browser closes | Browser window is closed | — | PASS |
sendKeys() method do in Selenium WebDriver?sendKeys() method simulates typing text into input fields.sendKeys().sendKeys() on it with the text.findElement and sendKeys correctly. Others use invalid methods or wrong parameters.WebElement input = driver.findElement(By.name("username"));
input.sendKeys("user123");
String value = input.getAttribute("value");
System.out.println(value);sendKeys("user123") types "user123" into the input field.getAttribute("value") returns the current text inside the input, which is "user123".WebElement password = driver.findElement(By.id("pass"));
password.sendkeys("admin");sendKeys with uppercase 'K'.By.id("pass") is valid and sendKeys works on password fields. Driver initialization is assumed done.clear() method empties the input field safely before typing.sendKeys("hello world") types the new text correctly.Pass the test and this pattern moves unknown