Jon Smith
Jon Smith

Reputation: 11

How to test if page_url is correct using the 'page-object' gem in Ruby

Here is an example of my Gherkin:

require 'watir-webdriver'
require 'page-object'

include PageObject::PageFactory

Given(/^user is on google home page$/) do
  visit_page GoogleHome
end

When(/^user enters search criteria in google search bar$/) do
  @browser.text_field(:name, "q").set "latest news"
end

Then(/^user is redirected to results page$/) do
  on_page ResultPage do |page|
    page.text.include? page.WEB_STRING
  end
end

Given(/^user is on result page$/) do
  visit_page ResultPage
end

When(/^user clicks on website link$/) do
  on_page ResultPage do |page|
    page.news_link
  end
end

Then(/^user is redirected to website$/) do
  on_page FoxNews do |page|
    page_url.value?("http://www.foxnews.com/")
  end
end

Here is an example of my page definition:

require 'page-object'

class FoxNews
  include PageObject
  page_url "http://www.foxnews.com/"
end

Basically, I want to test that the user has actually been redirected to the example news site. The code runs smoothly but there is a problem with my test case. I have tried page_url.inlcude? and page_url.value?.

Upvotes: 1

Views: 791

Answers (1)

Justin Ko
Justin Ko

Reputation: 46846

A page object has two methods related to getting the URL:

  1. page_url_value - This is the URL specified in the page_url accessor (ie the expected URL).
  2. current_url - This is the browser's current URL.

To check that the correct page is displayed, then you want to check the current_url. The step would be:

Then(/^user is redirected to website$/) do
  on_page FoxNews do |page|
    page.current_url == 'http://www.foxnews.com/'
  end
end

However, this step will always pass. I assume that you actually want to make an assertion:

Then(/^user is redirected to website$/) do
  on_page FoxNews do |page|
    expect(page.current_url).to eq('http://www.foxnews.com/')
  end
end

Instead of duplicating the expected URL in the page object and the test, you could also use the page_url_value:

Then(/^user is redirected to website$/) do
  on_page FoxNews do |page|
    expect(page.current_url).to eq(page.page_url_value)
  end
end

Upvotes: 1

Related Questions