Clear text field using appium

  • 1 Replies
  • 1724 Views
Clear text field using appium
« on: March 08, 2016, 19:45:04 pm »
On the main screen of my app one of my text fields is already populated with text from previous run.

I tried running :

Code
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
« Last Edit: March 09, 2016, 10:40:05 am by Sebastian »

*

Oren

  • **
  • 59
Re: Clear text field using appium
« Reply #1 on: March 09, 2016, 11:07:30 am »
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);
}

Hope this helps.

 

Sitemap 1 2 3 4 5 6 7 8