Search code examples
androidtestingautomated-testsappiumui-automation

Appium Program crash on isDisplayed


I am trying to check the visibility of a hidden element in my UI. When the element is on the screen, isDisplayed returns true. Else, When the element is not visible on the screen and is hidden, my Program crashes on isDisplayed.

Assert.assertTrue(driver.findElement(By.id("com.appiumdemoapp:id/btnInvisible")).isDisplayed());

Solution

  • It's failing because the element cannot be found, so you need to pre-condition the assert by first checking to see if the element exists:

    if (driver.findElements(By.id("com.appiumdemoapp:id/btnInvisible")).size() = 0)
     Assert.fail("Element not found");
    else
     Assert.assertTrue(driver.findElement(By.id("com.appiumdemoapp:id/btnInvisible")).isDisplayed());