I'm studying webrat and cucumber and trying to create simple example. Here is my feature file:
Scenario: Searching for something
Given I have opened "http://www.google.com/"
When I search for "some text"
Here is my step definitions:
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
When I am running the tests I get error message:
Could not find field: "q" (Webrat::NotFoundError)
If I'll comment 'fill_in'-line I get other error message:
Could not find button "Google Search" (Webrat::NotFoundError)
How can I fix it?
The problem may be that Webrat is not following the redirect from www.google.com to your locale specific Google site (for details on redirect). For example, since I am in Canada, going to www.google.com will redirect me to www.google.ca. As a result, when I use Webrat to visit www.google.com I see a '302 Moved' page. Since webrat does not follow the redirect to the search page, you do not have access to the text field 'q'.
I would try the save_and_open_page
method to check what page you actually ended up on. You can run the following script (and then open the file it creates) as a quick check:
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
If you are ending up on the '302 Moved' page like I was, then you can add the following after the visit:
click_link "here"