我一直在努力开始使用Cucumber和Watir。我已完成安装并编写/复制了一个简单的“功能”。但是,我无法检查页面上的内容。
这个我的功能:
功能:搜索Google 为了确保人们可以找到我的文档 我想检查它是否列在Google的第一页
Scenario: Searching for JS.Class docs
Given I have opened "http://www.google.com/"
When I search for "JS.Class"
Then I should see a link to "http://jsclass.jcoglan.com/" with text "JS.Class v3.0"
我的env.rb文件看起来像这样
require 'watir'
require 'rspec'
这是我的search_steps.rb
@ie
Given /^I have opened "([^\"]*)"$/ do |url|
@ie = Watir::IE.new
@ie.goto(url)
end
When /^I search for "([^\"]*)"$/ do |arg1|
@ie.text_field(:name, "q").set(arg1)
@ie.button(:name, "btnG").click
end
Then /^I should see a link to "([^\"]*)" with text "([^\"]*)"$/ do |arg1, text|
puts arg1
# @ie.text.should include(arg1)
@ie.close
end
当我取消注释'@ie.text.should include(arg1)'时,我会抛出此错误。
Then I should see a link to "http://jsclass.jcoglan.com/" with text "JS.Class v3.0" # features/step_definitions/search_steps.rb:13
true
undefined method `split' for ["http://jsclass.jcoglan.com/"]:Array (NoMethodError)
./features/step_definitions/search_steps.rb:17:in `/^I should see a link to "([^\"]*)" with text "([^\"]*)"$/'
features\search.feature:8:in `Then I should see a link to "http://jsclass.jcoglan.com/" with text "JS.Class v3.0"'
失败的场景: cucumber features \ search.feature:5#Scenario:搜索JS.Class文档
现在,Then函数没有完成它的工作。如果我注释掉这一行,那么它所做的一切就是写出一些文字。
我所拥有的宝石包括以下黄瓜< 1.1.9>,rspec< 2.9.0>,watir< 2.0.4>和watir-webdriver< 0.5.3>
我拥有的ruby版本是:ruby 1.9.3p125。
我在Windows 7 Ultimate上运行它。
我想知道我是否错过了宝石或其他东西。阅读消息意味着我正在调用一个找不到的方法,但我在网上发现了很多使用这种方法的页面。
非常欢迎任何正确方向的帮助,指导或指示。
答案 0 :(得分:2)
问题在于,如果您(手动)查看Google搜索结果的文本,您会注意到没有文本“http://jsclass.jcoglan.com/”。因此,正如预期的那样,测试将失败。
要解决此问题,您可以通过删除“http://”来纠正对该步骤的调用:
Then I should see a link to "jsclass.jcoglan.com/" with text "JS.Class v3.0"
也就是说,如果您使用以下代码实际检查链接,它会更强大。检查文本是否在页面上的某个位置可能会导致误报(请参阅WatirMelon上的最新帖子)
@ie.link(:url => arg1, :text => text).exists?.should be true