Skip to content

Instantly share code, notes, and snippets.

@ozeias
Forked from ntreadway/capybara-helpers.rb
Created October 2, 2012 19:09
Show Gist options
  • Save ozeias/3822613 to your computer and use it in GitHub Desktop.
Save ozeias/3822613 to your computer and use it in GitHub Desktop.
Capybara helpers
def set_mobile_agent
page.driver.header('User-Agent', 'iPhone')
end
def js_click(selector)
page.driver.execute_script "$('##{selector}').click()"
end
def screenshot
require 'capybara/util/save_and_open_page'
now = Time.now
p = "/#{now.strftime('%Y-%m-%d-%H-%M-%S')}-#{rand}"
Capybara.save_page body, "#{p}.html"
path = Rails.root.join("#{Capybara.save_and_open_page_path}" "#{p}.png").to_s
page.driver.render path
Launchy.open path
end
def verify_alert_message(msg, &block)
page.evaluate_script %Q| window.alert = function(msg) {
window.alert_message = msg;
return true;
}; |
yield
page.evaluate_script("window.alert_message").should eq(msg)
ensure
page.evaluate_script("window.alert_message = null)")
end
def js_confirm(accept=true)
page.evaluate_script "window.original_confirm = window.confirm"
page.evaluate_script "window.confirm = function(msg) { return #{!!accept}; }"
yield
ensure
page.evaluate_script "window.confirm = window.original_confirm"
end
def wait_until_ajax_done
Capybara.default_wait_time = 5
wait_until do
page.evaluate_script('$.active') == 0
end
Capybara.default_wait_time = 2
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment