用Cucumber和Watir检查值 [英] Checking values with Cucumber and Watir

查看:391
本文介绍了用Cucumber和Watir检查值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试着开始使用Cucumber和Watir。我做了安装和写/复制一个简单的功能。



这是我的功能:



功能:搜索Google
为了确保人们可以找到我的文档
我想检查它是否列在Google的第一页上

 场景:搜索JS.Class docs 
给定我已经打开http://www.google.com/
当我搜索JS.Class
然后我会看到一个链接到http://jsclass.jcoglan.com/,文本为JS.Class v3.0

我的env.rb文件看起来像这样

  require'watir'
require'rspec'

这是我的search_steps.rb

  @ie 

给定/ ^我已打开([^ \] *)$ / do | url | $ b / b我在搜索([^ \] *)时,会出现以下情况:$ b @ie = Watir :: IE.new
@ ie.goto(url)
end

)$ / do | arg1 |
@ ie.text_field(:name,q)。set(arg1)
@ ie.button(:name,btnG)。
end

然后/ ^我应该看到一个链接到([^ \] *)与文本([^ \] *)$ / do | arg1,text |
puts arg1

#@ ie.text.should include(arg1)
@ ie.close
end

当我取消注释行'@ie.text.should include(arg1)'时,我得到这个错误。

 然后我会看到一个链接到http://jsclass.jcoglan.com/,文本为JS.Class v3.0#features / step_definitions / search_steps。 rb:13 
true
未定义的方法`split'for [http://jsclass.jcoglan.com/\"]:Array(NoMethodError)
./features/step_definitions/search_steps.rb :17:in`/ ^我应该看到一个链接到([^ \] *)与文本([^ \] *)$ /'

features\\ \\ search.feature:8:in`然后我应该看到一个链接到http://jsclass.jcoglan.com/文本JS.Class v3.0'

失败的场景:
cucumber features\search.feature:5#场景:搜索JS.Class文档



现在,Then函数不能完成它的工作。如果我注释掉那行,那么它真正做的是写出一些文本。



我已经包括以下黄瓜< 1.1.9>,rspec< 2.9.0>,watir <2.0.4>和watir-webdriver <0.5.3>



ruby​​的版本是:ruby 1.9.3p125。 / p>

我在Windows 7旗舰版上运行这个程序。



我想知道我是否缺少一个gem或者其他的东西。阅读消息意味着我调用的方法,它找不到,但我发现了相当多的网页上使用这种方法。



任何帮助

解决方案

问题是,如果你(手动)看看文本的Google搜索结果,您会注意到没有文本http://jsclass.jcoglan.com/。



要解决这个问题,您可以通过删除http://来更正对该步骤的调用:

 然后我会看到一个链接到jsclass.jcoglan.com/,文本为JS.Class v3.0

也就是说,如果你实际使用下面的代码检查链接,它会更健壮。检查文本是否在页面上的某个位置可能会导致误报(请参阅 WatirMelon

  @ ie.link(:url => arg1,:text => text).exists?.should be true 


I've been trying to get started with Cucumber and Watir. I've done the installation and written/copied a simple 'feature'. However, I'm not getting anywhere with checking what's on a page.

This my feature:

Feature: Search Google In order to make sure people can find my documentation I want to check it is listed on the first page in 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"

My env.rb file looks like this

require 'watir'
require 'rspec'

And this is my 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

When I uncomment the line '@ie.text.should include(arg1)' I get this error thrown.

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"'

Failing Scenarios: cucumber features\search.feature:5 # Scenario: Searching for JS.Class docs

Right now the Then function isn't doing its job. If I comment out the line then all it's really doing is writing out some text.

The gems I have include the following cucumber <1.1.9>, rspec <2.9.0>, watir<2.0.4> and watir-webdriver <0.5.3>

The version of ruby I have is: ruby 1.9.3p125.

I'm running this on Windows 7 Ultimate.

I'm wondering if I'm missing a gem or something. Reading the message implies that I'm calling a method it can't find but I've found quite a few pages on the web that use this method.

Any help, guidance or pointers in the right direction are gratefully welcomed.

解决方案

The problem is that if you (manually) look at the text of the Google search results, you will noticed that there is no text "http://jsclass.jcoglan.com/". So, as expected, the test will fail.

To fix this, you can correct the call to the step by removing the 'http://':

Then I should see a link to "jsclass.jcoglan.com/" with text "JS.Class v3.0"

That said, it would be more robust if you actually make check for the link using the following code. Checking that the text is somewhere on the page can lead to false positives (see the latest post on WatirMelon)

@ie.link(:url => arg1, :text => text).exists?.should be true

这篇关于用Cucumber和Watir检查值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆