Permalink
Browse files

Merge branch 'refactor-system-test-driver'

  • Loading branch information...
eileencodes committed Mar 9, 2017
2 parents 37770bc + 7c9af60 commit 17c65342469f211f4a845073e47bf1dfa13ba259
@@ -2,7 +2,6 @@
require "action_controller"
require "action_dispatch/system_testing/driver"
require "action_dispatch/system_testing/server"
require "action_dispatch/system_testing/browser"
require "action_dispatch/system_testing/test_helpers/screenshot_helper"
require "action_dispatch/system_testing/test_helpers/setup_and_teardown"
@@ -84,12 +83,19 @@ class SystemTestCase < IntegrationTest
include SystemTesting::TestHelpers::SetupAndTeardown
include SystemTesting::TestHelpers::ScreenshotHelper
def initialize(*) # :nodoc:
super
self.class.superclass.driver.use
end
def self.start_application # :nodoc:
Capybara.app = Rack::Builder.new do
map "/" do
run Rails.application
end
end
SystemTesting::Server.new.run
end
# System Test configuration options
@@ -105,20 +111,12 @@ def self.start_application # :nodoc:
#
# driven_by :selenium, screen_size: [800, 800]
def self.driven_by(driver, using: :chrome, screen_size: [1400, 1400])
driver = if selenium?(driver)
SystemTesting::Browser.new(using, screen_size)
else
SystemTesting::Driver.new(driver)
end
setup { driver.use }
teardown { driver.reset }
SystemTesting::Server.new.run
@driver = SystemTesting::Driver.new(driver, using: using, screen_size: screen_size)
end
def self.selenium?(driver) # :nodoc:
driver == :selenium
# Returns the driver object for the initialized system test
def self.driver
@driver ||= SystemTestCase.driven_by(:selenium)
end
end
@@ -1,27 +0,0 @@
require "action_dispatch/system_testing/driver"
module ActionDispatch
module SystemTesting
class Browser < Driver # :nodoc:
def initialize(name, screen_size)
super(name)
@name = name
@screen_size = screen_size
end
def use
register
super
end
private
def register
Capybara.register_driver @name do |app|
Capybara::Selenium::Driver.new(app, browser: @name).tap do |driver|
driver.browser.manage.window.size = Selenium::WebDriver::Dimension.new(*@screen_size)
end
end
end
end
end
end
@@ -1,18 +1,33 @@
module ActionDispatch
module SystemTesting
class Driver # :nodoc:
def initialize(name)
def initialize(name, **options)
@name = name
@browser = options[:using]
@screen_size = options[:screen_size]
end
def use
@current = Capybara.current_driver
Capybara.current_driver = @name
register if selenium?
setup
end
def reset
Capybara.current_driver = @current
end
private
def selenium?
@name == :selenium
end
def register
Capybara.register_driver @name 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 setup
Capybara.current_driver = @name
end
end
end
end
@@ -57,7 +57,7 @@ def failed?
end
def supports_screenshot?
page.driver.public_methods(false).include?(:save_screenshot)
Capybara.current_driver != :rack_test
end
end
end
@@ -439,3 +439,11 @@ class ActiveSupport::TestCase
skip message if defined?(JRUBY_VERSION)
end
end
class DrivenByRackTest < ActionDispatch::SystemTestCase
driven_by :rack_test
end
class DrivenBySeleniumWithChrome < ActionDispatch::SystemTestCase
driven_by :selenium, using: :chrome
end
@@ -1,10 +0,0 @@
require "abstract_unit"
require "action_dispatch/system_testing/browser"
class BrowserTest < ActiveSupport::TestCase
test "initializing the browser" do
browser = ActionDispatch::SystemTesting::Browser.new(:chrome, [ 1400, 1400 ])
assert_equal :chrome, browser.instance_variable_get(:@name)
assert_equal [ 1400, 1400 ], browser.instance_variable_get(:@screen_size)
end
end
@@ -6,4 +6,15 @@ class DriverTest < ActiveSupport::TestCase
driver = ActionDispatch::SystemTesting::Driver.new(:selenium)
assert_equal :selenium, driver.instance_variable_get(:@name)
end
test "initializing the driver with a browser" do
driver = ActionDispatch::SystemTesting::Driver.new(:selenium, using: :chrome, screen_size: [1400, 1400])
assert_equal :selenium, driver.instance_variable_get(:@name)
assert_equal :chrome, driver.instance_variable_get(:@browser)
assert_equal [1400, 1400], driver.instance_variable_get(:@screen_size)
end
test "selenium? returns false if driver is poltergeist" do
assert_not ActionDispatch::SystemTesting::Driver.new(:poltergeist).send(:selenium?)
end
end
@@ -4,50 +4,38 @@
class ScreenshotHelperTest < ActiveSupport::TestCase
test "image path is saved in tmp directory" do
new_test = ActionDispatch::SystemTestCase.new("x")
new_test = DrivenBySeleniumWithChrome.new("x")
assert_equal "tmp/screenshots/x.png", new_test.send(:image_path)
end
test "image path includes failures text if test did not pass" do
new_test = ActionDispatch::SystemTestCase.new("x")
new_test = DrivenBySeleniumWithChrome.new("x")
new_test.stub :passed?, false do
assert_equal "tmp/screenshots/failures_x.png", new_test.send(:image_path)
end
end
test "image path does not include failures text if test skipped" do
new_test = ActionDispatch::SystemTestCase.new("x")
new_test = DrivenBySeleniumWithChrome.new("x")
new_test.stub :passed?, false do
new_test.stub :skipped?, true do
assert_equal "tmp/screenshots/x.png", new_test.send(:image_path)
end
end
end
end
class RackTestScreenshotsTest < DrivenByRackTest
test "rack_test driver does not support screenshot" do
begin
original_driver = Capybara.current_driver
Capybara.current_driver = :rack_test
new_test = ActionDispatch::SystemTestCase.new("x")
assert_not new_test.send(:supports_screenshot?)
ensure
Capybara.current_driver = original_driver
end
assert_not self.send(:supports_screenshot?)
end
end
class SeleniumScreenshotsTest < DrivenBySeleniumWithChrome
test "selenium driver supports screenshot" do
begin
original_driver = Capybara.current_driver
Capybara.current_driver = :selenium
new_test = ActionDispatch::SystemTestCase.new("x")
assert new_test.send(:supports_screenshot?)
ensure
Capybara.current_driver = original_driver
end
assert self.send(:supports_screenshot?)
end
end
@@ -1,23 +1,13 @@
require "abstract_unit"
class DrivenByCaseTestTest < ActiveSupport::TestCase
test "selenium? returns false if driver is poltergeist" do
assert_not ActionDispatch::SystemTestCase.selenium?(:poltergeist)
end
end
class DrivenByRackTestTest < ActionDispatch::SystemTestCase
driven_by :rack_test
class SetDriverToRackTestTest < DrivenByRackTest
test "uses rack_test" do
assert_equal :rack_test, Capybara.current_driver
end
end
class DrivenBySeleniumWithChromeTest < ActionDispatch::SystemTestCase
driven_by :selenium, using: :chrome
class SetDriverToSeleniumTest < DrivenBySeleniumWithChrome
test "uses selenium" do
assert_equal :chrome, Capybara.current_driver
assert_equal :selenium, Capybara.current_driver
end
end

0 comments on commit 17c6534

Please sign in to comment.