Skip to content

Instantly share code, notes, and snippets.

@jamiecobbett
Created June 18, 2009 23:00
Show Gist options
  • Save jamiecobbett/132264 to your computer and use it in GitHub Desktop.
Save jamiecobbett/132264 to your computer and use it in GitHub Desktop.
Use Cucumber and Culerity to run Celerity (a headless browser with JS support)
require 'culerity'
Before do
$server ||= Culerity::run_server
$browser = Culerity::RemoteBrowserProxy.new $server, {:browser => :firefox}
@host = 'http://www.google.com'
end
at_exit do
$browser.exit
$server.close
end
When /I press "(.*)"/ do |button|
$browser.button(:text, button).click
assert_successful_response
end
When /I follow "(.*)"/ do |link|
$browser.link(:text, /#{link}/).click
assert_successful_response
end
When /I fill in "(.*)" for "(.*)"/ do |value, field|
$browser.text_field(:id, find_label(field).for).set(value)
end
When /I fill in "(.*)" for the field named "(.*)"/ do |value, field|
$browser.text_field(:name, field).set(value)
end
When /I check "(.*)"/ do |field|
$browser.check_box(:id, find_label(field).for).set(true)
end
When /^I uncheck "(.*)"$/ do |field|
$browser.check_box(:id, find_label(field).for).set(false)
end
When /I select "(.*)" from "(.*)"/ do |value, field|
$browser.select_list(:id, find_label(field).for).select value
end
When /I choose "(.*)"/ do |field|
$browser.radio(:id, find_label(field).for).set(true)
end
When /I go to (.+)/ do |path|
$browser.goto @host + path_to(path)
assert_successful_response
end
When /I wait for the AJAX call to finish/ do
$browser.wait
end
Then /I should see "(.*)"/ do |text|
# if we simply check for the browser.html content we don't find content that has been added dynamically, e.g. after an ajax call
div = $browser.div(:text, /#{text}/)
begin
div.html
rescue
#puts $browser.html
raise("div with '#{text}' not found")
end
end
Then /I should not see "(.*)"/ do |text|
div = $browser.div(:text, /#{text}/).html rescue nil
div.should be_nil
end
def find_label(text)
$browser.label :text, text
end
def assert_successful_response
status = $browser.page.web_response.status_code
if(status == 302 || status == 301)
location = $browser.page.web_response.get_response_header_value('Location')
puts "Being redirected to #{location}"
$browser.goto location
assert_successful_response
elsif status != 200
tmp = Tempfile.new 'culerity_results'
tmp << $browser.html
tmp.close
`open -a /Applications/Safari.app #{tmp.path}`
raise "Brower returned Response Code #{$browser.page.web_response.status_code}"
end
end
Feature: Google
In order to test a remote site
I surf to google
And take a look
Scenario: Trying Google
When I go to the homepage
And I fill in "Aslak Hellesoy" for the field named "q"
And I press "Google Search"
Then I should see "So little to do - so much time"
# complete rip-off what is generated in a new rails project: features/support/paths.rb
# Maps a name to a path. Used by the
#
# When /^I go to (.+)$/ do |page_name|
#
# step definition in webrat_steps.rb
#
def path_to(page_name)
case page_name
when /the homepage/
'/'
# Add more mappings here.
# Here is a more fancy example:
#
# when /^(.*)'s profile page$/i
# user_profile_path(User.find_by_login($1))
else
raise "Can't find mapping from \"#{page_name}\" to a path.\n" +
"Now, go and add a mapping in #{__FILE__}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment