Appium : Clear a field

后端 未结 26 3188
野趣味
野趣味 2020-12-16 16:03

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:

26条回答
  •  不知归路
    2020-12-16 16:55

    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 tapObject = new HashMap();
        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.

提交回复
热议问题