Custom swipe for iOS objects with poor identifiers

Sometimes when we want to scroll to an item it is impossible to use the regular swipe functions due to poor properties of the object that needs to be found, like lacking accessibility id. To work around this we create a custom swipe function that will repeatedly swipe until the object is visible on the screen of the device and is identifiable by other property, like 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 3 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);