This test opens a login page, types an old email, clears the field, then types a new email. It checks if the new email is correctly set.
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class ClearFieldExample {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
WebDriver driver = new ChromeDriver();
try {
driver.get("https://example.com/login");
WebElement emailField = driver.findElement(By.id("email"));
emailField.sendKeys("oldemail@example.com");
// Clear the field before entering new email
emailField.clear();
emailField.sendKeys("newemail@example.com");
// Verify the field contains the new email
String currentValue = emailField.getAttribute("value");
if ("newemail@example.com".equals(currentValue)) {
System.out.println("Test Passed: Field cleared and new text entered.");
} else {
System.out.println("Test Failed: Field value is incorrect.");
}
} finally {
driver.quit();
}
}
}