[英]Capybara selenium_chrome going to example.com instead of localhost
I am setting up Cucumber tests in a Rails project. 我正在Rails项目中设置Cucumber测试。 Everything works fine when I use the default driver; 使用默认驱动程序时,一切正常。 but, when I try to use the :selenium_chrome
driver, the browser tries to load example.com
instead of the local Rails server. 但是,当我尝试使用:selenium_chrome
驱动程序时,浏览器尝试加载example.com
而不是本地Rails服务器。 Any idea what I'm missing? 知道我缺少什么吗?
My steps look like this: 我的步骤如下所示:
Before do |scenario|
Capybara.current_driver = :selenium_chrome
end
When(/^I visit the posts page$/) do
visit posts_url
end
When I run the features, I can see that the rails server gets launched: 运行这些功能时,可以看到启动了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
But, the Chrome window that pops up is attempting to access http://www.example.com/posts
instead of http://127.0.0.1:62056/posts
但是,弹出的Chrome窗口正在尝试访问http://www.example.com/posts
而不是http://127.0.0.1:62056/posts
Am I missing a configuration step somewhere? 我在某处缺少配置步骤吗?
On a related note: If I want to run all my tests using Selenium, should I have to put the Capybara.current_driver
line in a Before
block? 相关说明:如果要使用Selenium运行所有测试,是否必须将Capybara.current_driver
行放在Before
块中? I tried just adding it to features/support/env.rb
, but it didn't seem to have any effect. 我尝试只将其添加到features/support/env.rb
,但似乎没有任何效果。
I have Chrome 73.0.3683.86 and Rails 5.2.2 running on MacOS 10.14.4. 我在MacOS 10.14.4上运行了Chrome 73.0.3683.86和Rails 5.2.2。
If you want to use :selenium_chrome
as the default driver, you can set Capybara.default_driver = :selenium_chrome
. 如果要使用:selenium_chrome
作为默认驱动程序,则可以设置Capybara.default_driver = :selenium_chrome
。
As to the example.com
issue that's because you're visiting posts_url
and have your Rails default hostname set to be example.com
in your test environment. 至于example.com
问题,这是因为您正在访问posts_url
并且在测试环境中将Rails的默认主机名设置为example.com
。 You can either visit posts_path
which will allow Capybara to default the hostname to localhost - or update your test environment config so the url helpers produce the urls you expect. 您可以访问posts_path
,这将允许posts_path
将主机名默认为localhost,或者更新您的测试环境配置,以便url帮助程序生成您期望的url。
You need to set Capybara app_host
config eg Capybara.app_host = ...
. 您需要设置app_host
配置,例如app_host
Capybara.app_host = ...
See full docs here 在这里查看完整的文档
Generally, you set Capybara config inside spec_helper.rb
so that it's enabled in all specs eg: 通常,您在spec_helper.rb
设置spec_helper.rb
配置,以便在所有规范中都启用它,例如:
Capybara.configure do |config|
config.current_driver = :selenium_chrome
config.app_host = ...
end
Hope that answers your questions? 希望能回答您的问题?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.