Search code examples
jqueryseleniumspockgeb

GEB not finding elements


i am very fresh to GEB and i am trying to create a very simple test case using spock and selenium.

The application is a single page application using OJET

HTML CODE

<div class="oj-hybrid-padding">
    <div id="dashboardTitleDiv">
        <h1>Dashboard Content Area</h1>
    </div>
    .....
</div>

GEB Page

class ISSDashboardPage extends Page {
    static url = "?root=dashboard"
    static at = { $("div#dashboardTitleDiv>h1").text() == "Dashboard Content Area" }
}

The Groovy Test

class NavigateToWorkspaceSpec extends GebSpec {
    def "can navigate to Workspace"() {
        when: to ISSDashboardPage
        then: waitFor(25){at ISSDashboardPage}
    }
}

For some reason that i dont understand, the selector on the Page "at" is not working. This is the error i am getting:

Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 10.763 sec <<< FAILURE! - in NavigateToWorkspaceSpec
can navigate to Workspace(NavigateToWorkspaceSpec)  Time elapsed: 10.39 sec  <<< FAILURE!
org.codehaus.groovy.runtime.powerassert.PowerAssertionError:
$("div#dashboardTitleDiv>h1").text() == "Dashboard Content Area"
|                             |      |
[]                            null   false
        at NavigateToWorkspaceSpec.can navigate to Workspace(NavigateToWorkspaceSpec.groovy:8)


Results :

Failed tests:
  NavigateToWorkspaceSpec.can navigate to Workspace:8->GebSpec.methodMissing:56 $("div#dashboardTitleDiv>h1").text() == "Dashboard Content Area"
|                             |      |
[]                            null   false

Tests run: 1, Failures: 1, Errors: 0, Skipped: 0

if the selector is just "$(h1).text() it finds the text of the first "h1" element

$("h1").text() == "Dashboard Content Area"
|       |      |
|       |      false
|       Inside Sales

Can anyone spot what is wrong? I notice that partial part of the page was not yet rendered (actually i can't see yet the i am expecting to see). But i was expecting that the waitFor on the test would waint at least 25 seconds till that selector to be resolved.

Thank you very much.

Best Regards


Solution

  • Make sure

    • the at checker is not being cached,
    • the element is optional and
    • just integrate the waiting right into the page element in order to avoid the waitFor in your test. (But hey, 25 seconds is really long. Is your page really so slow?)
    static at = { dynamicH1.text() == "Dashboard Content Area" }
    
    static content = {
      dynamicH1(required: false, wait: 25, cache: false) { $("div#dashboardTitleDiv>h1") }
    }