以下是我的小黄瓜的一个例子:
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?
。
答案 0 :(得分:1)
页面对象有两个与获取URL相关的方法:
page_url_value
- 这是page_url
访问者中指定的网址(即预期的网址)。current_url
- 这是浏览器的当前网址。要检查是否显示了正确的页面,您需要检查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