30
Jul

Generic function to clear the text box before entering values via Selenium WebDriver

Following library function can be used to clear the text set in any “Text field” before setting new value to it

public static void clearAndSet(WebElement element, String value) {

if ((element == null) || (StringUtils.isBlank(value))){
return;
}
element.clear();
element.sendKeys(value);
System.out.println(“Entered Value: {} “, value);
}

@Test
public void TestNGFunction()
{
WebDriver driver;
driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.manage().timeouts().pageLoadTimeout(20, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get(“https://www.google.co.in”);

WebDriverWait wait = new WebDriverWait(driver, 20);

By addItem = By.name(“q”);
WebElement element = wait.until(ExpectedConditions.presenceOfElementLocated(addItem));
clearAndSet(element, “Hello google”);

}