简体   繁体   中英

Capybara selenium_chrome going to example.com instead of localhost

I am setting up Cucumber tests in a Rails project. 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. 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:

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

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? I tried just adding it to features/support/env.rb , but it didn't seem to have any effect.

I have Chrome 73.0.3683.86 and Rails 5.2.2 running on MacOS 10.14.4.

If you want to use :selenium_chrome as the default driver, you can set 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. 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.

You need to set Capybara app_host config eg 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:

Capybara.configure do |config|
  config.current_driver = :selenium_chrome
  config.app_host   = ...
end

Hope that answers your questions?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM