appiumselenide

How to use if-else statements in Appium Test (Java)


I want to use some construction like this in Appium in my Tests:

if (element.exists()) {
 System.out.println("OK");
}

But Test failed with NoSuchElementException. Thy/catch constructiont doesnt work too. How can I use if/else statements in Appium?


Solution

  • You can check if the element exists or not by first fetching the list of that element and then checking its size. If the size is greater than 0 it means it is present on the page else it is not present.
    You can do it like:

    List<WebElement> elementList = driver.findElements(By.xpath("Enter your xpath here"));
    if(elementList.size()>0){
       //Element is present
    }
    else{
       //Element is not present  
    }