mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Move browser checking to its own class
This commit is contained in:
parent
3ddb811acc
commit
f52e17f1c3
4 changed files with 57 additions and 29 deletions
|
@ -6,6 +6,7 @@ require "capybara/dsl"
|
|||
require "capybara/minitest"
|
||||
require "action_controller"
|
||||
require "action_dispatch/system_testing/driver"
|
||||
require "action_dispatch/system_testing/browser"
|
||||
require "action_dispatch/system_testing/server"
|
||||
require "action_dispatch/system_testing/test_helpers/screenshot_helper"
|
||||
require "action_dispatch/system_testing/test_helpers/setup_and_teardown"
|
||||
|
|
49
actionpack/lib/action_dispatch/system_testing/browser.rb
Normal file
49
actionpack/lib/action_dispatch/system_testing/browser.rb
Normal file
|
@ -0,0 +1,49 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
module ActionDispatch
|
||||
module SystemTesting
|
||||
class Browser # :nodoc:
|
||||
attr_reader :name
|
||||
|
||||
def initialize(name)
|
||||
@name = name
|
||||
end
|
||||
|
||||
def type
|
||||
case name
|
||||
when :headless_chrome
|
||||
:chrome
|
||||
when :headless_firefox
|
||||
:firefox
|
||||
else
|
||||
name
|
||||
end
|
||||
end
|
||||
|
||||
def options
|
||||
case name
|
||||
when :headless_chrome
|
||||
headless_chrome_browser_options
|
||||
when :headless_firefox
|
||||
headless_firefox_browser_options
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
def headless_chrome_browser_options
|
||||
options = Selenium::WebDriver::Chrome::Options.new
|
||||
options.args << "--headless"
|
||||
options.args << "--disable-gpu"
|
||||
|
||||
options
|
||||
end
|
||||
|
||||
def headless_firefox_browser_options
|
||||
options = Selenium::WebDriver::Firefox::Options.new
|
||||
options.args << "-headless"
|
||||
|
||||
options
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -5,7 +5,7 @@ module ActionDispatch
|
|||
class Driver # :nodoc:
|
||||
def initialize(name, **options)
|
||||
@name = name
|
||||
@browser = options[:using]
|
||||
@browser = Browser.new(options[:using])
|
||||
@screen_size = options[:screen_size]
|
||||
@options = options[:options]
|
||||
end
|
||||
|
@ -32,34 +32,11 @@ module ActionDispatch
|
|||
end
|
||||
|
||||
def browser_options
|
||||
if @browser == :headless_chrome
|
||||
browser_options = Selenium::WebDriver::Chrome::Options.new
|
||||
browser_options.args << "--headless"
|
||||
browser_options.args << "--disable-gpu"
|
||||
|
||||
@options.merge(options: browser_options)
|
||||
elsif @browser == :headless_firefox
|
||||
browser_options = Selenium::WebDriver::Firefox::Options.new
|
||||
browser_options.args << "-headless"
|
||||
|
||||
@options.merge(options: browser_options)
|
||||
else
|
||||
@options
|
||||
end
|
||||
end
|
||||
|
||||
def browser
|
||||
if @browser == :headless_chrome
|
||||
:chrome
|
||||
elsif @browser == :headless_firefox
|
||||
:firefox
|
||||
else
|
||||
@browser
|
||||
end
|
||||
@options.merge(options: @browser.options).compact
|
||||
end
|
||||
|
||||
def register_selenium(app)
|
||||
Capybara::Selenium::Driver.new(app, { browser: browser }.merge(browser_options)).tap do |driver|
|
||||
Capybara::Selenium::Driver.new(app, { browser: @browser.type }.merge(browser_options)).tap do |driver|
|
||||
driver.browser.manage.window.size = Selenium::WebDriver::Dimension.new(*@screen_size)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -12,7 +12,8 @@ class DriverTest < ActiveSupport::TestCase
|
|||
test "initializing the driver with a browser" do
|
||||
driver = ActionDispatch::SystemTesting::Driver.new(:selenium, using: :chrome, screen_size: [1400, 1400], options: { url: "http://example.com/wd/hub" })
|
||||
assert_equal :selenium, driver.instance_variable_get(:@name)
|
||||
assert_equal :chrome, driver.instance_variable_get(:@browser)
|
||||
assert_equal :chrome, driver.instance_variable_get(:@browser).name
|
||||
assert_nil driver.instance_variable_get(:@browser).options
|
||||
assert_equal [1400, 1400], driver.instance_variable_get(:@screen_size)
|
||||
assert_equal ({ url: "http://example.com/wd/hub" }), driver.instance_variable_get(:@options)
|
||||
end
|
||||
|
@ -20,7 +21,7 @@ class DriverTest < ActiveSupport::TestCase
|
|||
test "initializing the driver with a headless chrome" do
|
||||
driver = ActionDispatch::SystemTesting::Driver.new(:selenium, using: :headless_chrome, screen_size: [1400, 1400], options: { url: "http://example.com/wd/hub" })
|
||||
assert_equal :selenium, driver.instance_variable_get(:@name)
|
||||
assert_equal :headless_chrome, driver.instance_variable_get(:@browser)
|
||||
assert_equal :headless_chrome, driver.instance_variable_get(:@browser).name
|
||||
assert_equal [1400, 1400], driver.instance_variable_get(:@screen_size)
|
||||
assert_equal ({ url: "http://example.com/wd/hub" }), driver.instance_variable_get(:@options)
|
||||
end
|
||||
|
@ -28,7 +29,7 @@ class DriverTest < ActiveSupport::TestCase
|
|||
test "initializing the driver with a headless firefox" do
|
||||
driver = ActionDispatch::SystemTesting::Driver.new(:selenium, using: :headless_firefox, screen_size: [1400, 1400], options: { url: "http://example.com/wd/hub" })
|
||||
assert_equal :selenium, driver.instance_variable_get(:@name)
|
||||
assert_equal :headless_firefox, driver.instance_variable_get(:@browser)
|
||||
assert_equal :headless_firefox, driver.instance_variable_get(:@browser).name
|
||||
assert_equal [1400, 1400], driver.instance_variable_get(:@screen_size)
|
||||
assert_equal ({ url: "http://example.com/wd/hub" }), driver.instance_variable_get(:@options)
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue