Class: ActionDispatch::SystemTestCase
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 Poltergeist, update your Gemfile to use Poltergeist 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/poltergeist"
class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
driven_by :poltergeist, screen_size: [1400, 1400], options:
{ js_errors: true }
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
-
METHODS =
# File 'actionpack/lib/action_dispatch/system_testing/test_helpers/undef_methods.rb', line 9%i(get post put patch delete).freeze
::ActiveSupport::TestCase
- Inherited
Integration::Runner
- Included
SystemTesting::TestHelpers::UndefMethods
- Attributes & Methods
Class Attribute Summary
- .driver rw
- .driver? ⇒ Boolean rw
::ActiveSupport::TestCase
- Inherited
.config, .config?, .file_fixture_path, .file_fixture_path?, .fixture_class_names, .fixture_class_names?, .fixture_path, .fixture_path?, .fixture_table_names, .fixture_table_names?, .pre_loaded_fixtures, .pre_loaded_fixtures?, | |
.test_order | Returns the order in which test cases are run. |
.test_order= | Sets the order in which test cases are run. |
.use_instantiated_fixtures, .use_instantiated_fixtures?, .use_transactional_tests, .use_transactional_tests? |
Class Method Summary
-
.driven_by(driver, using: :chrome, screen_size: [1400, 1400], options: {})
System Test configuration options.
::ActiveSupport::Testing::Declarative
- Extended
Instance Attribute Summary
Integration::Runner
- Included
::ActiveSupport::TestCase
- Inherited
::ActiveRecord::TestFixtures
- Included
Instance Method Summary
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. |
IntegrationTest::UrlOptions
- Inherited
Routing::UrlFor
- Inherited
#initialize, | |
#route_for | Allows calling direct or regular named route. |
#url_for | Generate a URL based on the options provided, default_url_options and the routes defined in routes.rb. |
#url_options | Hook overridden in controller to add request information with |
#_routes_context, #_with_routes |
Routing::PolymorphicRoutes
- Included
#polymorphic_path | Returns the path component of a URL for the given record. |
#polymorphic_url | Constructs a call to a named RESTful route for the given record and returns the resulting URL string. |
IntegrationTest::Behavior
- Included
::ActionController::TemplateAssertions
- Included
Integration::Runner
- Included
#create_session, #initialize, #integration_session, | |
#open_session | Open a new session instance. |
#reset! | Reset the current session. |
Assertions
- Included
Assertions::RoutingAssertions
- Included
#assert_generates | Asserts that the provided options can be used to generate the provided path. |
#assert_recognizes | Asserts that the routing of the given |
#assert_routing | Asserts that path and options match both ways; in other words, it verifies that |
#method_missing | ROUTES TODO: These assertions should really work in an integration context. |
#with_routing | A helper to make it easier to test different route configurations. |
Assertions::ResponseAssertions
- Included
#assert_redirected_to | Asserts that the redirection options passed in match those of the redirect called in the latest action. |
#assert_response | Asserts that the response is one of the following types: |
TestProcess::FixtureFile
- Included
#fixture_file_upload | Shortcut for |
::ActiveSupport::TestCase
- Inherited
#assert_no_match, #assert_not_empty, #assert_not_equal, #assert_not_in_delta, #assert_not_in_epsilon, #assert_not_includes, #assert_not_instance_of, #assert_not_kind_of, #assert_not_nil, #assert_not_operator, #assert_not_predicate, #assert_not_respond_to, #assert_not_same, | |
#assert_raise | test/unit backwards compatibility methods. |
#method_name |
::ActiveRecord::TestFixtures
- Included
::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 |
::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. |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block)
[ GitHub ]# File 'actionpack/lib/action_dispatch/system_testing/test_helpers/undef_methods.rb', line 15
def method_missing(method, *args, &block) if METHODS.include?(method) raise NoMethodError, "System tests cannot make direct requests via ##{method}; use #visit and #click_on instead. See http://www.rubydoc.info/github/teamcapybara/capybara/master#The_DSL for more information." else super end end
Class Attribute Details
.driver (rw)
[ GitHub ]# File 'actionpack/lib/action_dispatch/system_test_case.rb', line 117
class_attribute :driver, instance_accessor: false
.driver? ⇒ Boolean
(rw)
[ GitHub ]
# File 'actionpack/lib/action_dispatch/system_test_case.rb', line 117
class_attribute :driver, instance_accessor: false
Class Method Details
.driven_by(driver, using: :chrome, screen_size: [1400, 1400], options: {})
System Test configuration options
The default settings are Selenium, using Chrome, with a screen size of 1400x1400.
Examples:
driven_by :poltergeist
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