简体   繁体   English

水豚selenium_chrome转到example.com而不是localhost

[英]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.

相关问题 Capybara.current_driver =:chrome与:selenium_chrome - Capybara.current_driver = :chrome versus :selenium_chrome 在Rails / Capybara / Poltergeist规范中使用url_for将驱动程序发送到example.com而不是app - Using url_for in Rails/Capybara/Poltergeist spec sends the driver to example.com instead of the app Capybara Webkit在RSpec测试中将我重定向到example.com - Capybara webkit redirects me to example.com in RSpec tests request.url给了我example.com/login而不是example.com/feed - request.url is giving me example.com/login instead of example.com/feed Capybara,Devise,CanCan和RSpec集成测试:有效登录302重定向到example.com - Capybara, Devise, CanCan and RSpec integration tests: valid sign in 302 redirects to example.com 尝试使用Capybara,Cucumber,RSpec和Devise登录时重定向到example.com - redirect to example.com when trying to sign in with Capybara, Cucumber, RSpec and Devise 用Rails应用强制域的最佳方法是什么? 例如:example.com,而不是www.example.com - what's the best way to force a domain with a rails app? ex: example.com instead of www.example.com 将网址帮助程序路由到example.com - Routes URL helper leading to example.com example.herokuapp.com重定向到https://example.com - example.herokuapp.com redirection to https://example.com Rails如何将example.com/post/1更改为example.com/blog/post/1 - Rails how to make example.com/post/1 to example.com/blog/post/1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM