How to Clear a Pre-filled Text Field in Appium

How to Clear a Pre-filled Text Field in Appium

10 November 2024 Stephan Petzl Leave a comment Tech-Help

When automating mobile applications using Appium, you might encounter situations where text fields are pre-filled with old data. This can pose challenges, especially if the standard clear() method does not perform as expected. Below, we provide a step-by-step guide to effectively clear a text field on your application using Appium.

Solution Overview

The key to successfully clearing a text field in Appium is to ensure that the cursor is engaged with the text field before attempting to clear it. This involves clicking on the text field to focus on it and then using the clear() method. Here’s a method that has been found effective:


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

This approach involves a simple click on the text field before calling the clear() method. This ensures that the field is correctly focused, allowing the clear operation to proceed without issue.

Alternative Method for Complex Scenarios

In cases where the above method does not work, you can opt for a more detailed approach. This involves precise tapping and using JavaScript executor to simulate key events:


    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);
    }
    

This method involves calculating precise tap locations and simulating key events to ensure the text is cleared. While more complex, it provides a robust solution for more challenging scenarios.

Using Repeato for Efficient Test Automation

For those seeking a more efficient and robust testing approach, consider using Repeato, a no-code test automation tool for iOS and Android. Repeato allows you to create, run, and maintain automated tests effortlessly. Its use of computer vision and AI makes test creation and execution significantly faster and more stable compared to traditional tools like Appium. This can be particularly beneficial in scenarios where Appium tests are slow or unstable.

For more information on setting up and using Repeato, visit our documentation pages.

Like this article? there’s more where that came from!