diff options
author | eileencodes <eileencodes@gmail.com> | 2016-11-06 18:55:15 -0500 |
---|---|---|
committer | eileencodes <eileencodes@gmail.com> | 2017-02-20 15:07:33 -0500 |
commit | 5bf0aa6745db27c45c0778f9f6e9046f9ee9fb94 (patch) | |
tree | 7ba85a8ea15146725c68ce6b9e5a94ebac6d0c9c /actionsystemtest/lib/action_system_test/driver_adapters | |
parent | a21e18d5080a2c4808330271885f5664a725d3f3 (diff) | |
download | rails-5bf0aa6745db27c45c0778f9f6e9046f9ee9fb94.tar.gz rails-5bf0aa6745db27c45c0778f9f6e9046f9ee9fb94.tar.bz2 rails-5bf0aa6745db27c45c0778f9f6e9046f9ee9fb94.zip |
Turn system testing into it's own gem and rename
Renames `Rails::SystemTestCase` to `ActionSystemTest` and moves it to a
gem under the Rails name.
We need to name the class `ActionSystemTestCase` because the gem expects
a module but tests themselves expect a class.
Adds MIT-LICENSE, CHANGELOG, and README for the future.
Diffstat (limited to 'actionsystemtest/lib/action_system_test/driver_adapters')
3 files changed, 226 insertions, 0 deletions
diff --git a/actionsystemtest/lib/action_system_test/driver_adapters/capybara_driver.rb b/actionsystemtest/lib/action_system_test/driver_adapters/capybara_driver.rb new file mode 100644 index 0000000000..5362f6db25 --- /dev/null +++ b/actionsystemtest/lib/action_system_test/driver_adapters/capybara_driver.rb @@ -0,0 +1,83 @@ +require "action_system_test/driver_adapters/web_server" + +module ActionSystemTest + module DriverAdapters + # == CapybaraDriver for System Testing + # + # The <tt>CapybaraDriver</tt> is a shim that sits between Rails and + # Capybara. + # + # The drivers Capybara supports are: +:rack_test+, +:selenium+, +:webkit+, + # and +:poltergeist+. + # + # Rails provides its own defaults for Capybara with the Selenium driver + # through <tt>RailsSeleniumDriver</tt>, but allows users to use Selenium + # directly. + # + # To set your system tests to use one of Capybara's default drivers, add + # the following to yur Rails' configuration test environment: + # + # config.system_testing.driver = :rack_test + # + # The +:rack_test+ driver is a basic test driver that doesn't support + # JavaScript testing and doesn't require a server. + # + # The +:poltergeist+ and +:webkit+ drivers are headless, but require some + # extra environment setup. Because the default server for Rails is Puma, each + # of the Capybara drivers will default to using Puma. Changing the configuration + # to use Webrick is possible by initalizing a new driver object. + # + # The default settings for the <tt>CapybaraDriver</tt> are: + # + # #<ActionSystemTest::DriverAdapters::CapybaraDriver:0x007ff0e992c1d8 + # @name=:rack_test, + # @server=:puma, + # @port=28100 + # > + # + # The settings for the <tt>CapybaraDriver</tt> can be changed from + # Rails' configuration file. + # + # config.system_testing.driver = ActionSystemTest::DriverAdapters::CapybaraDriver.new( + # name: :webkit, + # server: :webrick + # ) + class CapybaraDriver + include WebServer + + CAPYBARA_DEFAULTS = [ :rack_test, :selenium, :webkit, :poltergeist ] + + attr_reader :name, :server, :port + + def initialize(name: :rack_test, server: :puma, port: 28100) + @name = name + @server = server + @port = port + end + + def call + registration + setup + end + + def supports_screenshots? + @name != :rack_test + end + + private + def registration + register_server + end + + def setup + set_server + set_port + set_driver + end + + def set_driver + Capybara.default_driver = @name + end + end + end +end diff --git a/actionsystemtest/lib/action_system_test/driver_adapters/rails_selenium_driver.rb b/actionsystemtest/lib/action_system_test/driver_adapters/rails_selenium_driver.rb new file mode 100644 index 0000000000..16e485398a --- /dev/null +++ b/actionsystemtest/lib/action_system_test/driver_adapters/rails_selenium_driver.rb @@ -0,0 +1,98 @@ +require "action_system_test/driver_adapters/web_server" + +module ActionSystemTest + module DriverAdapters + # == RailsSeleniumDriver for System Testing + # + # The <tt>RailsSeleniumDriver</tt> uses the Selenium 2.0 webdriver. The + # selenium-webdriver gem is required by this driver. + # + # The <tt>RailsSeleniumDriver</tt> is useful for real browser testing and + # supports Chrome and Firefox. + # + # By default Rails system testing will use Rails' configuration with Capybara + # and the Selenium driver. To explictly set the <tt>RailsSeleniumDriver</tt> + # add the following to your Rails' configuration test environment: + # + # config.system_testing.driver = :rails_selenium_driver + # + # Because this driver supports real browser testing it is required that a + # server is configured. + # + # If no server is specified when the driver is initialized, Puma will be used + # by default. The default settings for the <tt>RailsSeleniumDriver</tt> + # are as follows: + # + # #<ActionSystemTest::DriverAdapters::RailsSeleniumDriver:0x007ff0e992c1d8 + # @browser=:chrome, + # @server=:puma, + # @port=28100, + # @screen_size=[ 1400, 1400 ] + # > + # + # The settings for the <tt>RailsSeleniumDriver</tt> can be changed in the + # Rails configuration file. + # + # config.system_testing.driver = ActionSystemTest::DriverAdapters::RailsSeleniumDriver.new( + # server: :webrick, + # port: 28100, + # screen_size: [ 800, 800 ] + # ) + # + # The default browser is set to Chrome because the current version of + # Firefox does not work with selenium-webdriver. If you want to use Firefox, + # you will need to use Firefox 45.0esr or 47.0 and ensure + # that selenium-webdriver is version 2.53.4. To change the browser from + # +:chrome+ to +:firefox+, initialize the Selenium driver in your Rails' + # test environment: + # + # config.system_testing.driver = ActionSystemTest::DriverAdapters::RailsSeleniumDriver.new( + # browser: :firefox + # ) + class RailsSeleniumDriver + include WebServer + + attr_reader :browser, :server, :port, :screen_size + + def initialize(browser: :chrome, server: :puma, port: 28100, screen_size: [ 1400, 1400 ]) # :nodoc: + @browser = browser + @server = server + @port = port + @screen_size = screen_size + end + + def call # :nodoc: + registration + setup + end + + def supports_screenshots? + true + end + + private + def registration + register_browser_driver + register_server + end + + def setup + set_server + set_port + set_driver + end + + def register_browser_driver + Capybara.register_driver @browser do |app| + Capybara::Selenium::Driver.new(app, browser: @browser).tap do |driver| + driver.browser.manage.window.size = Selenium::WebDriver::Dimension.new(*@screen_size) + end + end + end + + def set_driver + Capybara.default_driver = @browser.to_sym + end + end + end +end diff --git a/actionsystemtest/lib/action_system_test/driver_adapters/web_server.rb b/actionsystemtest/lib/action_system_test/driver_adapters/web_server.rb new file mode 100644 index 0000000000..e115fe583c --- /dev/null +++ b/actionsystemtest/lib/action_system_test/driver_adapters/web_server.rb @@ -0,0 +1,45 @@ +begin + require "rack/handler/puma" +rescue LoadError + false +end + +module ActionSystemTest + module DriverAdapters + module WebServer # :nodoc: + def register_server + Capybara.register_server @server do |app, port, host| + case @server + when :puma + register_puma(app, port) + when :webrick + register_webrick(app, port) + else + register_default(app, port) + end + end + end + + private + def register_default(app, port) + Capybara.run_default_server(app, port) + end + + def register_puma(app, port) + Rack::Handler::Puma.run(app, Port: port, Threads: "0:4") + end + + def register_webrick(app, port) + Rack::Handler::WEBrick.run(app, Port: port) + end + + def set_server + Capybara.server = @server + end + + def set_port + Capybara.server_port = @port + end + end + end +end |