Capybara selenium_chrome going to example.com instead of localhost - ruby-on-rails

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?

Related

Firefox is default but still get WebDriverError: unable to connect to chromedriver 127.0.0.1:9515

I'm setting up Capybara for the first time and it seems to be calling Chrome instead of Firefox by default.
At first I was getting the webdriver error:
Selenium::WebDriver::Error::WebDriverError:
unable to connect to chromedriver 127.0.0.1:9515*
with associated stacktrace
https://pastebin.com/TW5NWJgu
I was able to clear this by adding a gem 'chromedriver-helper' and the test now opens with chromium.
I also tried adding this to both spec_helper and rails_helper:
Capybara.register_driver :selenium do |app|
Capybara::Selenium::Driver.new(app, browser: :firefox)
end
I was able to confirm that rails was able to successfully call firefox because the following command in rails_helper does successfully launch Firefox (but does not take any further action) when I start the test (as per comments, I later removed this command).
RSpec.configure do |config|
driver = Selenium::WebDriver.for :firefox
end
I'm getting the same error on a separate machine and on a different rails app (also didn't have geckodriver set-up on the first pass of Capybara)
My understanding is that Capybara should call Firefox by default. There seems to be a second configuration somewhere that I can't find. Does anyone have an idea of where I might find the line that is calling chrome?
From the log file you provided we can see that you're using Rails 5.1, RSpec 3.8 and Capybara 2.18. Since the log also includes "actionpack-5.1.6/lib/action_dispatch/system_testing/driver.rb" we can tell that you're writing system tests/specs (through rspec-rails). The driver used by system tests is controlled by the driven_by method as documented in the RSpec system spec docs and by default uses the Rails registered :selenium driver which is configured to use Chrome. As documented in the Rails System Test docs you can switch to Firefox by specifying
driven_by :selenium, using: :firefox
Additionally, Capybara 2.18 is pretty much obsolete at this point. You probably want to update to the latest version (3.6 as of now) if you plan on using the latest versions of Firefox/Chrome.

Rspec using test, Capybara using development database

I have an interesting problem. I'm using Rspec for test driven development and Capybara with Poltergeist for acceptance testing. Oh, and FactoryGirl, too. Rspec and FactoryGirl is operating against the test database, which is what I want. The problem is that acceptance tests are operating against the development database.
This causes simple tests like the following to fail:
my_class = FactoryGirl.create(:my_class)
visit my_classes_path
expect(page).to have_content(my_class.title)
I've even checked screenshots along the way using:
page.save_screenshot("screenshot#{__FILE__}_#{__LINE__}.png")
SOLUTION
So apparently Capybara was attempting to use the same URL and port that is initialized in my local environment when I kickoff "rails server". Specifying a different port in my Capybara configuration did the trick as seen below:
Capybara.configure do |c|
c.run_server = true
c.javascript_driver = :poltergeist
c.default_driver = :poltergeist
c.server_port = 7000
c.app_host = "http://localhost:#{c.server_port}"
end
For normal use you shouldn't have to lock to a specific port or set app_host. If app_host isn't set then Capybara defaults to http://#{Capybara.server_host}:#{Capybara.server_port} which by default is http://127.0.0.1:<the port capybara runs the server on> . If you need to use localhost instead of 127.0.0.1 (because of IPv6 or something) then just set
Capybara.server_host = 'localhost'
instead of app host and fixing the port. app_host is really for when you're trying to test an external site, or you need to access subdomains to test your app - and fixing the port is really intended for issues with firewalls, etc.

Cannot specify no_proxy for Capybara with poltergeist

I there, I am a bit stuck with this...
I can't find documentation on how to tell Capybara/Poltergeist with phantomjs to avoid the proxy and go directly to the specified IP.
Host machine (Fedora 20) config? Already tried setting no_proxy
Or, Application config?
A bit of context:
I am making a few web tests using Capybara with Poltergeist/PhantomJS. Unfortunately, my company decided that any request for an internal network that hits the proxy won't be forwarded anymore. So, since I am testing between two different machines in the company's intranet, putting the env variable no_proxy should be enough. But no...
Well... after digging a bit more on the PhantomJS github API Reference page I found the answer.
There is an option --proxy-type=[http|socks5|none] that can be passed to phantomjs command line or in the initialization of Capybara with poltergeist like this:
Capybara.run_server = false
Capybara.javascript_driver = :poltergeist
Capybara.default_wait_time = 60
Capybara.register_driver :poltergeist do |app|
Capybara::Poltergeist::Driver.new(app, { :phantomjs_options => ['--ignore-ssl-errors=yes', '--proxy-type=none'] })
end

Port of the Rails app when running Cucumber tests

Is there a way to get, in the test, the port in which the rails app is running during cucumber test? I tried Capybara.server_port but that's nil.
Thanks.
When using the selenium driver, the port can be found on:
Capybara.current_session.driver.rack_server.port
and when using the webkit driver, it can be found on:
Capybara.current_session.driver.server_port
Alternative, you can set
Capybara.server_port
to a known value and use that.
My understanding is that if you're using rack-test, the default Capybara driver, then there's isn't actually any real web server running to make requests to.
If you want to view your app as Cucumber/Capybara would, then you'd need to start it up manually on a chosen port:
$ RAILS_ENV=test rails s -p 4000
And then have something like this in env.rb:
Capybara.configure do |config|
config.run_server = false
config.app_host = "http://localhost:4000"
end

Running capybara with nginx

Is is possible to run capybara with nginx and passenger? instead or webrick? Capybara is installed with cucumber in a rails app.
It is easy - the only thing you have to do is to switch your mind - neither capybara nor cucumber are not tied to local environment you can test application that is located in internet and it will not care about it - you can even test google.com if you want.
For your particular problem you'll have to set
Capybara.run_server = false
Capybara.server_port = 8000 # or whatever port is your instance of nginx is configured to serve
Capybara.app_host = 'http://www.google.com' # if your instance is running on remote machine, else just drop it and capybara will use localhost
You can easily control restarting of your application using cucumber hooks, you can configure it to restart before each test or before test suite. (See cucumber wiki) Within hook you'll have to issue FileUtils.touch tmp/restart.txt command. The same with database - you can manually setup hook to truncate it whenever it is needed (See database_cleaner gem)

Resources