系统测试¶ ↑
系统测试允许您在浏览器中测试应用程序。由于系统测试使用真实的浏览器体验,因此您可以轻松地从测试套件中测试所有 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 hash。
例如,如果您想为 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) Link
系统测试配置选项
默认设置为 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
# File actionpack/lib/action_dispatch/system_test_case.rb, line 158 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