8
votes

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.

3
Note: One thing I have also tried is telling poltergeist explicitly where my phantomjs can be found: :phantomjs => "/usr/local/Cellar/phantomjs/1.7.0/bin/phantomjs". This doesn't change the result... However, I think it proves that phantomjs IS actually getting run, because if I give it a bogus path, I do get an error.Dave Collins

3 Answers

11
votes

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?

6
votes

I had the same problem, but in my case it was caused by using subdomains. Poltergeist was pointed to meaningless url (sort of "http://spb.:22789") so it receives nothing but 'about:blank'.

To solve this issue I did following:

  1. Set app_host and server_port for Capybara

    Capybara.app_host = 'http://city.tulp.test:3003'
    Capybara.server_port = 3003

  2. Add dummy domain to /etc/hosts

Hope this helps.

2
votes

Maybe it helps if you register the driver?

Capybara.register_driver :poltergeist do |app|
  Capybara::Poltergeist::Driver.new(app, {debug: false})
end
Capybara.current_driver = :poltergeist # NOTE THE CURRENT_DRIVER, NOT JAVASCRIPT_DRIVER!