How do I check if Geb Module "content" is present?

geb
Zach Lysobey picture Zach Lysobey · Dec 19, 2013 · Viewed 8.6k times · Source

I'm a bit new to the whole Selenium/Geb thing, so I'm probably going about this a bit wrong, but I'm trying to get the exists() method in the following code to work properly.

class Question extends Module {
    static base = { $("fieldset") }
    static content = {
        input { $("input[type=text]") }
        name { input.getAttribute("name").toString() }
    }
    boolean exists() {
        return input.isPresent()
    }

Frustratingly, when I try to execute that code (from a Spock Test, "go"ing to a PageObjectm including this module, I get the following:

The required page content 'input - SimplePageContent (owner: question - Question (owner: MyPage, args: [], value: null), args: [], value: null)' is not present

I've tried a number of other things, including:

  • if (input) return true; return false,
  • ... input.size() == 0,
  • Using static at = {...} (doesn't seem to be supported for modules"

Any ideas

Answer

erdi picture erdi · Dec 20, 2013

By default Geb ensures that your content definitions return non empty elements. If your content is optional you can tell it using required content option:

name(required: false) { input.getAttribute("name").toString() }

Because Geb utilizes Groovy Truth to redefine how navigator are coerced to boolean values(empty navigators are falsey and non-empty are truthy) you can simplify your exists method to:

boolean exists() {
    input
}