1

I am working through a segment of a Geb test for a Grails app, and can't find this in the docs (though I might be overlooking something). How do you select and click on the link of a controller action? Here's the form element with that has a controller action link (I had to rename some things...)

Is it the problem of the elementId or do I need to look elsewhere? Normally I can hit Ids with the # selector just fine. Had a hard time finding an example in the docs. Any help or direction is greatly appreciated.

(Using Grails 2.4.3, Geb 9.3, and Firefox 37.0.2)

Form item:

    <li id="formItemID">
        <g:link elementId="linkId" controller="controller" action="linkAction">Link Text HERE</g:link>
    </li>

Geb Test:

def "test controller action link"(){
    when:
    go linkURL here 
    $('#linkId').click()  // this won't hit
    then:
    $('.some kind of panel-heading').value('something value...')

}
ry1633
  • 453
  • 1
  • 10
  • 24

1 Answers1

1

Take a look at the rendered HTML in firebug (or chrome dev console, or whatever).

What gets rendered?

<li id="formItemID">
    <a id="linkId" href="http://localhost:8080/controller/linkAction">Link Text HERE</g:link>
</li>

If it looks like this, then you should be able to click the link using the code snippet you've supplied.

$('#linkId').click()

Alternatively, you could try some of these:

$("a", id: "linkId").click()
$("li", id: "formItemID").find("a")[0].click()

Normally, I create links like this:

<a id="myId" href="${createLink(controller: 'myController', action: 'myAction')}">link text</a>

So I'm more certain of what the rendered HTML output will be.

EDIT:

Some tips for creating geb tests

  • Use the debugger in GGTS. Put a breakpoint in your test method and try out different ways to reference parts of the DOM using the Display View.
  • When you figure out how to interact with a particular UI component (like a JQGrid table, Select2 dropdown, etc), encapsulate this functionality in a utility method
  • For sequential flows like new user registration, password reset etc, again encapsulte these in utility methods. You'll really cut down on bloat in your tests, and if the registration (or whatever) process changes, you'll only need to change your tests in one place.
  • Try to avoid using Thread.sleep(), it's a one way ticket to the world of flaky tests. If you need to wait for some ajax to finish, create a utility method that waits for the loading spinner to disappear, and call this from your tests.
Community
  • 1
  • 1
rcgeorge23
  • 3,594
  • 4
  • 29
  • 54
  • If you put a breakpoint in your IDE and evaluate `$("#linkId")`, what do you get? Are you sure the page has completely finished loading? What error do you get when you invoke `.click()`? – rcgeorge23 May 13 '15 at 16:00
  • this one worked: $("li", id: "formItemID").find("a")[0].click() - not sure why the other ones did or didn't. Is there a way to step through each Geb selector. Sometime I've tried a waitFor(x seconds). – ry1633 May 13 '15 at 16:37
  • Not sure what you mean, 'step through each geb selector'? You can certainly iterate over collections of selectors, e.g. `$(".someSelector").each { ... }` – rcgeorge23 May 14 '15 at 06:32
  • no what I meant - is there a way to see each selector one at a time or a way to slow the test down to watch each selector to see if it gets filled. As it stands right now, the test runs too fast for me to double-check each one. – ry1633 May 14 '15 at 13:23
  • Edited my answer - I've added a few tips based on 2 years of fighting with geb ;-) – rcgeorge23 May 14 '15 at 13:55