8

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.

Nakilon
  • 34,866
  • 14
  • 107
  • 142
Dave Collins
  • 1,077
  • 1
  • 15
  • 23
  • 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 Nov 27 '12 at 01:03

3 Answers3

11

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?

theSociableme
  • 842
  • 1
  • 10
  • 23
  • Yes, it does... In fact, I have found that if I leave a rails server running, and modify your above code to visit "http://localhost:5000" then it does indeed render that page faithfully. However, when I add include Capybara::DSL into my other test specs they are crashing at different locations... Troubleshooting now. Thanks!! – Dave Collins Nov 27 '12 at 16:22
  • I'm going to mark this answer as correct because it got me past the issue of "cannot make a screenshot from within rspec". I've still got Capybara::DSL troubleshooting, but that is a separate issue. Thanks! – Dave Collins Nov 28 '12 at 13:11
6

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.

Eugene
  • 448
  • 4
  • 12
  • I will try this out and let you know! Thanks! – Dave Collins Dec 05 '12 at 21:46
  • Hmmm. Same result... @Eugene I am assuming that you set your .app_host to your LOCAL machine? Where the test suite is running? – Dave Collins Dec 06 '12 at 17:35
  • Oh.. I've just noticed your question, sorry. In case if it is still actual - yes, tests are running on local machine, but you can use http://lvh.me (which always points to 127.0.0.1) as more common solution. – Eugene Jan 11 '13 at 15:32
2

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!
BvuRVKyUVlViVIc7
  • 11,641
  • 9
  • 59
  • 111