【问题标题】:How to setup Selenium Webdriver, Capybara, and Rails Minitest?如何设置 Selenium Webdriver、Capybara 和 Rails Minitest?
【发布时间】:2021-10-11 01:14:54
【问题描述】:

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

google-chrome-stable:谷歌浏览器 92.0.4515.131

chromedriver: ChromeDriver 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>'

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

【问题讨论】:

  • 你可以试试option.add_argument "no-sandbox"而不是option.add_argument "--no-sandbox"
  • 好的,我想我知道问题出在哪里了。 Capybara.app_host = "http://app.example.com/" 是这里的罪魁祸首。当我删除它并更改我的路线时,测试会成功运行。
  • 但是,路由有子域约束。那么如何使用app 子域制作水豚访问路线?有什么线索吗?如果我覆盖 Capybara.app_host 测试将失败并出现问题中提到的错误。

标签: ruby-on-rails ruby selenium selenium-webdriver capybara


【解决方案1】:

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

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

# test/application_system_test_case.rb

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

现在测试按预期运行。

【讨论】:

    【解决方案2】:

    错误net::ERR_NAME_NOT_RESOLVED 告诉您问题所在。 www.example.com 没有解析到正在启动浏览器的任何机器上的 IP 地址。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-20
      • 1970-01-01
      • 1970-01-01
      • 2020-07-19
      • 2012-05-21
      相关资源
      最近更新 更多