2012-03-17 62 views
0

我正在学习网页和黄瓜,并试图创建一个简单的例子。这是我的特点文件:测试谷歌搜索页面

Scenario: Searching for something 
    Given I have opened "http://www.google.com/" 
    When I search for "some text" 

这是我的步骤定义:

Given /^I have opened "([^\"]*)"$/ do |url| 
    visit url 
end 

When /^I search for "([^\"]*)"$/ do |query| 
    fill_in "q", :with => query 
    click_button "Google Search" 
end 

当我运行测试,我得到错误信息:

找不到场:“Q “(Webrat :: NotFoundError)

如果我会评论'fill_in'行我得到其他错误消息GE:

找不到按钮 “谷歌搜索”(Webrat :: NotFoundError)

我怎样才能解决呢?

回答

2

问题可能在于Webrat未按照www.google.com重定向到您的特定于Google语言环境的本地站点(for details on redirect)。例如,由于我在加拿大,因此转到www.google.com会将我重定向到www.google.ca。因此,当我使用Webrat访问www.google.com时,我看到一个'302 Moved'页面。由于webrat不遵循重定向到搜索页面,因此您无权访问文本字段'q'。

我会尝试save_and_open_page方法来检查你实际上结束了什么页面。您可以运行下面的脚本(然后打开它创建的文件)作为一个快速检查:

require "mechanize" 
require 'webrat' 
include Webrat::Matchers 
include Webrat::Methods 

Webrat.configure {|c| c.mode = :mechanize} 
begin 
    visit('http://www.google.com/') #=> 
    fill_in "q", :with => 'some text' 
    click_button "Google Search" 
rescue 
    save_and_open_page 
end 

如果您在“302”移动页面,好像我是在结束了,那么你可以添加后下面的访问:

click_link "here"