Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I configure Capybara to work with Poltergeist?

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?
like image 632
Kevin Avatar asked Jun 25 '14 18:06

Kevin


1 Answers

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.

like image 135
agmcleod Avatar answered Oct 19 '22 15:10

agmcleod