Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save tanduong/c8e9c1938fa699ea6bfb to your computer and use it in GitHub Desktop.
Save tanduong/c8e9c1938fa699ea6bfb to your computer and use it in GitHub Desktop.
# turn on webkit driver debug
$webkit_debug = false
$wait_time = 5
$use_chrome_instead_of_firefox = true
def set_webkit_driver()
if $webkit_debug
Capybara.javascript_driver = :webkit_debug
else
Capybara.javascript_driver = :webkit
end
end
def webkit?()
[:webkit, :webkit_debug].include? Capybara.current_driver
end
# Possible values are :truncation and :transaction
# The :transaction strategy is faster, but might give you threading problems.
# See https://github.com/cucumber/cucumber-rails/blob/master/features/choose_javascript_database_strategy.feature
Cucumber::Rails::Database.javascript_strategy = :truncation
if $use_chrome_instead_of_firefox
# Register chrome browser for the :selenium driver, can be triggered below with the combination of @javascript @show
Capybara.register_driver :selenium do |app|
Capybara::Selenium::Driver.new(app, :browser => :chrome)
end
#else
# Capybara.register_driver :selenium do |app|
# require 'selenium/webdriver'
# profile = Selenium::WebDriver::Firefox::Profile.new
# profile['focusmanager.testmode'] = 'true'
# Capybara::Selenium::Driver.new(app, :profile => profile)
# end
end
# Unnecessary on mac
if (!OS.mac? && !$headless_started)
require 'headless'
# allow display autopick (by default)
# allow each headless to destroy_at_exit (by default)
# allow each process to have their own headless by setting reuse: false
headless_server = Headless.new(:reuse => false)
headless_server.start
$headless_started = true
puts "Process[#{Process.pid}] started headless server display: #{headless_server.display}"
end
# Set capybara-webkit as the default driver for javascript, it is 3x faster than selenium in a simple test YMMV
set_webkit_driver()
#
# If the @show tag is present on a non-mac environment, ignore it,
# otherwise allow the combination of @javascript @show to trigger the
# :selenium driver (which is :chrome browser as configured above)
#
Around('@javascript') do |scenario, block|
# problem on @show with backgrounds: https://github.com/cucumber/cucumber/issues/52
if should_show(scenario)
# for some reason, just using the `using_driver` doesn't get us all the way there, so set/reset the `javascript_driver` as necessary.
original_driver = Capybara.javascript_driver
begin
Capybara.javascript_driver = :selenium
Capybara.using_driver :selenium do
block.call
end
ensure
Capybara.javascript_driver = original_driver
end
else
block.call
end
end
def should_show(scenario)
OS.mac? && has_tag(scenario, '@show')
end
def has_tag(scenario, tag)
(scenario.source_tags.map do |tag|
tag.name
end).include? tag
end
# Increase wait time for ajax retries
Capybara.default_wait_time = $wait_time
Before do |scenario|
@scenario = scenario
# reset to wait time in case something in the scenario upped it as it was long running
Capybara.default_wait_time = $wait_time
end
# webkit javascript check only
AfterStep do
if webkit?
#puts "All Javascript errors: #{page.driver.error_messages}"
real_error_messages = page.driver.error_messages
#real_error_messages = []
#page.driver.error_messages.each_with_index do |e, i|
# # first two messages appear to be bogus, always.
# if (e[:line_number] == 0) && (e[:source].eql? 'undefined') && (e[:message].eql? 'TypeError: \'null\' is not an object') && i <= 1
# # discard message
# else
# real_error_messages << e
# end
#end
raise "Javascript errors: #{real_error_messages}" if real_error_messages.length > 0
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment