Clear text from input fields on a form
Preconditions (1)
✅ Expected Result: Both 'username' and 'email' input fields are 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.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 ClearFieldsTest { private WebDriver driver; private WebDriverWait wait; @BeforeClass public void setUp() { driver = new ChromeDriver(); wait = new WebDriverWait(driver, Duration.ofSeconds(10)); driver.get("https://example.com/formpage"); } @Test public void testClearInputFields() { // Locate username field and enter text WebElement usernameField = wait.until(ExpectedConditions.elementToBeClickable(By.id("username"))); usernameField.sendKeys("testuser"); // Locate email field and enter text WebElement emailField = wait.until(ExpectedConditions.elementToBeClickable(By.id("email"))); emailField.sendKeys("testuser@example.com"); // Clear username field usernameField.clear(); // Clear email field emailField.clear(); // Assert username field is empty Assert.assertEquals(usernameField.getAttribute("value"), "", "Username field should be empty after clearing"); // Assert email field is empty Assert.assertEquals(emailField.getAttribute("value"), "", "Email field should be empty after clearing"); } @AfterClass public void tearDown() { if (driver != null) { driver.quit(); } } }
This test uses Selenium WebDriver with Java and TestNG for assertions.
In setUp(), we open the browser and navigate to the form page.
In the test method, we wait until the input fields are clickable to ensure they are ready for interaction.
We enter text into the username and email fields, then clear them using the clear() method.
Assertions check that the fields are empty by verifying their value attribute is an empty string.
Finally, tearDown() closes the browser to clean up.
Now add data-driven testing with 3 different sets of input values for username and email, then clear and verify each.
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