paul
paul

Reputation: 4487

"page_url" in WATIR when part of it is changing on run time

I am working on WATIR using page object. Here we need to make page files in .rb example

order_page.rb URL for this page is https://www.test-xyz.com/order/123123 here last digits of this page always changes.

Now how to create a page object for this page file so that I can use:

on(OrderPage).continue_shopping

Upvotes: 0

Views: 411

Answers (2)

Justin Ko
Justin Ko

Reputation: 46836

The page_url accessor method is only needed if using the PageFactory visit method. It is not used when using the on method.

Given that you only want to call the on method, just drop the page_url accessor from the page-object:

require 'watir-webdriver'
require 'page-object'
include PageObject::PageFactory

class OrderPage
  include PageObject

  link(:continue_shopping, :text => 'Continue Shopping')
end

@browser = Watir::Browser.new :chrome
@browser.goto('path\to\page\test.htm')

on(OrderPage).continue_shopping

If you do want to specify the page_url so that you can visit it, you can specify a dynamic URL that is evaluated by ERB. For example, you would define the page-object as:

class OrderPage
  include PageObject
  page_url "https://www.test-xyz.com/order/<%=params[:id]%>"

  link(:continue_shopping, :text => 'Continue Shopping')
end

With this page-object, you can still call the on method as before:

on(OrderPage).continue_shopping

This also gives you the ability to directly access the page when know the dynamic portion of the URL:

visit OrderPage, :using_params => {:id => '123123'} do |page|
  page.continue_shopping
end

There are some more details/tricks for specifying a dynamic URL on my blog post, "Creating page objects with a dynamic page_url".

Upvotes: 2

alannichols
alannichols

Reputation: 1506

You don't have to use the page url when using the page object gem. It is only relevant if you have to navigate directly to that page. If you are already on the page then you can just execute your method.

If you do need to navigate to that page, and don't know the url until runtime you can define a method that takes the part of the url that you don't know until runtime and navigates to the page.

def navigate_to_order_page(order_number)
  @browser.goto("https://www.test-xyz.com/order/#{order_number}")
end

Then just call that method when you want to go to the page.

Upvotes: 0

Related Questions