Search code examples
javaappium

How to make condition to find AccessibilityId?


I create some code to find AccessibilityId after swipe action, but it did "not" go into my while condition. How to make condition to find AccessibilityId?

This is scenario I want.

1.Launch app, there is no test1_action buttons (AccessibilityId = test1_action)
2.To Click test1_action, will repeat swipe "i" times till find AccessibilityId.
3.After "i" times swiping, if there will be test1_action button then it will click it

iOS/iPhone/Appium

WebDriverWait wait = new WebDriverWait(driver, 20);
int i = 0;
while 
(!driver.findElement(MobileBy.AccessibilityId("test1_action")).isDisplayed()) {
    swipe(331, 544, 50, 544, 2);
    i++;
        }
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("test1_action"))).click();

When i run this, I faced below error

Caused by: org.openqa.selenium.NoSuchElementException: An element could not be located on the page using the given search parameters.


Solution

  • If the element is not present you will get a NoSuchElementException on attempt to call isDisplayed() function, consider amending your code to look like:

    while (driver.findElements(MobileBy.AccessibilityId("test1_action")).size() == 0 && i < 100) {
        swipe(331, 544, 50, 544, 2);
        i++;
    }
    

    You can also consider using SwipeWhileNotFound method.