Home > Mobile >  Is there an easy way to assert that an element is NOT present?
Is there an easy way to assert that an element is NOT present?

Time:12-08

I have tried a few different things but I get an element not found exception each time... Which means that the test should pass but I'm not sure how to capitalize on that. I've tried:

public void AssertDeleteLogBookCategoryButtonIsNotPresent()
{
    Assert.Throws<Exception>(() => _elements.deleteLogBookCategoryButton.Click());
}

and also:

public void AssertDeleteLogBookCategoryButtonIsNotPresent()
{
    Assert.That(!_elements.deleteLogBookCategoryButton.Displayed);
}

...but both throw the exception while it is finding the element that it shouldn't find. Advice please!

CodePudding user response:

You can apply the assertion on Driver.FindElements().Any().
This will not throw exception because FindElements() method returns a list of elements matching the passed locator. So, in case of no match (element not found) this will return an empty list so that the entire expression will return Boolean False while in case of presence of such element the result will be Boolean True.

  • Related