Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

geb

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

like image 760
Zach Lysobey Avatar asked Oct 15 '25 03:10

Zach Lysobey


1 Answers

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
} 
like image 179
erdi Avatar answered Oct 17 '25 00:10

erdi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!