Custom swipe for iOS objects with poor identifiers

Sometimes, it is not possible to use the regular swipe functions when you need to scroll to an item, for example due to poor properties of the object that needs to be found, such as a missing accessibility ID. To work around this, you can create a custom swipe function that will repeatedly swipe until the object is visible on the screen of the device and is identifiable by another property, for example a label.

Following is the function code.

Copy
public static void scrollDownAndClickText(String text, IOSDriver driver) {
        boolean flag = true;
        int count =1;

        while(flag){
            try {
              Map<String, Object> params = new HashMap<>();
              params.put("label", text);
              params.put("threshold", 80);
              params.put("ignorecase", "nocase");

              driver.executeScript("mobile:button-text:click", params);
                flag = false;
                break;
            } catch (Exception NoSuchElementException) {
                count = count + 1;
                Map<String, Object> params = new HashMap<>();
                params.put("start", "40%,60%");
                params.put("end", "40%,30%");
                params.put("duration", "2");

                Object res = driver.executeScript("mobile:touch:swipe", params);

                if(count==3)
                {
                     break;
                }
            }

        }

This function will scroll down three times before attempting to reach a specific label.

Calling the function in the test body is achieved with the following line:

Copy
scrollDownAndClickText("label to find", driver);