0
0
Selenium Javatesting~10 mins

Clearing fields in Selenium Java - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test opens a web page with a text input field, enters some text, then clears the field. It verifies that the field is empty after clearing.

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.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 ClearFieldTest {
    WebDriver driver;

    @BeforeEach
    public void setUp() {
        driver = new ChromeDriver();
    }

    @Test
    public void testClearInputField() {
        driver.get("https://example.com/form");
        WebElement inputField = driver.findElement(By.id("username"));
        inputField.sendKeys("testuser");
        inputField.clear();
        String fieldValue = inputField.getAttribute("value");
        assertEquals("", fieldValue, "Input field should be empty after clearing");
    }

    @AfterEach
    public void tearDown() {
        driver.quit();
    }
}
Execution Trace - 8 Steps
StepActionSystem StateAssertionResult
1Test startsTest framework initializes ChromeDriver-PASS
2Browser opens ChromeChrome browser window is open and ready-PASS
3Navigates to https://example.com/formPage with input field having id 'username' is loaded-PASS
4Finds input field by id 'username'Input field element is located on the page-PASS
5Enters text 'testuser' into input field using sendKeysInput field now contains text 'testuser'-PASS
6Clears the input field using clear() methodInput field is now empty-PASS
7Gets the value attribute of input fieldRetrieved value is '' (empty string)Assert that input field value equals ''PASS
8Test ends and browser closesBrowser window is closed, WebDriver quits-PASS
Failure Scenario
Failing Condition: Input field is not found or clear() does not empty the field
Execution Trace Quiz - 3 Questions
Test your understanding
What Selenium method is used to empty the text from the input field?
AsendKeys("")
Bclear()
Cdelete()
Dremove()
Key Result
Always verify the locator used to find elements is correct and that the clear() method empties the field before asserting its value.