I have a page (StartPage) with links to several books and I'd like to create a generic method or content definition that clicks on a link with a specific title and navigate to that page. All book pages have a static String TITLE
defined which defines the book's title. What I want to do is something like this (StartPage):
def <T extends Page> T openBook(Class<T> pageType) {
$(".product h3", text: pageType.TITLE).parents("a").click()
browser.to(pageType)
browser.page as T
}
However this doesn't work since when I navigate to the pageType
(using browser.to(pageType)
) it seems like Geb redirects me to the login page instead of the requested pageType
(maybe because when I call to
the session is reset?).
If I refactor the method and hardcode everything into a content definition:
static content = {
openMyBook(to: MyBookPage) { $(".product h3", text: "My Book Title").parents("a")}
}
it works by just calling openMyBook
but as you can see the book page (MyBookPage
) and text
is hardcoded.
So my question is basically if I can pass arguments to the content definition (openMyBook
) or modify the openBook
method to navigate to the book page without lossing the session.
You need to use browser.page(pageType)
here instead of to()
because to()
drives the browser to the url of the page type passed in. You probably didn't specify static url = '...'
field for these pages and the default is the root of your base url which is why you're seeing the browser end up at the login page.
It would be even better if you used browser.at(pageType)
here because then the at checker for your book pages would be verified and at()
also sets the page on the browser after successful verification.