0
0
Selenium Javatesting~15 mins

Clearing fields in Selenium Java - Build an Automation Script

Choose your learning style9 modes available
Clear text from input fields on a form
Preconditions (1)
Step 1: Locate the input field with id 'username' and enter the text 'testuser'
Step 2: Locate the input field with id 'email' and enter the text 'testuser@example.com'
Step 3: Clear the text from the 'username' input field
Step 4: Clear the text from the 'email' input field
✅ Expected Result: Both 'username' and 'email' input fields are empty after clearing
Automation Requirements - Selenium WebDriver with Java
Assertions Needed:
Verify 'username' input field is empty after clearing
Verify 'email' input field is empty after clearing
Best Practices:
Use explicit waits to ensure fields are interactable before actions
Use By.id locator strategy for input fields
Use clear() method to clear input fields
Use assertions from a testing framework like TestNG or JUnit
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 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.

Common Mistakes - 4 Pitfalls
Using Thread.sleep() instead of explicit waits
Using incorrect locator strategies like XPath with absolute paths
{'mistake': 'Not verifying the field is empty after clearing', 'why_bad': 'Without assertions, the test does not confirm the clear action succeeded.', 'correct_approach': "Add assertions to check the input field's value attribute is empty."}
Calling clear() before the element is interactable
Bonus Challenge

Now add data-driven testing with 3 different sets of input values for username and email, then clear and verify each.

Show Hint