Talking testing, automation... and anything else.


Customize Page Navigation By Overriding Geb’s getPageUrl()

gebGeb’s page object pattern offers the ability to set a url for a page, that allows you to navigate directly to a page. The url is used when calling the to() method (eg. to myPage).

An application I’m working on includes dynamic elements in its urls; the urls contain a session token, and can include a return page, and/or various content IDs. This makes it essentially impossible to navigate directly to the page. And unless you’re testing app navigation, it’s better to navigate directly. It keeps your code clean and helps mitigate brittleness.

The solution I’ve come up with is to parse the current url and use it to navigate directly to my pages. To do this, I needed to override Geb’s getPageUrl() (thanks to the Geb user group on this!).

BasePage.groovy

In my example above, we override Geb’s getPageUrl with our own getPageUrl, added to a BasePage, that we’ll extend in our page object. In this new getPageUrl, we want to replace the page name in the url (eg. http://localhost/html#/PAGENAME/…). To do this we grab the current url and just replace the current page name with the target page. All the other elements in the url should be correct.

Now we also need to make sure normal pages continue to operate, so we first test to see if pageName property exists in the page. If not, we execute Geb’s normal url code. To get the target pageName, we add it as a static property to the page (instead of url), named pageName. The code would looke something like this:

MyPage.groovy

Now we can simply call to MyPage as you would with a page with a normal url. Not too shabby!

Plusone Linkedin Facebook Twitter Digg Email

2 comments

  1. Really great job.. this solved my issue .. thanks and Great !

  2. Alister Ernest

    Hi,
    We have recently adopted the Geb-Groovy-Spock framework for our automation tests. I am working on a scenario where I am stuck.

    I have Page1 with Link1 on it, clicking on it will launch Page2 which has Link2 on it, clicking Link2 launches Page3. I have to interact with Page3 and then close it, then close Page2 and finally return back to Page1. My logic is structured as below:

    doSomethingOnPage1
    withNewWindow(clickLink1OnPage1) {
    assert Page2
    withNewWindow(clickLink2OnPage2) {
    assert Page3
    doSomethingOnPage3
    }
    }

    The first withNewWindow works, but the nested call to withNewWindow breaks for me. Am I doing something out of order here or is there a better way to do it?

    Any help and guidance will be greatly appreciated.

    Thank You

Leave a Reply to Prashanth Cancel reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.