Rails Phantomjs, poltergeist, and Capybara not playing well together

Dave Collins picture Dave Collins · Nov 26, 2012 · Viewed 11.6k times · Source

Working in a rails 3.1.2 project (mac OS X), I have PhantomJS properly installed (I can run code like the following and it works perfectly, accurately grabbing the title of the page and saving an accurate screenshot)

try_phantom.coffee

page = require('webpage').create()
page.open 'http://localhost:5000/parties/onetestparty', (status) ->
    title = page.evaluate -> document.title
    console.log "Title: #{title}"
    page.render './log/javascript_screenshot.png'
    phantom.exit()

However, when I attempt to use capybara/poltergeist in rspec as follows:

spec_helper.rb

require 'capybara/poltergeist'
Capybara.javascript_driver = :poltergeist

and then using a spec with a call requiring javascript:

parties_spec.rb

        it "should allow a simple screenshot", js: true do
            visit "/"
            page.driver.render('./log/screen_Home.png', :full => true)
        end

It doesn't appear that my javascript is being rendered, and also the screenshot is always blank!

I've tried the debugger, but that seems to also bring up a blank HTML page (just html with empty head and body tags)

I'm pretty sure the problem is either the interface between capybara and poltergeist or (more likely) poltergeist and phantomjs. Here are the versions of the relevant gems:

capybara 1.1.3
capybara-webkit 0.13.0
poltergeist 1.0.2
phantomjs is 1.7.0

Not sure how to troubleshoot further... Any help would be appreciated.

Answer

theSociableme picture theSociableme · Nov 27, 2012

Create a very simple test and see what happens.

simple_spec.rb

require 'spec_helper'
require 'capybara/poltergeist'
include Capybara::DSL
Capybara.javascript_driver = :poltergeist

describe 'some stuff which requires js', :js => true do
  it 'will take a screenshot' do
    visit("http://google.com")
    page.driver.render('./file.png', :full => true)
  end
end

Does that get you an image of Google?