Skip to content

Instantly share code, notes, and snippets.

@danmartens
Last active December 11, 2015 06:39
Show Gist options
  • Select an option

  • Save danmartens/4561192 to your computer and use it in GitHub Desktop.

Select an option

Save danmartens/4561192 to your computer and use it in GitHub Desktop.
Capybara JavaScript Helpers
# Example:
#
# it "should alert me" do
# stub_browser_alert
#
# visit "/"
#
# wait_until { browser_alert_messages.count > 0 }
#
# browser_alert_messages.last.should eq("Hello World!")
# end
module CapybaraJavaScriptHelpers
include Capybara::DSL
def stub_browser_alert
page.execute_script(%{
window.Capybara || (window.Capybara = {});
window.Capybara.alertMessages = [];
window.alert = function(message) {
window.Capybara.alertMessages.push(message);
}
})
end
def browser_alert_messages
page.evaluate_script("window.Capybara.alertMessages")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment