Checking values with Cucumber and Watir

怎甘沉沦 提交于 2019-12-25 08:56:58

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/9980125/checking-values-with-cucumber-and-watir

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!