Search code examples
geb

How to make geb throw error when element not found instead of returning EmptyNavigator


in my page object I have simple method

def clickSomething(byName) {    
    $("a.name", text: contains(byName)).click()
}

and during execution it does not find required element and goes further.

it happens because, according to documentation, $() returns EmptyNavigator if element not found.

I want for test to fail with some kind of "ElementNotFoundException" or "NullPointerException" on trying to make click on null element.

I also do not want to add additional checks for returned element (because I would need to add that for every element identification).

Is there an elegant workaround for that ?

e.g. for elements declared within "content" there is performed such a check. But what is the best practice for elements found outside content block ?


Solution

  • The issue that you've encountered which is click() not throwing an error when called on en empty navigator has been fixed recently and will be released in the next version of Geb.

    If you need to get an error when a selector results in an empty navigator then you can either: