Search code examples
groovyautomationgeb

Setting page url dynamically in Geb


In this scenario, I want to navigate from the main page to the sub page and perform some actions. The url of the sub page is dynamically generated, but the page content is the same. This is what I have tried so far:

In the main class:

to MainPage
    SubPageButton.click()
    to SubPage
    SelectAddressButton.click()

In the Page class:

public class SubPage extends Page {
    static url = getCurrentUrl()
    static content = {
        SelectAddressButton { $("button", 0) }
    }
}

I have alternatively tried (without any success)

  1. Defining the static content for the sub page ( SelectAddressButton ) in the Page object for the main page.
  2. Defining the sub page without the url

Thanks a lot in advance!


Solution

  • It won't work, as static fields are initialized when the class is loaded.

    However, you can use go "http://my_dynamic_url_string" in your specification and then make your assertions. The at assertion would work here too.

    Also, if your "dynamic" url can be parametrized you should have a look at Advanced Page Navigation.

    Defining a Page without a url static field should work. I've used it a lot. Currently I have it working with Geb '0.10.0' and use it via withNewWindow { link.click(CustomPage) } and it does static at verification for me. Or you can put logic in methods, and call them in closure you pass to withNewWindow.

    For example (untested):

    class CustomPage {
      static content = {
          SelectAddressButton { $("button", 0) }
      }
    
      static at = { "check smth" }
    
      def orSomehowLikeThis() {
        assert "smth"
        SelectAddressButton.click() // accessing content
      }
    }
    
    withNewWindow { link.click(CustomPage) } {
      orSomehowLikeThis()
    }