如何设置 Selenium Webdriver、Capybara 和 Rails Minitest?

How to setup Selenium Webdriver, Capybara, and Rails Minitest?

我目前正在 运行宁 WSL2。我已经在系统中安装了google-chrome-stablechromedriver

google-chrome-stable: Google Chrome 92.0.4515.131

chromedriver: Chrome驱动程序 92.0.4515.43

# Gemfile

group :test do
  gem "capybara", ">= 3.26"
  gem "selenium-webdriver"
  gem "webdrivers"
end
# test/application_system_test_case.rb

class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
  driven_by :selenium, using: :headless_chrome do |option|
    option.add_argument "no-sandbox"
  end
end
# test/system/users_test.rb

class UsersTest < ApplicationSystemTestCase
  def setup
    Capybara.app_host = "http://app.example.com/"
  end

  test "user sign up" do
    visit new_user_path
    fill_in "user[name]", with: "Jane Hemmingway"
    fill_in "user[email]", with: "jane@example.com"
    fill_in "user[password]", with: "secret_password"
    click_button "get started"

    assert_redirected_to accounts_path
  end
end

但是当我运行这个测试时,它抛出一个错误。

Error:
UsersTest#test_user_sign_up:
Selenium::WebDriver::Error::UnknownError: unknown error: net::ERR_NAME_NOT_RESOLVED
  (Session info: headless chrome=92.0.4515.131)
    test/system/users_test.rb:9:in `block in <class:UsersTest>'

我对这个错误的含义感到困惑。有什么办法可以解决吗?

好的,所以我们需要将其设置为http://app.lvh.me,而不是将Capybara.app_host设置为http://app.example.com/

我们还需要告诉水豚始终包含端口。

# test/application_system_test_case.rb

Capybara.configure do |config|
  config.always_include_port = true
end

现在测试按预期运行。

错误 net::ERR_NAME_NOT_RESOLVED 告诉您问题出在哪里。 www.example.com 未解析为启动浏览器的任何计算机上的 IP 地址。