测试谷歌搜索页面



我正在研究webrat和cucumber,并试图创建一个简单的示例。这是我的功能文件:

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",我会得到另一条错误消息:

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

我该怎么修?

问题可能是Webrat没有按照从www.google.com重定向到您所在地区特定的谷歌网站(有关重定向的详细信息)。例如,由于我在加拿大,访问www.google.com会将我重定向到www.google.ca。因此,当我使用Webrat访问www.google.com时,我会看到一个"302移动"页面。由于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"

最新更新