Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails Phantomjs, poltergeist, and Capybara not playing well together

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.

like image 711
Dave Collins Avatar asked Nov 26 '12 19:11

Dave Collins


3 Answers

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?

like image 173
theSociableme Avatar answered Nov 14 '22 14:11

theSociableme


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.

like image 34
Eugene Avatar answered Nov 14 '22 12:11

Eugene


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!
like image 2
BvuRVKyUVlViVIc7 Avatar answered Nov 14 '22 14:11

BvuRVKyUVlViVIc7