Dynamic page URL
-
06-06-2021 - |
Question
I have a page with URL that is dynamic. Let's call it view post page. URL for post 1 is site.com/post/1
and for post 2 is site.com/post/2
.
This is what I do at the moment to check if I am at the right page.
The page:
class ViewPostPage
include PageObject
def self.url
"site.com/post/"
end
end
Cucumber step:
on(ViewPostPage) do |page|
@browser.url.should == "#{page.class.url}#{@id}"
end
Is there a better way? Do you even bother checking the entire URL, or just the site.com/post/
part?
I am using the latest page-object gem (0.6.6).
Update
Even bigger problem is going directly to the page that has dynamic URL.
The page:
class ViewPostPage
include PageObject
def self.url
"site.com/post/"
end
page_url url
end
Cucumber step:
visit ViewPostPage
What I do now is to change the Cucumber step to:
@browser.goto "#{ViewPostPage.url}#{@id}"
It would be great if there was a way for the page to know it's ID, but I have not figured out yet how to do it.
Solution
You can get the url for the page using the method current_url. On your test above are you trying to determine if you are on the correct page? If that is the case I might suggest using one of the two "expected" methods - expected_title and expected_element.
The page_url method is more than likely not the choice for you if you need to navigate to a url dynamically. What you might try instead is add a method to your page that does something like this:
class ViewPostPage
include PageObject
URL = "site.com/post/"
expected_title "The expected title"
def navigate_to_post_with_id(id)
navigate_to "#{URL}/#{id}"
end
end
And in your test
on_page(ViewPostPage) do |page|
page.navigate_to_post_with_id @id
page.should have_expected_title
end
Let me know if this helps.
OTHER TIPS
There is an option to check dynamic URL in the Page Object gem.
Use the below code:
class ViewPostPage
include PageObject expected_url "The expected URL" def initialize has_expected_url? end
end
It'll help you
As far as I know, #page_url
is for opening corresponding page along with page object initialization. To verify you're on correct page, you can try to use #expected_title
method.
Also, maybe it'll be useful for you. When symbol is passed to #page_url
, it calls corresponding method, so'd better use it. I haven't tried it, but here are few links for you.
- Original issue on GitHub
- Specs
- Documentation of #page_url