How do I configure Capybara to work with Poltergeist? - ruby-on-rails-3

I have an RSpec integration test that needs to execute some JavaScript. I've included Poltergeist and installed PhantomJS, but whenever I run the example, I get this error:
Failure/Error: page.execute_script("$('form')[0].submit();")
Capybara::NotSupportedByDriverError:
Capybara::Driver::Base#execute_script
The spec is:
require 'spec_helper'
describe "Signup", :type => :feature do
describe "workflow" do
it "ensures entry of contact information" do
visit 'signup/action'
# snip - use Capybara to fill out form elements
page.execute_script("$('form')[0].submit();")
page.should have_content("Name can't be blank")
page.should have_content("Email can't be blank")
# snip - use Capybara to fill out more form elements
page.execute_script("$('form')[0].submit();")
page.should have_content("Next page")
end
end
end
I think the problem is that I'm not sure how to indicate that Capybara should use Poltergeist as its JavaScript driver. The Poltergeist documentation says:
Installation
Add poltergeist to your Gemfile, and in your test setup add:
require 'capybara/poltergeist'
Capybara.javascript_driver = :poltergeist
But it doesn't say which file specifically it should go into. It also says:
Customization
You can customize the way that Capybara sets up Poltegeist via the following code in your test setup:
Capybara.register_driver :poltergeist do |app|
Capybara::Poltergeist::Driver.new(app, options)
end
But it's not clear to me if or when I would need to include this in my tests. And again, I'm not sure where to put it if I need to.
What am I missing?
Where do I need to put the configuration for Capybara and Poltergiest, and what exactly does it need to say (or how can I determine that for myself)?
Is there a step or piece of configuration that I missed?

Try putting js: true in your describe line. I know i had to do that for feature specs on an app at work:
describe "Signup", :type => :feature, :js => true do
I don't see any other configuration for it. Was a while ago when I set it up :)
You can just call the Capybara driver config methods once before your RSpec.configure block:
Capybara.default_selector = :css
Capybara.register_driver :poltergeist do |app|
Capybara::Poltergeist::Driver.new(app, :window_size => [1920, 1080], :phantomjs_logger => nil)
end
Capybara.javascript_driver = :poltergeist
RSpec.configure do |config|
Also be sure to use truncation not transaction with database cleaner. Poltergeist runs on a separate thread, so you'll likely have weird db issues if you use transactional.
Edit
Ah the js true thing is mentioned under here: https://github.com/jnicklas/capybara#using-capybara-with-rspec in the capybara readme.

Related

Capybara selenium xvfb throwing end of file error after recent update of Ubuntu 16.04

Currently in my Ruby on Rails application cucumber test are being run with the capybara gem and using selenium.
Recently after a system update to Ubuntu 16.04 the tests started failing with EOFError: end of file reached errors
Additionally, it also occasionally includes the following error.
session not created exception
from unknown error: Runtime.executionContextCreated has invalid 'context': {"auxData": "frameId":"14314.1","isDefault":true},"id":1,"name":"","origin":"://"}
(Session info: chrome=54.0.2840.59)
(Driver info: chromedriver=2.21.371461 (633e689b520b25f3e264a2ede6b74ccc23cb636a),platform=Linux 4.4.0-43-generic x86_64) (Selenium::WebDriver::Error::SessionNotCreatedError)
This question also refers to something similar but with minitest.
This what I believe to be the relevant part of the capybara env.rb file:
require 'cucumber/rails'
require 'capybara'
require 'capybara/cucumber'
require 'capybara-screenshot'
require 'capybara-screenshot/cucumber'
require 'rails/test_help'
require 'minitest/rails'
require 'mocha/mini_test'
require 'headless'
require 'selenium-webdriver'
require 'rack_session_access/capybara'
require 'webmock/cucumber'
WebMock.allow_net_connect!
include Sprig::Helpers
ActionController::Base.allow_rescue = false
Dir.mkdir('test_result') unless File.exist?('test_result')
# Remove/comment out the lines below if your app doesn't have a database.
# For some databases (like MongoDB and CouchDB) you may need to use :truncation instead.
begin
DatabaseCleaner.strategy = :truncation, { only: [] }
DatabaseCleaner.clean_with :truncation, { only: [] }
rescue NameError
raise "You need to add database_cleaner to your Gemfile (in the :test group) if you wish to use it."
end
Capybara.register_driver :selenium do |app|
client = Selenium::WebDriver::Remote::Http::Default.new
client.timeout = 15
Capybara::Selenium::Driver.new(app, browser: :chrome, http_client: client)
end
Capybara.default_driver = :selenium
Capybara.javascript_driver = :selenium
Capybara.raise_server_errors = true
Capybara.default_selector = :css
Capybara.default_max_wait_time = 15
Capybara.save_and_open_page_path = 'tmp/capybara'
I've looked over at a thread in the capybara-webkit gem that looks related, but I'm not actually using webkit.
Any help would be appreciated.
Turns out I'm using a gem called chromedriver-helper that was using rbenv to override the version of chromedriver that was actually being used by capybara and selenium to run the tests. The gem readme said to try running chromedriver-update in the context of the rails app, which cleared everything up.

No method error_messages selenium and capybara-webkit

When using capybara-webkit I have come across the issue of selenium not having an error_messages method, although, the documentation for capybara-webkit says that I should be able to access it through page.driver.error_messages once my flag on the scenerio is set with :js => true. However, trying to access it, it throws:
NoMethodError:
undefined method `error_messages' for #<Capybara::Selenium::Driver:0xc501fc0>
and I have tried to access the messages using:
page.should_not have page.driver.error_messages
I have also tried:
page.should_not :have_errors
as well as:
page.should_not have_errors
but with not avail.
My understanding is that error_messages is a method included in the capybara-webkit driver. It is not available for the selenium-webdriver driver.
Based on the exception you are getting, you are using the selenium-webdriver. By default, Capybara uses selenium-webdriver when :js => true is specified.
You likely want to change your javascript_driver to use :webkit (as mentioned in the usage documentation - https://github.com/thoughtbot/capybara-webkit).
Capybara.javascript_driver = :webkit

How do you run a single capybara test that uses the selenium driver?

There are pleanty of examples where you use rspec, selenium, and capybara to run tests that start like this:
it "does something", :js => true do
and you can run everything by running "rake spec", but how do you run a single capybara/selenium test? I cannot figure it out for the life of me.
You can tag an example:
it "does something", :js => true, :focus => true do
$ rspec . --tag focus
Or you can specify the line number of the spec:
$ rspec my_spec.rb:37

Best way to debug rspec integration tests with capybara?

I have an rspec test failing, but in development, when I perform the actions myself, the page renders correctly.
Here's the snippet:
describe "sign up process" do
let (:admin1) { FactoryGirl.create(:admin) }
describe "with valid information"
before do
visit new_admin_registration_path
fill_in "Email", with: admin1.email
fill_in "Password", with: admin1.password
fill_in "Password confirmation", with: admin1.password_confirmation
click_button "Sign up"
end
it { should have_selector('div.alert') }
end
It's not finding that selector at all, but I confirmed it exists when viewing the source using the development environment.
What I'd like to be able to do is inspect the page that's rendering to see what I'm missing. Is there a way to do that?
Capybara has a method save_and_open_page which will open a new browser window with the current state of the page (it requires the launchy gem).
For example:
before do
...
click_button "Sign up"
save_and_open_page
end
this is as easy as using the debugger or something different like pry in your spec.
i personally prefer pry, cause installing the debugger was kind of a hassle everytime i tried it with ruby 1.9...
with pry you would just add a require "pry" in your spec_helper.rb and then write a binding.pry the line before the problem occurs. you then have an irb session attached to your test.

How can Rails automatically rescue from ActiveRecord::RecordNotFound in development mode?

I am using the following piece of code on my ApplicationController :
rescue_from ActiveRecord::RecordNotFound, :with => :not_found
def not_found
render :nothing => true, :status => :not_found
end
in order make Rails respond with correct status code and not raise an exception
in development mode.
I know that how-to-handle-errors-like-404-500-in-rails3 explains how Rails works in production mode. In other words, what I am trying to do in development mode is done without any piece of code in production mode.
How can I make development mode behave like production mode and get rid of the above piece of code?
Note that I am using Rails 3.2.3
Perhaps you want to turn this off?:
config.consider_all_requests_local = false
This is set to true in config/environments/development.rb by default.