Best Practice: Testing for "close" action

I am trying to determine the best way to test for an action that results in an element disappearing. The use case is testing that clicking a close button causes a menu to close. There are different approaches I have found online but I am having trouble determining which one is best.

I can use a try/catch and catch the NoSuchElementException thrown when trying to find the element that has now disappeared. I can also use WebDriverWait with ExpectedConditions.InvisibilityOfElementLocated. BUT it seems like there should be a way to properly assert that something doesn’t exist?

I personally like way just check that element not exists.
Algorithm is simple:

  1. mark start time
  2. set boolean variable notFound = false
  3. limit search element for 1 sec or less
  4. in loop until X sec passed from start time
    - check element NOT exist -> notFound = true -> break
    - sleep 1 sec
    - check time X sec from start -> break
  5. restore default search element time
  6. return notFound
1 Like