[英]Capybara.current_driver = :chrome versus :selenium_chrome
[英]Capybara selenium_chrome going to example.com instead of localhost
我正在Rails项目中设置Cucumber测试。 使用默认驱动程序时,一切正常。 但是,当我尝试使用:selenium_chrome
驱动程序时,浏览器尝试加载example.com
而不是本地Rails服务器。 知道我缺少什么吗?
我的步骤如下所示:
Before do |scenario|
Capybara.current_driver = :selenium_chrome
end
When(/^I visit the posts page$/) do
visit posts_url
end
运行这些功能时,可以看到启动了Rails服务器:
Using the default profile...
Feature: Posts
Capybara starting Puma...
* Version 3.12.0 , codename: Llamas in Pajamas
* Min threads: 0, max threads: 4
* Listening on tcp://127.0.0.1:62056
但是,弹出的Chrome窗口正在尝试访问http://www.example.com/posts
而不是http://127.0.0.1:62056/posts
我在某处缺少配置步骤吗?
相关说明:如果要使用Selenium运行所有测试,是否必须将Capybara.current_driver
行放在Before
块中? 我尝试只将其添加到features/support/env.rb
,但似乎没有任何效果。
我在MacOS 10.14.4上运行了Chrome 73.0.3683.86和Rails 5.2.2。
如果要使用:selenium_chrome
作为默认驱动程序,则可以设置Capybara.default_driver = :selenium_chrome
。
至于example.com
问题,这是因为您正在访问posts_url
并且在测试环境中将Rails的默认主机名设置为example.com
。 您可以访问posts_path
,这将允许posts_path
将主机名默认为localhost,或者更新您的测试环境配置,以便url帮助程序生成您期望的url。
您需要设置app_host
配置,例如app_host
Capybara.app_host = ...
在这里查看完整的文档
通常,您在spec_helper.rb
设置spec_helper.rb
配置,以便在所有规范中都启用它,例如:
Capybara.configure do |config|
config.current_driver = :selenium_chrome
config.app_host = ...
end
希望能回答您的问题?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.