2017-07-24 16:20:53 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-07-24 22:19:21 -04:00
|
|
|
gem "capybara", ">= 3.26"
|
2017-10-23 14:01:42 -04:00
|
|
|
|
2017-02-19 11:50:42 -05:00
|
|
|
require "capybara/dsl"
|
Bump Capybara and include Minitest::Assertions
Capybara was updated in teamcapybara/capybara#1841 to use Minitest style
assertions so that system test output shows x number of assertions, x
numbe of failures, etc.
Before:
```
6 runs, 0 assertions, 0 failures, 0 errors, 0 skips
```
After:
```
6 runs, 7 assertions, 1 failures, 0 errors, 0 skips
```
This change bumps Capybara from 2.7.0 to 2.13.0 and includes the
required minitest assertion file in the test case. :tada:
2017-03-17 08:18:55 -04:00
|
|
|
require "capybara/minitest"
|
2017-02-19 11:50:42 -05:00
|
|
|
require "action_controller"
|
2017-10-21 09:18:17 -04:00
|
|
|
require "action_dispatch/system_testing/driver"
|
2018-01-15 14:21:48 -05:00
|
|
|
require "action_dispatch/system_testing/browser"
|
2017-10-21 09:18:17 -04:00
|
|
|
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"
|
2017-02-19 11:50:42 -05:00
|
|
|
|
|
|
|
module ActionDispatch
|
2017-02-23 14:47:09 -05:00
|
|
|
# = 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 <tt>ApplicationSystemTestCase</tt>. System tests use Capybara as a
|
|
|
|
# base and allow you to configure the settings through your
|
|
|
|
# <tt>application_system_test_case.rb</tt> file that is generated with a new
|
|
|
|
# application or scaffold.
|
|
|
|
#
|
|
|
|
# Here is an example system test:
|
|
|
|
#
|
2020-03-29 19:30:52 -04:00
|
|
|
# require "application_system_test_case"
|
2017-02-23 14:47:09 -05:00
|
|
|
#
|
|
|
|
# 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, <tt>ActionDispatch::SystemTestCase</tt> is driven by the
|
|
|
|
# Selenium driver, with the Chrome browser, and a browser size of 1400x1400.
|
|
|
|
#
|
2017-03-12 12:51:26 -04:00
|
|
|
# Changing the driver configuration options is easy. Let's say you want to use
|
2017-02-23 14:47:09 -05:00
|
|
|
# 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.
|
|
|
|
#
|
2017-12-14 11:55:22 -05:00
|
|
|
# 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+.
|
|
|
|
#
|
2021-07-14 22:40:03 -04:00
|
|
|
# To use a headless driver, like Cuprite, update your Gemfile to use
|
|
|
|
# Cuprite instead of Selenium and then declare the driver name in the
|
2017-06-06 11:31:24 -04:00
|
|
|
# +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
|
2017-06-01 15:58:42 -04:00
|
|
|
# +:screen_size+ to change the size of the browser screen, also you can use
|
2017-06-03 10:00:59 -04:00
|
|
|
# +:options+ to pass options supported by the driver. Please refer to your
|
2017-06-01 15:58:42 -04:00
|
|
|
# driver documentation to learn about supported options.
|
2017-02-23 14:47:09 -05:00
|
|
|
#
|
|
|
|
# require "test_helper"
|
2021-07-14 22:40:03 -04:00
|
|
|
# require "capybara/cuprite"
|
2017-02-23 14:47:09 -05:00
|
|
|
#
|
|
|
|
# class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
|
2021-07-14 22:40:03 -04:00
|
|
|
# driven_by :cuprite, screen_size: [1400, 1400], options:
|
2017-06-06 11:31:24 -04:00
|
|
|
# { js_errors: true }
|
2017-02-23 14:47:09 -05:00
|
|
|
# end
|
|
|
|
#
|
2019-01-29 09:01:58 -05:00
|
|
|
# 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
|
2019-02-01 02:07:41 -05:00
|
|
|
# create an instance of selenium's +Chrome::Options+ object and add
|
2019-01-29 09:01:58 -05:00
|
|
|
# capabilities with a block.
|
|
|
|
#
|
2019-02-01 02:07:41 -05:00
|
|
|
# The block will be passed an instance of <tt><Driver>::Options</tt> where you can
|
2019-01-29 09:01:58 -05:00
|
|
|
# define the capabilities you want. Please refer to your driver documentation
|
|
|
|
# to learn about supported options.
|
2018-04-03 21:58:39 -04:00
|
|
|
#
|
2019-02-01 02:07:41 -05:00
|
|
|
# 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
|
2018-04-03 21:58:39 -04:00
|
|
|
# end
|
|
|
|
#
|
2017-02-23 14:47:09 -05:00
|
|
|
# Because <tt>ActionDispatch::SystemTestCase</tt> 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.
|
2019-05-16 09:02:36 -04:00
|
|
|
class SystemTestCase < ActiveSupport::TestCase
|
2017-02-19 11:50:42 -05:00
|
|
|
include Capybara::DSL
|
Bump Capybara and include Minitest::Assertions
Capybara was updated in teamcapybara/capybara#1841 to use Minitest style
assertions so that system test output shows x number of assertions, x
numbe of failures, etc.
Before:
```
6 runs, 0 assertions, 0 failures, 0 errors, 0 skips
```
After:
```
6 runs, 7 assertions, 1 failures, 0 errors, 0 skips
```
This change bumps Capybara from 2.7.0 to 2.13.0 and includes the
required minitest assertion file in the test case. :tada:
2017-03-17 08:18:55 -04:00
|
|
|
include Capybara::Minitest::Assertions
|
2017-02-19 17:49:21 -05:00
|
|
|
include SystemTesting::TestHelpers::SetupAndTeardown
|
2017-02-19 11:50:42 -05:00
|
|
|
include SystemTesting::TestHelpers::ScreenshotHelper
|
|
|
|
|
2021-09-21 06:45:13 -04:00
|
|
|
DEFAULT_HOST = "http://127.0.0.1"
|
|
|
|
|
2017-03-06 12:33:34 -05:00
|
|
|
def initialize(*) # :nodoc:
|
|
|
|
super
|
2019-11-20 17:49:03 -05:00
|
|
|
self.class.driven_by(:selenium) unless self.class.driver?
|
2017-03-27 08:13:44 -04:00
|
|
|
self.class.driver.use
|
2017-03-06 12:33:34 -05:00
|
|
|
end
|
|
|
|
|
2017-02-19 11:50:42 -05:00
|
|
|
def self.start_application # :nodoc:
|
|
|
|
Capybara.app = Rack::Builder.new do
|
|
|
|
map "/" do
|
|
|
|
run Rails.application
|
|
|
|
end
|
|
|
|
end
|
2017-03-06 12:33:34 -05:00
|
|
|
|
|
|
|
SystemTesting::Server.new.run
|
2017-02-19 11:50:42 -05:00
|
|
|
end
|
|
|
|
|
2017-03-27 08:13:44 -04:00
|
|
|
class_attribute :driver, instance_accessor: false
|
|
|
|
|
2017-02-19 11:50:42 -05:00
|
|
|
# System Test configuration options
|
|
|
|
#
|
2017-02-20 16:08:20 -05:00
|
|
|
# The default settings are Selenium, using Chrome, with a screen size
|
2017-02-19 11:50:42 -05:00
|
|
|
# of 1400x1400.
|
|
|
|
#
|
|
|
|
# Examples:
|
|
|
|
#
|
2021-07-14 22:40:03 -04:00
|
|
|
# driven_by :cuprite
|
2017-02-19 11:50:42 -05:00
|
|
|
#
|
2017-12-07 13:02:34 -05:00
|
|
|
# driven_by :selenium, screen_size: [800, 800]
|
|
|
|
#
|
|
|
|
# driven_by :selenium, using: :chrome
|
2017-02-19 11:50:42 -05:00
|
|
|
#
|
2017-10-13 02:17:17 -04:00
|
|
|
# driven_by :selenium, using: :headless_chrome
|
|
|
|
#
|
2017-12-07 13:02:34 -05:00
|
|
|
# driven_by :selenium, using: :firefox
|
|
|
|
#
|
|
|
|
# driven_by :selenium, using: :headless_firefox
|
2019-01-29 09:01:58 -05:00
|
|
|
def self.driven_by(driver, using: :chrome, screen_size: [1400, 1400], options: {}, &capabilities)
|
2018-04-03 21:58:39 -04:00
|
|
|
driver_options = { using: using, screen_size: screen_size, options: options }
|
|
|
|
|
2019-09-08 12:14:44 -04:00
|
|
|
self.driver = SystemTesting::Driver.new(driver, **driver_options, &capabilities)
|
2017-03-06 12:33:34 -05:00
|
|
|
end
|
2017-02-23 20:48:12 -05:00
|
|
|
|
2019-07-24 22:19:21 -04:00
|
|
|
private
|
|
|
|
def url_helpers
|
|
|
|
@url_helpers ||=
|
|
|
|
if ActionDispatch.test_app
|
|
|
|
Class.new do
|
|
|
|
include ActionDispatch.test_app.routes.url_helpers
|
2019-11-20 16:19:31 -05:00
|
|
|
include ActionDispatch.test_app.routes.mounted_helpers
|
2019-07-24 22:19:21 -04:00
|
|
|
|
|
|
|
def url_options
|
2021-09-21 06:45:13 -04:00
|
|
|
default_url_options.reverse_merge(host: app_host)
|
|
|
|
end
|
|
|
|
|
|
|
|
def app_host
|
|
|
|
Capybara.app_host || Capybara.current_session.server_url || DEFAULT_HOST
|
2019-07-24 22:19:21 -04:00
|
|
|
end
|
|
|
|
end.new
|
|
|
|
end
|
2019-07-16 14:21:45 -04:00
|
|
|
end
|
|
|
|
|
2019-07-24 22:19:21 -04:00
|
|
|
def method_missing(name, *args, &block)
|
|
|
|
if url_helpers.respond_to?(name)
|
|
|
|
url_helpers.public_send(name, *args, &block)
|
|
|
|
else
|
|
|
|
super
|
|
|
|
end
|
|
|
|
end
|
2017-02-19 11:50:42 -05:00
|
|
|
|
2019-07-24 22:19:21 -04:00
|
|
|
def respond_to_missing?(name, include_private = false)
|
|
|
|
url_helpers.respond_to?(name)
|
|
|
|
end
|
|
|
|
end
|
2017-02-19 11:50:42 -05:00
|
|
|
end
|
2019-07-24 22:19:21 -04:00
|
|
|
|
|
|
|
ActiveSupport.run_load_hooks :action_dispatch_system_test_case, ActionDispatch::SystemTestCase
|
|
|
|
ActionDispatch::SystemTestCase.start_application
|