0

I'm working on writing a test automation script for a android app.

I'm using python to write the code and using Appium server to run these tests and using Appium UI inspector to identify elements.

I'm testing on a action and, after performing this action a specific element will no longer be visible on the screen and that is how i will validate the test if its pass/fail

if the element is not present - pass, if element is displayed - fail

is there a way to actually check if an element is not present without trying to find the element (since the script anyway fails when trying to element which is not present in the screen.

mali abey
  • 11
  • 2

2 Answers2

0

You could do a find_elements and then check if the list of WebElements is empty. If it's empty, the element is not present, else it is present.

I don't code in Python, but in Java it would be like this:

if (driver.findElements(By.xpath("yourxpath")).isEmpty()) {
    //Element is not present
} else {
    //Element is present
}

Present is not the same as visible though. For visibility you can add this logic (this is in Python, I'm fairly sure for this one that the syntax is correct).

if element.is_displayed():
  //Element is visible
else:
  //Element is not visible
valies
  • 96
  • 5
0
def is_displayed(self, locator: Type) -> bool:
    is_displayed: bool = False
    count = len(self.driver.find_elements(*locator))
    if count > 0:
        is_displayed = True
    return is_displayed


Then call this method in your test script

assert False == self.driver.is_displayed(<element_locator>)


You can also add this method to the base class