Class: ActionDispatch::SystemTestCase
Relationships & Source Files | |
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
self,
::ActiveSupport::TestCase ,
::ActiveSupport::Testing::Declarative ,
Minitest::Test
|
|
Instance Chain:
self,
SystemTesting::TestHelpers::ScreenshotHelper ,
SystemTesting::TestHelpers::SetupAndTeardown ,
Capybara::Minitest::Assertions,
Capybara::DSL,
::ActiveSupport::TestCase ,
::ActiveSupport::Testing::FileFixtures ,
::ActiveSupport::Testing::TimeHelpers ,
::ActiveSupport::Testing::ConstantStubbing ,
::ActiveSupport::Testing::Deprecation ,
::ActiveSupport::Testing::ErrorReporterAssertions ,
::ActiveSupport::Testing::Assertions ,
::ActiveSupport::Testing::TestsWithoutAssertions ,
::ActiveSupport::Testing::SetupAndTeardown ,
::ActiveSupport::Testing::TaggedLogging ,
Minitest::Test
|
|
Inherits: |
ActiveSupport::TestCase
|
Defined in: | actionpack/lib/action_dispatch/system_test_case.rb |
Overview
System Testing
System tests let you test applications in the browser. Because system tests use a real browser experience, you can test all of your JavaScript easily from your test suite.
To create a system test in your application, extend your test class from ApplicationSystemTestCase
. System tests use Capybara as a base and allow you to configure the settings through your application_system_test_case.rb
file that is generated with a new application or scaffold.
Here is an example system test:
require "application_system_test_case"
class Users::CreateTest < ApplicationSystemTestCase
test "adding a new user" do
visit users_path
click_on 'New User'
fill_in 'Name', with: 'Arya'
click_on 'Create User'
assert_text 'Arya'
end
end
When generating an application or scaffold, an application_system_test_case.rb
file will also be generated containing the base class for system testing. This is where you can change the driver, add Capybara settings, and other configuration for your system tests.
require "test_helper"
class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
driven_by :selenium, using: :chrome, screen_size: [1400, 1400]
end
By default, SystemTestCase
is driven by the Selenium driver, with the Chrome browser, and a browser size of 1400x1400.
Changing the driver configuration options is easy. Let’s say you want to use the Firefox browser instead of Chrome. In your application_system_test_case.rb
file add the following:
require "test_helper"
class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
driven_by :selenium, using: :firefox
end
.driven_by has a required argument for the driver name. The keyword arguments are :using
for the browser and :screen_size
to change the size of the browser screen. These two options are not applicable for headless drivers and will be silently ignored if passed.
Headless browsers such as headless Chrome and headless Firefox are also supported. You can use these browsers by setting the :using
argument to :headless_chrome
or :headless_firefox
.
To use a headless driver, like Cuprite, update your Gemfile to use Cuprite instead of Selenium and then declare the driver name in the application_system_test_case.rb
file. In this case, you would leave out the :using
option because the driver is headless, but you can still use :screen_size
to change the size of the browser screen, also you can use :options
to pass options supported by the driver. Please refer to your driver documentation to learn about supported options.
require "test_helper"
require "capybara/cuprite"
class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
driven_by :cuprite, screen_size: [1400, 1400], options:
{ js_errors: true }
end
Some drivers require browser capabilities to be passed as a block instead of through the options
hash.
As an example, if you want to add mobile emulation on chrome, you’ll have to create an instance of selenium’s Chrome::Options
object and add capabilities with a block.
The block will be passed an instance of
where you can define the capabilities you want. Please refer to your driver documentation to learn about supported options.
class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
driven_by :selenium, using: :chrome, screen_size: [1024, 768] do |driver_option|
driver_option.add_emulation(device_name: 'iPhone 6')
driver_option.add_extension('path/to/chrome_extension.crx')
end
end
Because SystemTestCase
is a shim between Capybara and ::Rails
, any driver that is supported by Capybara is supported by system tests as long as you include the required gems and files.
Constant Summary
-
DEFAULT_HOST =
# File 'actionpack/lib/action_dispatch/system_test_case.rb', line 120"http://127.0.0.1"
::ActiveSupport::Testing::Assertions
- Included
::ActiveSupport::TestCase
- Inherited
Class Attribute Summary
- .driver rw
- .driver? ⇒ Boolean rw
::ActiveSupport::TestCase
- Inherited
.file_fixture_path, .file_fixture_path?, | |
.test_order | Returns the order in which test cases are run. |
.test_order= | Sets the order in which test cases are run. |
Class Method Summary
-
.driven_by(driver, using: :chrome, screen_size: [1400, 1400], options: {}, &capabilities)
System Test configuration options.
-
.served_by(host:, port:)
Configuration for the System Test application server.
- .new ⇒ SystemTestCase constructor Internal use only
- .start_application Internal use only
::ActiveSupport::TestCase
- Inherited
.fixture_paths | Returns the |
.fixture_paths= | Sets the given path to the fixture set. |
.parallelize | Parallelizes the test suite. |
.parallelize_setup | Set up hook for parallel testing. |
.parallelize_teardown | Clean up hook for parallel testing. |
::ActiveSupport::Testing::Declarative
- Extended
Instance Attribute Summary
SystemTesting::TestHelpers::ScreenshotHelper
- Included
::ActiveSupport::TestCase
- Inherited
::ActiveSupport::Testing::TimeHelpers
- Included
::ActiveSupport::Testing::TaggedLogging
- Included
Instance Method Summary
- #method_missing(name) private
- #respond_to_missing?(name, include_private = false) ⇒ Boolean private
- #url_helpers private
SystemTesting::TestHelpers::ScreenshotHelper
- Included
#take_failed_screenshot | Takes a screenshot of the current page in the browser if the test failed. |
#take_screenshot | Takes a screenshot of the current page in the browser. |
#absolute_html_path, #absolute_image_path, #absolute_path, #display_image, #html_path, #image_name, #image_path, #increment_unique, #inline_base64, #output_type, #relative_image_path, #save_html, #save_image, #screenshots_dir, #show, #unique |
SystemTesting::TestHelpers::SetupAndTeardown
- Included
::ActiveSupport::TestCase
- Inherited
#assert_no_match | Alias for: refute_match. |
#assert_not_empty | Alias for: refute_empty. |
#assert_not_equal | Alias for: refute_equal. |
#assert_not_in_delta | Alias for: refute_in_delta. |
#assert_not_in_epsilon | Alias for: refute_in_epsilon. |
#assert_not_includes | Alias for: refute_includes. |
#assert_not_instance_of | Alias for: refute_instance_of. |
#assert_not_kind_of | Alias for: refute_kind_of. |
#assert_not_nil | Alias for: refute_nil. |
#assert_not_operator | Alias for: refute_operator. |
#assert_not_predicate | Alias for: refute_predicate. |
#assert_not_respond_to | Alias for: refute_respond_to. |
#assert_not_same | Alias for: refute_same. |
#method_name, #inspect |
::ActiveSupport::Testing::FileFixtures
- Included
#file_fixture | Returns a |
::ActiveSupport::Testing::TimeHelpers
- Included
#after_teardown, | |
#freeze_time | Calls |
#travel | Changes current time to the time in the future or in the past by a given time difference by stubbing |
#travel_back | Returns the current time back to its original state, by removing the stubs added by |
#travel_to | Changes current time to the given time by stubbing |
#unfreeze_time | |
#simple_stubs |
::ActiveSupport::Testing::ConstantStubbing
- Included
#stub_const | Changes the value of a constant for the duration of a block. |
::ActiveSupport::Testing::Deprecation
- Included
#assert_deprecated | Asserts that a matching deprecation warning was emitted by the given deprecator during the execution of the yielded block. |
#assert_not_deprecated | Asserts that no deprecation warnings are emitted by the given deprecator during the execution of the yielded block. |
#collect_deprecations | Returns the return value of the block and an array of all the deprecation warnings emitted by the given deprecator during the execution of the yielded block. |
::ActiveSupport::Testing::ErrorReporterAssertions
- Included
#assert_error_reported | Assertion that the block should cause at least one exception to be reported to Rails.error. |
#assert_no_error_reported | Assertion that the block should not cause an exception to be reported to Rails.error. |
::ActiveSupport::Testing::Assertions
- Included
#assert_changes | Assertion that the result of evaluating an expression is changed before and after invoking the passed in block. |
#assert_difference | Test numeric difference between the return value of an expression as a result of what is evaluated in the yielded block. |
#assert_no_changes | Assertion that the result of evaluating an expression is not changed before and after invoking the passed in block. |
#assert_no_difference | Assertion that the numeric result of evaluating an expression is not changed before and after invoking the passed in block. |
#assert_not | Asserts that an expression is not truthy. |
#assert_nothing_raised | Assertion that the block should not raise an exception. |
#assert_raise | |
#assert_raises | Asserts that a block raises one of |
#_assert_nothing_raised_or_warn, #_callable_to_source_string |
::ActiveSupport::Testing::TestsWithoutAssertions
- Included
::ActiveSupport::Testing::SetupAndTeardown
- Included
::ActiveSupport::Testing::TaggedLogging
- Included
Constructor Details
.new ⇒ SystemTestCase
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name) (private)
[ GitHub ]# File 'actionpack/lib/action_dispatch/system_test_case.rb', line 191
def method_missing(name, ...) if url_helpers.respond_to?(name) url_helpers.public_send(name, ...) else super end end
Class Attribute Details
.driver (rw)
[ GitHub ]# File 'actionpack/lib/action_dispatch/system_test_case.rb', line 138
class_attribute :driver, instance_accessor: false
.driver? ⇒ Boolean
(rw)
[ GitHub ]
# File 'actionpack/lib/action_dispatch/system_test_case.rb', line 138
class_attribute :driver, instance_accessor: false
Class Method Details
.driven_by(driver, using: :chrome, screen_size: [1400, 1400], options: {}, &capabilities)
System Test configuration options
The default settings are Selenium, using Chrome, with a screen size of 1400x1400.
Examples:
driven_by :cuprite
driven_by :selenium, screen_size: [800, 800]
driven_by :selenium, using: :chrome
driven_by :selenium, using: :headless_chrome
driven_by :selenium, using: :firefox
driven_by :selenium, using: :headless_firefox
.served_by(host:, port:)
Configuration for the System Test application server.
By default this is localhost. This method allows the host and port to be specified manually.
# File 'actionpack/lib/action_dispatch/system_test_case.rb', line 167
def self.served_by(host:, port:) Capybara.server_host = host Capybara.server_port = port end
.start_application
# File 'actionpack/lib/action_dispatch/system_test_case.rb', line 128
def self.start_application # :nodoc: Capybara.app = Rack::Builder.new do map "/" do run Rails.application end end SystemTesting::Server.new.run end
Instance Method Details
#respond_to_missing?(name, include_private = false) ⇒ Boolean
(private)
# File 'actionpack/lib/action_dispatch/system_test_case.rb', line 199
def respond_to_missing?(name, include_private = false) url_helpers.respond_to?(name) end
#url_helpers (private)
[ GitHub ]# File 'actionpack/lib/action_dispatch/system_test_case.rb', line 173
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 .reverse_merge(host: app_host) end def app_host Capybara.app_host || Capybara.current_session.server_url || DEFAULT_HOST end end.new end end