3

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!

dshgna
  • 812
  • 1
  • 15
  • 34
  • 2
    I think you are failing with Page w/o url, because you try to navigate it with `to`. When you do a `click` you can provide expected Page. See example below. – Seagull Aug 28 '15 at 17:03
  • Thanks. That's where I was going wrong. Instead of using 'to', doing the at check as you had mentioned in your answer worked. – dshgna Aug 28 '15 at 17:19

2 Answers2

3

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()
}
Alex
  • 8,093
  • 6
  • 49
  • 79
Seagull
  • 13,484
  • 2
  • 33
  • 45
  • Thanks! Where can I define the static content for the sub page? I have tried defining in the main page but it does not work. Defining the page without the url does not work either though that method is used in examples. – dshgna Aug 28 '15 at 16:44
  • @dshgna Updated the answer. – Seagull Aug 28 '15 at 17:02
1

Override the method getPageUrl in your Page class.

Go through source code Spec, Page. Notice how getPageUrl is being called.

Alex
  • 8,093
  • 6
  • 49
  • 79
Durga
  • 141
  • 1
  • 4