# frozen_string_literal: true # This plugin does not attempt to require capybara. It assumes that if you're # using capybara, you're probably using something like Bundler which has already # required it for you. # # If this is not the case, you will need to require it prior to requiring # rspeckled. require 'capybara/rspec' require 'capybara/rails' require 'rspeckled/selenium/drivers/chrome' require 'rspeckled/selenium/drivers/firefox' # Configure Capybara Capybara.configure do |config| config.exact = true config.match = :one config.ignore_hidden_elements = true config.visible_text_only = true end if defined?(Chamber) Capybara.default_host = Chamber.env.http.base_url Capybara.app_host = Chamber.env.http.base_url Capybara.asset_host = Chamber.env.http.base_url Capybara.server_port = Chamber.env.http.url_components.port end Capybara.save_path = "#{Dir.pwd}/tmp/screenshots" # Configure Server Capybara.register_server :puma do |app, _port, _host| require 'rack/handler/puma' host = (defined?(Chamber) && Chamber.env.http.url_components.host) || ENV['HOST'] || 'lvh.me' port = (defined?(Chamber) && Chamber.env.http.url_components.port) || ENV['PORT'] || '3000' Rack::Handler::Puma.run(app, :Host => host, :Port => port, :Threads => '0:1', :Silent => true) end Capybara.server = :puma # Configure Browser Drivers downloads_directory = "#{Dir.pwd}/tmp/downloads/" chrome_driver = ::Rspeckled::Selenium::Drivers::Chrome.new(:downloads_directory => downloads_directory) firefox_driver = ::Rspeckled::Selenium::Drivers::Firefox.new(:downloads_directory => downloads_directory) Capybara.register_driver(:chrome) { |app| chrome_driver.driver(app) } Capybara.register_driver(:headless_chrome) { |app| chrome_driver.headless(app) } Capybara.register_driver(:firefox) { |app| firefox_driver.driver(app) } Capybara.register_driver(:headless_firefox) { |app| firefox_driver.headless(app) } Capybara.javascript_driver = :headless_firefox ::Selenium::WebDriver.logger.output = 'log/selenium.log' if defined?(::Selenium::WebDriver) # Configure Screenshotting if defined?(::Capybara::Screenshot) require 'capybara-screenshot/rspec' ::Capybara::Screenshot.autosave_on_failure = true ::Capybara::Screenshot.prune_strategy = :keep_last_run ::Capybara::Screenshot::RSpec.add_link_to_screenshot_for_failed_examples = false ::Capybara::Screenshot.register_filename_prefix_formatter(:rspec) do |example| example.full_description.downcase.tr(' ', '-').gsub(%r{^.*/spec/}, '') end ::Capybara::Screenshot.register_driver(:chrome) { |driver, path| driver.browser.save_screenshot(path) } ::Capybara::Screenshot.register_driver(:headless_chrome) { |driver, path| driver.browser.save_screenshot(path) } ::Capybara::Screenshot.register_driver(:firefox) { |driver, path| driver.browser.save_screenshot(path) } ::Capybara::Screenshot.register_driver(:headless_firefox) { |driver, path| driver.browser.save_screenshot(path) } end # Configure Example Metadata RSpec.configure do |config| config.before(:each, :js => true) do page.driver.reset! end # Focus The Browser on Activation config.around(:each, :js => true, :autofocus => true) do |example| browser_name = example.metadata[:driver].to_s.capitalize Thread.new do sleep 1.5 ` osascript -e ' tell the application named "#{browser_name}" activate set theWindow to the first item of windows if index of theWindow is not 1 then set index to 1 set visible to false set visible to true end if end tell ' > /dev/null ` end example.run sleep example.metadata[:delay] end # Setup Debug Logging config.before(:each, :js => true, :debug => true) do Selenium::WebDriver.logger.level = :debug if defined?(Selenium::WebDriver) end config.after(:each, :js => true, :debug => true) do Selenium::WebDriver.logger.level = :warn if defined?(Selenium::WebDriver) end end