How do I configure Capybara to work with Poltergeist?
Asked Answered
H

1

10

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?
Herbarium answered 25/6, 2014 at 18:19 Comment(0)
P
12

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.

Planar answered 27/6, 2014 at 19:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.