class ActionDispatch::SystemTestCase
as you include the required gems and files.
any driver that is supported by Capybara is supported by system tests as long
Because ‘ActionDispatch::SystemTestCase` is a shim between Capybara and Rails,
end
end
driver_option.add_extension(’path/to/chrome_extension.crx’)
driver_option.add_emulation(device_name: ‘iPhone 6’)
driven_by :selenium, using: :chrome, screen_size: [1024, 768] do |driver_option|
class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
learn about supported options.
define the capabilities you want. Please refer to your driver documentation to
The block will be passed an instance of ‘<Driver>::Options` where you can
with a block.
create an instance of selenium’s ‘Chrome::Options` object and add capabilities
As an example, if you want to add mobile emulation on chrome, you’ll have to
through the ‘options` hash.
Some drivers require browser capabilities to be passed as a block instead of
end
{ js_errors: true }
driven_by :cuprite, screen_size: [1400, 1400], options:
class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
require “capybara/cuprite”
require “test_helper”
driver documentation to learn about supported options.
`:options` to pass options supported by the driver. Please refer to your
`:screen_size` to change the size of the browser screen, also you can use
`:using` option because the driver is headless, but you can still use
`application_system_test_case.rb` file. In this case, you would leave out the
instead of Selenium and then declare the driver name in the
To use a headless driver, like Cuprite, update your Gemfile to use Cuprite
`:headless_chrome` or `:headless_firefox`.
supported. You can use these browsers by setting the `:using` argument to
Headless browsers such as headless Chrome and headless Firefox are also
will be silently ignored if passed.
browser screen. These two options are not applicable for headless drivers and
are `:using` for the browser and `:screen_size` to change the size of the
`driven_by` has a required argument for the driver name. The keyword arguments
end
driven_by :selenium, using: :firefox
class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
require “test_helper”
`application_system_test_case.rb` file add the following:
the Firefox browser instead of Chrome. In your
Changing the driver configuration options is easy. Let’s say you want to use
with the Chrome browser, and a browser size of 1400x1400.
By default, ‘ActionDispatch::SystemTestCase` is driven by the Selenium driver,
end
driven_by :selenium, using: :chrome, screen_size: [1400, 1400]
class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
require “test_helper”
Capybara settings, and other configuration for your system tests.
base class for system testing. This is where you can change the driver, add
`application_system_test_case.rb` file will also be generated containing the
When generating an application or scaffold, an
end
end
assert_text ’Arya’
click_on ‘Create User’
fill_in ‘Name’, with: ‘Arya’
click_on ‘New User’
visit users_path
test “adding a new user” do
class Users::CreateTest < ApplicationSystemTestCase
require “application_system_test_case”
Here is an example system test:
that is generated with a new application or scaffold.
to configure the settings through your ‘application_system_test_case.rb` file
`ApplicationSystemTestCase`. System tests use Capybara as a base and allow you
To create a system test in your application, extend your test class from
your test suite.
use a real browser experience, you can test all of your JavaScript easily from
System tests let you test applications in the browser. Because system tests
# System Testing
def self.driven_by(driver, using: :chrome, screen_size: [1400, 1400], options: {}, &capabilities)
driven_by :selenium, using: :firefox
driven_by :selenium, using: :headless_chrome
driven_by :selenium, using: :chrome
driven_by :selenium, screen_size: [800, 800]
driven_by :cuprite
Examples:
1400x1400.
The default settings are Selenium, using Chrome, with a screen size of
System Test configuration options
def self.driven_by(driver, using: :chrome, screen_size: [1400, 1400], options: {}, &capabilities) driver_options = { using: using, screen_size: screen_size, options: options } self.driver = SystemTesting::Driver.new(driver, **driver_options, &capabilities) end
def self.served_by(host:, port:)
Configuration for the System Test application server.
def self.served_by(host:, port:) Capybara.server_host = host Capybara.server_port = port end
def self.start_application # :nodoc:
def self.start_application # :nodoc: Capybara.app = Rack::Builder.new do map "/" do run Rails.application end end SystemTesting::Server.new.run end
def app_host
def app_host Capybara.app_host || Capybara.current_session.server_url || DEFAULT_HOST end
def initialize(*) # :nodoc:
def initialize(*) # :nodoc: super self.class.driven_by(:selenium) unless self.class.driver? self.class.driver.use end
def method_missing(name, ...)
def method_missing(name, ...) if url_helpers.respond_to?(name) url_helpers.public_send(name, ...) else super end end
def respond_to_missing?(name, include_private = false)
def respond_to_missing?(name, include_private = false) url_helpers.respond_to?(name) end
def url_helpers
def url_helpers @url_helpers ||= if ActionDispatch.test_app Class.new do include ActionDispatch.test_app.routes.url_helpers include ActionDispatch.test_app.routes.mounted_helpers def url_options default_url_options.reverse_merge(host: app_host) end def app_host Capybara.app_host || Capybara.current_session.server_url || DEFAULT_HOST end end.new end end
def url_options
def url_options default_url_options.reverse_merge(host: app_host) end