如何使用Ruby中的“page-object”gem测试page_url是否正确

时间:2015-01-03 06:52:41

标签: ruby watir-webdriver page-object-gem

以下是我的小黄瓜的一个例子:

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

以下是我的网页定义示例:

require 'page-object'

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

基本上,我想测试用户实际上已被重定向到示例新闻站点。代码运行顺利,但我的测试用例存在问题。我尝试过page_url.inlcude?page_url.value?

1 个答案:

答案 0 :(得分:1)

页面对象有两个与获取URL相关的方法:

  1. page_url_value - 这是page_url访问者中指定的网址(即预期的网址)。
  2. current_url - 这是浏览器的当前网址。
  3. 要检查是否显示了正确的页面,您需要检查current_url。步骤将是:

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

    但是,此步骤将始终通过。我假设你真的想做一个断言:

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

    您也可以使用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