系统测试
系统测试允许您在浏览器中测试应用程序。由于系统测试使用真实的浏览器体验,因此您可以轻松地从测试套件中测试所有 JavaScript。
要在应用程序中创建系统测试,请从 ApplicationSystemTestCase
扩展您的测试类。系统测试使用 Capybara 作为基础,并允许您通过 application_system_test_case.rb
文件配置设置,该文件是使用新应用程序或脚手架生成的。
以下是一个系统测试示例
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
生成应用程序或脚手架时,还会生成一个 application_system_test_case.rb
文件,其中包含系统测试的基础类。您可以在此处更改驱动程序、添加 Capybara 设置以及系统测试的其他配置。
require "test_helper"
class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
driven_by :selenium, using: :chrome, screen_size: [1400, 1400]
end
默认情况下,ActionDispatch::SystemTestCase
由 Selenium 驱动程序驱动,使用 Chrome 浏览器,浏览器大小为 1400x1400。
更改驱动程序配置选项很容易。假设您想使用 Firefox 浏览器而不是 Chrome。在您的 application_system_test_case.rb
文件中添加以下内容
require "test_helper"
class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
driven_by :selenium, using: :firefox
end
driven_by
具有驱动程序名称的必需参数。关键字参数是 :using
用于浏览器,:screen_size
用于更改浏览器屏幕的大小。这两个选项不适用于无头驱动程序,如果传递,将被静默忽略。
还支持无头浏览器,例如无头 Chrome 和无头 Firefox。您可以通过将 :using
参数设置为 :headless_chrome
或 :headless_firefox
来使用这些浏览器。
要使用无头驱动程序,例如 Cuprite,请更新您的 Gemfile 以使用 Cuprite 而不是 Selenium,然后在 application_system_test_case.rb
文件中声明驱动程序名称。在这种情况下,您将省略 :using
选项,因为驱动程序是无头的,但您仍然可以使用 :screen_size
来更改浏览器屏幕的大小,您还可以使用 :options
来传递驱动程序支持的选项。请参阅您的驱动程序文档以了解支持的选项。
require "test_helper"
require "capybara/cuprite"
class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
driven_by :cuprite, screen_size: [1400, 1400], options:
{ js_errors: true }
end
某些驱动程序需要将浏览器功能作为块传递,而不是通过options
哈希传递。
例如,如果您想在 Chrome 上添加移动模拟,您需要创建一个 Selenium 的Chrome::Options
对象实例,并使用块添加功能。
该块将传递一个<Driver>::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
由于ActionDispatch::SystemTestCase
是 Capybara 和 Rails 之间的垫片,因此只要您包含了所需的 gem 和文件,Capybara 支持的任何驱动程序都将被系统测试支持。
- Capybara::DSL
- Capybara::Minitest::Assertions
- ActionDispatch::SystemTesting::TestHelpers::ScreenshotHelper
常量
DEFAULT_HOST | = | "http://127.0.0.1" |
类公共方法
driven_by(driver, using: :chrome, screen_size: [1400, 1400], options: {}, &capabilities) 链接
系统测试配置选项
默认设置是 Selenium,使用 Chrome,屏幕尺寸为 1400x1400。
示例
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
来源:显示 | 在 GitHub 上
# File actionpack/lib/action_dispatch/system_test_case.rb, line 156 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