0
0
Selenium Javatesting~10 mins

Typing text (sendKeys) in Selenium Java - Test Execution Trace

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

Test Code - JUnit with Selenium WebDriver
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.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();
    }
}
Execution Trace - 6 Steps
StepActionSystem StateAssertionResult
1Test starts and Chrome browser opensChrome browser window is open and ready-PASS
2Navigates to URL https://example.com/searchBrowser displays the search page with input box having id 'search-box'-PASS
3Finds the input element by id 'search-box'Input element is located and ready for interaction-PASS
4Types text 'Selenium testing' into the input box using sendKeysInput box now contains the text 'Selenium testing'-PASS
5Retrieves the value attribute of the input boxValue attribute is 'Selenium testing'Assert that the input box value equals 'Selenium testing'PASS
6Test ends and browser closesBrowser window is closed-PASS
Failure Scenario
Failing Condition: The input element with id 'search-box' is not found or the typed text does not match the expected value
Execution Trace Quiz - 3 Questions
Test your understanding
What does the sendKeys method do in this test?
AClears the input box
BClicks the input box
CTypes the specified text into the input box
DSubmits the form
Key Result
Always verify that the text you typed into an input box matches the expected value by checking the element's 'value' attribute after using sendKeys.