2

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.

Zack
  • 6,232
  • 8
  • 38
  • 68

2 Answers2

2

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.

Thomas Walpole
  • 48,548
  • 5
  • 64
  • 78
  • Right on both counts. The `default_driver` vs `current_driver` issue was just a dumb mistake on my part. I would have never figured out the `posts_url` vs `posts_path` mistake. – Zack Apr 03 '19 at 20:17
0

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?

kasperite
  • 2,460
  • 1
  • 15
  • 17
  • 1
    `app_host` won't do anything since the user is visiting full urls (as opposed to paths) which will overwrite the hostname anyway, and setting the current driver in the configure block won't do anything in the default configuration because `current_driver` is reset to `default_driver` at the beginning of every test. – Thomas Walpole Apr 03 '19 at 20:14
  • How do I know what the host and port are? I assume Capybara is launching the rails server. What methods do I call to query which port was used? – Zack Apr 03 '19 at 20:23
  • @Zack `Capybara.current_session.server.port` should be the port once the server is started, or you can fix it by setting `Capybara.server_port`, or you can set `Capybara.always_include_port = true` to have Capybara always insert the port into urls where no specific port is provided. – Thomas Walpole Apr 04 '19 at 23:37