Search code examples
seleniumgeb

Assert an element is not present in the dom using GEB


I am trying to verify a page based on who logs in. Certain users have more security and will see more items on the page. When a user with lower security logs in, they will not see options. I need to verify those options are not present on the page.

I have a page object for the option that displays based on user security

testObject {$("#test")}

I have tried using isDisplayed()

boolean hidden =  testObject.isDisplayed()
assert !hidden

But I keep getting an error that says

geb.error.RequiredPageContentNotPresent: The required page content geb.navigator.EmptyNavigator' is not present

The error message is what I want to verify. The object is not present, and I need to verify that is true and pass the test.


Solution

  • You want to use the required option for your content element.

    If the page is dynamic, maybe you also want to wait for a while before you let Geb decide that the element is empty. Checks for non-existence of elements can be tricky, because they could just pass because a dynamic element has not finished loading yet.

    testObject(required: false, wait: 2) { $("#test") }
    

    In your Geb test you just do this (no helper method needed):

    given:
    def page = to MyPage
    
    expect:
    page.testObject.empty