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.
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.
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(); } }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts | Test framework initializes ChromeDriver | — | PASS |
| 2 | Browser opens Chrome | Chrome browser window is open and ready | — | PASS |
| 3 | Navigates to https://example.com/form | Page with input field having id 'username' is loaded | — | PASS |
| 4 | Finds input field by id 'username' | Input field element is located on the page | — | PASS |
| 5 | Enters text 'testuser' into input field using sendKeys | Input field now contains text 'testuser' | — | PASS |
| 6 | Clears the input field using clear() method | Input field is now empty | — | PASS |
| 7 | Gets the value attribute of input field | Retrieved value is '' (empty string) | Assert that input field value equals '' | PASS |
| 8 | Test ends and browser closes | Browser window is closed, WebDriver quits | — | PASS |
clear() method in Selenium WebDriver when working with input fields?clear() in Seleniumclear() method is designed to remove any text or value present in an input field, making it empty.clear() specifically empties the field content.clear() empties input fields [OK]clear() is called on the WebElement returned by findElement().findElement(By.id("username")) with clear(). Other options misuse method calls or syntax.findElement(...).clear() to clear fields [OK]WebElement input = driver.findElement(By.name("email"));
input.sendKeys("user@example.com");
input.clear();
String text = input.getAttribute("value");
System.out.println(text);clear().getAttribute("value") returns an empty string.driver.findElement(By.id("searchBox")).clear;clear method requires parentheses to invoke it as a function: clear().clear is a method, not a field.getAttribute("value"), not getText() or getValue().isEmpty() and then calls clear().Pass the test and this pattern moves unknown