Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Appium : Clear a field

Tags:

appium

I have my login screen in app now. Each time the app is launched in screen the mobile number is pre filled with the older text.

I just want to know I have tried:

WebElement mob = driver.findElement(By.name("Mobile Number"));
mob.clear // Not working

I have tried :

String Mobile
mob="";   

but still it cannot delete the pre filled text.

I am trying to automate my android app using appium, please help me with this.

like image 799
user1664899 Avatar asked Mar 27 '14 06:03

user1664899


3 Answers

I had trouble with this too. A main issue I found was that in order to delete the area by pressing the delete key was that it needed to tap at the end of the line. This works for me:

public void clearTextField(WebElement element) {
    double x = element.getLocation().getX() + element.getSize().width - 5;
    double y = element.getLocation().getY() + ((double) element.getSize().height / 3);
    preciseTap(x, y, 0.1, 1);
    while (!element.getText().isEmpty()) {
        pressDeleteKey();
    }
}

public void preciseTap(double x, double y, double duration, int touchCount) {
    JavascriptExecutor js = (JavascriptExecutor) driver;
    HashMap<String, Double> tapObject = new HashMap<String, Double>();
    tapObject.put("x", x);
    tapObject.put("y", y);
    tapObject.put("touchCount", (double)touchCount);
    tapObject.put("duration", duration);
    js.executeScript("mobile: tap", tapObject);
}

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

It is a lot slower than just clearing it all out but I have not figured out how to do this yet. Would be ideal to double tap or tap and hold until everything is selected.

like image 121
plosco Avatar answered Nov 13 '22 01:11

plosco


A click on to the textBox before you clear should do with the latest libs:

WebElement mob = driver.findElement(By.name("Mobile Number"));
mob.click();
mob.clear();

OR from the sample jUnitTest here

WebElement text = driver.findElement(By.xpath("//UIATextField[1]"));
text.sendKeys("12");
text.clear();

I guess

like image 7
Naman Avatar answered Nov 13 '22 00:11

Naman


It is definitely not efficient, could be improved and there's probably a better way... But, using adb's shell input key event codes I simply called "dpad right" to move the cursor all the way to the right. Once there, send the key code "DEL" to start deleting all the way back... So... two for-loops. This was mainly used for short texts:

public void cleatTextFully(WebElement element) {
    int stringLength = element.getText().length();

    for (int i = 0; i < stringLength; i++) {
        mDriver.sendKeyEvent(22); // "KEYCODE_DPAD_RIGHT"
    }

    for (int i = 0; i < stringLength; i++) {
        mDriver.sendKeyEvent(67); // "KEYCODE_DEL"
    }
}

mDriver is the AppiumDriver instance. Hope this helps some what.

like image 6
Alon Minski Avatar answered Nov 13 '22 01:11

Alon Minski