0
0
Selenium Javatesting~15 mins

Typing text (sendKeys) in Selenium Java - Build an Automation Script

Choose your learning style9 modes available
Verify typing text into the search input field
Preconditions (1)
Step 1: Locate the search input field by its id 'search-input'
Step 2: Click on the search input field to focus
Step 3: Type the text 'Selenium testing' into the search input field
Step 4: Verify that the search input field contains the text 'Selenium testing'
✅ Expected Result: The search input field should contain the exact text 'Selenium testing' after typing
Automation Requirements - Selenium WebDriver with Java
Assertions Needed:
Verify the search input field contains the typed text 'Selenium testing'
Best Practices:
Use explicit waits to ensure the search input field is visible and enabled before typing
Use By.id locator for the search input field
Use assertions from a testing framework like TestNG or JUnit
Keep code readable and maintainable
Automated Solution
Selenium Java
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.time.Duration;

public class TypingTextTest {
    private WebDriver driver;
    private WebDriverWait wait;

    @BeforeClass
    public void setUp() {
        // Set path to chromedriver if needed
        driver = new ChromeDriver();
        wait = new WebDriverWait(driver, Duration.ofSeconds(10));
        driver.get("https://example.com"); // Replace with actual URL
    }

    @Test
    public void testTypingTextInSearchInput() {
        // Wait until the search input is visible and enabled
        WebElement searchInput = wait.until(ExpectedConditions.elementToBeClickable(By.id("search-input")));

        // Click to focus
        searchInput.click();

        // Type text
        String textToType = "Selenium testing";
        searchInput.sendKeys(textToType);

        // Verify the input contains the typed text
        String enteredText = searchInput.getAttribute("value");
        Assert.assertEquals(enteredText, textToType, "The search input should contain the typed text.");
    }

    @AfterClass
    public void tearDown() {
        if (driver != null) {
            driver.quit();
        }
    }
}

This test script uses Selenium WebDriver with Java and TestNG for assertions.

Setup: We open the browser and navigate to the homepage URL.

Wait: We use an explicit wait to ensure the search input field with id 'search-input' is clickable (visible and enabled).

Action: We click the input to focus and then type the text 'Selenium testing' using sendKeys.

Assertion: We get the value attribute of the input and assert it matches the typed text exactly.

Teardown: We close the browser after the test.

This approach ensures the element is ready before typing and verifies the input content correctly.

Common Mistakes - 4 Pitfalls
Using Thread.sleep() instead of explicit waits
Using incorrect locator like XPath with absolute path
{'mistake': 'Not verifying the input value after typing', 'why_bad': 'Without verification, the test does not confirm if typing actually worked.', 'correct_approach': "Assert the input field's value attribute matches the expected text."}
Not clicking the input field before typing
Bonus Challenge

Now add data-driven testing with 3 different input texts to verify typing works for each.

Show Hint