TestProject Forum
Powered by leading experts in the test automation community

Clear text field using appium

On the main screen of my app one of my text fields is already populated with text from previous run.

I tried running :

WebElement nameField = driver.findElement(By.name("Name")); nameField.clear nameField = " "

But both options are not working.

The text won’t go away.

Any help would be appreciated

I also struggled with this issue. One thing i found was that if I wanted to delete text area by pressing the delete key, it needed to tap at the end of the line.

[code]public void clearText(WebElement webElement) {
double xLocation = webElement.getLocation().getX() + webElement.getSize().width - 5;
double yLocation = webElement.getLocation().getY() + ((double) webElement.getSize().height / 3);
tap(xLocation, yLocation, 0.1, 1);
while (!webElement.getText().isEmpty()) {
pressDelete();
}
}

public void pressDelete() {
HashMap swipeObject = new HashMap();
swipeObject.put(“keycode”, 67);
((JavascriptExecutor) driver).executeScript(“mobile: keyevent”, swipeObject);
}

public void tap(double xLocation, double y, double duration, int touchCount) {
JavascriptExecutor js = (JavascriptExecutor) driver;
HashMap<String, Double> tap = new HashMap<String, Double>();
tap.put(“x”, xLocation);
tap.put(“y”, yLocation);
tap.put(“touchCount”, (double)touchCount);
tap.put(“duration”, duration);
js.executeScript(“mobile: tap”, tap);
}[/code]

Hope this helps.