public
Last active

Capybara helpers

  • Download Gist
capybara-helpers.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
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) {
$.cookie('alert_message', msg, { path: '/' } );
return true;
}; |
yield
page.evaluate_script("$.cookie('alert_message')").should eq(msg)
ensure
page.evaluate_script("$.cookie('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

Instead of temporarily overriding Capybara.default_wait_time, I would do this in wait_until_ajax_done:

def wait_until_ajax_done(wait_time=Capybara.default_wait_time)
  wait_until wait_time do
    page.evaluate_script('$.active') == 0
  end
end

@BMorearty I like that suggestion, thanks!

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.