跳到內容 跳到搜尋

系統測試

系統測試讓您可以於瀏覽器中測試應用程式。由於系統測試使用真實的瀏覽器體驗,您可以輕鬆從您的測試套件測試所有 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

有些驅動程式需要將瀏覽器功能傳遞為區塊,而非透過「選項」雜湊函式傳遞。

例如,如果您想在 Chrome 上加入行動模擬,您必須建立一個 selenium 「Chrome::Options」物件的執行個體,並透過區塊加入功能。

區塊將傳遞一個「::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 支援的驅動程式都受系統測試支援。

方法
D
S
已包含模組

常數

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
# 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

served_by(host:, port:)

System Test 應用程式伺服器組態設定。

預設這會是 localhost。這個方法允許手動指定主機和連接埠。

# 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