Created
May 10, 2014 03:01
-
-
Save seanknox/d008b7a672846e647f38 to your computer and use it in GitHub Desktop.
RSpec + Capybara + Sauce Labs + Selenium (local or remote)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module CapybaraHelpers | |
class << self | |
def local_ip | |
ipv4_addr_info.ip_address | |
end | |
def setup_selenium_remote | |
app_host ||= ENV.fetch('SELENIUM_APP_HOST', local_ip) | |
Capybara.server_host = app_host | |
Capybara.app_host = "http://#{app_host}:#{Capybara.current_session.server.port}" | |
# Fix remote form uploads http://sauceio.com/index.php/2013/12/remote-file-uploads-with-selenium-capybara/ | |
Capybara.current_session.driver.browser.file_detector = lambda do |args| | |
str = args.first.to_s | |
str if File.exist?(str) | |
end | |
end | |
def poltergeist_driver? | |
driver_name =~ /\Apoltergeist/ | |
end | |
def selenium_remote_driver? | |
driver_name =~ /\Aselenium_remote/ | |
end | |
def selenium_local_driver? | |
driver_name =~ /\Aselenium_(chrome|firefox)/ | |
end | |
def ie_driver? | |
driver_name =~ /\Aselenium_remote_internet_explorer/ | |
end | |
def sauce_driver? | |
driver_name =~ /\Asauce/ | |
end | |
def local_storage_unavailable? | |
driver_name =~ /\Aselenium/ | |
end | |
def selenium_hub | |
ENV.fetch('SELENIUM_HUB', nil) | |
end | |
private | |
def ipv4_addr_info | |
Socket.ip_address_list.find { |intf| intf.ipv4_private? } | |
end | |
def driver_name | |
Capybara.current_driver.to_s | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# You should edit this file with the browsers you wish to use | |
# For options, check out http://saucelabs.com/docs/platforms | |
require 'sauce/capybara' | |
Sauce.config do |config| | |
config[:start_tunnel] = false | |
config[:browsers] = [ | |
['Windows 7', 'Internet Explorer', '8'], | |
# ['Windows 7', 'Firefox', '27'], | |
# ['Windows 7', 'Chrome', nil], | |
# ['OS X 10.8', 'Safari', '6'], | |
# ['Linux', 'Chrome', nil], | |
] | |
# config['record-video'] = false | |
# config['record-screenshots'] = false | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ENV['RAILS_ENV'] ||= 'test' | |
require 'spec_helper_common' | |
require File.expand_path('../../config/environment', __FILE__) | |
require 'rspec/rails' | |
require 'capybara_helpers' | |
require 'sauce_helper' if ENV['CAPYBARA_DRIVER'] == 'sauce' | |
# Requires supporting ruby files with custom matchers and macros, etc, | |
# in spec/support/ and its subdirectories. | |
Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f } | |
RSpec.configure do |config| | |
# ## Mock Framework | |
# | |
# If you prefer to use mocha, flexmock or RR, uncomment the appropriate line: | |
# | |
# config.mock_with :mocha | |
# config.mock_with :flexmock | |
# config.mock_with :rr | |
# If true, the base class of anonymous controllers will be inferred | |
# automatically. This will be the default behavior in future versions of | |
# rspec-rails. | |
config.infer_base_class_for_anonymous_controllers = false | |
# Run specs in random order to surface order dependencies. If you find an | |
# order dependency and want to debug it, you can fix the order by providing | |
# the seed, which is printed after each run. | |
# --seed 1234 | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'capybara/rspec' | |
require 'capybara/poltergeist' | |
require 'capybara/email/rspec' | |
require Rails.root.join('spec/support/capybara_driver_registrar') | |
capybara_driver = ENV.fetch('CAPYBARA_DRIVER', 'poltergeist') | |
# Decide which driver to use; defaults to poltergeist if ENV var isn't set | |
Capybara.default_driver = capybara_driver.to_sym | |
puts "Capybara driver: #{Capybara.current_driver}" | |
# Register various Selenium drivers | |
CapybaraDriverRegistrar.register_poltergeist_driver | |
CapybaraDriverRegistrar.register_selenium_local_driver(:firefox) | |
CapybaraDriverRegistrar.register_selenium_local_driver(:chrome) | |
CapybaraDriverRegistrar.register_selenium_remote_driver(:firefox) | |
CapybaraDriverRegistrar.register_selenium_remote_driver(:chrome) | |
CapybaraDriverRegistrar.register_selenium_remote_driver(:internet_explorer) | |
if CapybaraHelpers.sauce_driver? | |
CapybaraDriverRegistrar.register_selenium_remote_driver(:sauce) | |
end | |
[ApplicationController, User::Sharing].each do |c| | |
c.class_eval do | |
def default_url_options(options = {}) | |
{ host: Capybara.current_session.server.host, port: Capybara.current_session.server.port }.merge(options) | |
end | |
end | |
end | |
if ENV['CIRCLE_ARTIFACTS'] | |
Capybara.save_and_open_page_path = ENV['CIRCLE_ARTIFACTS'] | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module CapybaraDriverRegistrar | |
class << self | |
def url | |
if CapybaraHelpers.sauce_driver? | |
"http://#{ENV['SAUCE_USERNAME']}:#{ENV['SAUCE_ACCESS_KEY']}@ondemand.saucelabs.com:80/wd/hub" | |
else | |
selenium_hub = ENV.fetch('SELENIUM_HUB', nil) | |
"http://#{selenium_hub}:4444/wd/hub" | |
end | |
end | |
def register_poltergeist_driver | |
Capybara.register_driver :poltergeist_debug do |app| | |
Capybara::Poltergeist::Driver.new(app, js_errors: true, inspector: true, timeout: 45) | |
end | |
Capybara.javascript_driver = :poltergeist | |
Capybara.default_wait_time = 20 | |
end | |
def register_selenium_local_driver(browser) | |
Capybara.register_driver "selenium_#{browser}".to_sym do |app| | |
Capybara::Selenium::Driver.new(app, browser: browser) | |
end | |
Capybara.default_wait_time = 10 | |
end | |
def register_selenium_remote_driver(browser) | |
require 'selenium/webdriver/remote/http/persistent' | |
if CapybaraHelpers.selenium_remote_driver? && !ENV['SELENIUM_HUB'] | |
puts "ENV['SELENIUM_HUB'] needs to be defined if running remote Selenium tests." | |
exit 1 | |
end | |
capabilities = Selenium::WebDriver::Remote::Capabilities.new | |
capabilities.browser_name = browser | |
capabilities.version = ENV.fetch('version', nil) | |
capabilities.platform = ENV.fetch('os', 'any') | |
capabilities['ie.ensureCleanSession'] = true | |
Capybara.register_driver "selenium_remote_#{browser}".to_sym do |app| | |
http_client = Selenium::WebDriver::Remote::Http::Persistent.new | |
http_client.timeout = 300 | |
driver_options = { | |
browser: :remote, | |
url: url, | |
desired_capabilities: capabilities, | |
http_client: http_client | |
} | |
Capybara::Selenium::Driver.new app, driver_options | |
end | |
Capybara.javascript_driver = Capybara.default_driver | |
Capybara.run_server = false if CapybaraHelpers.sauce_driver? | |
if CapybaraHelpers.ie_driver? | |
Capybara.default_wait_time = 30 | |
else | |
Capybara.default_wait_time = 10 | |
end | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
unless CapybaraHelpers.poltergeist_driver? | |
VCR.configure do |config| | |
config.ignore_hosts CapybaraHelpers.local_ip | |
if CapybaraHelpers.selenium_hub | |
config.ignore_hosts CapybaraHelpers.selenium_hub | |
end | |
end | |
RSpec.configure do |config| | |
config.before(:each, type: :feature) do | |
CapybaraHelpers.setup_selenium_remote if CapybaraHelpers.selenium_remote_driver? | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment