Rails Phantomjs, poltergeist, and Capybara not playing well together
Asked Answered
C

3

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.

Conduction answered 26/11, 2012 at 19:49 Comment(1)
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.Conduction
E
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?

Erminiaerminie answered 27/11, 2012 at 14:43 Comment(2)
Yes, it does... In fact, I have found that if I leave a rails server running, and modify your above code to visit "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!!Conduction
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!Conduction
N
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.

Nullipore answered 5/12, 2012 at 11:0 Comment(3)
I will try this out and let you know! Thanks!Conduction
Hmmm. Same result... @Nullipore I am assuming that you set your .app_host to your LOCAL machine? Where the test suite is running?Conduction
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 lvh.me (which always points to 127.0.0.1) as more common solution.Nullipore
A
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!
Adamo answered 26/11, 2012 at 22:2 Comment(8)
Great suggestion, but I have tried that (with debug both false and true) and no difference in the problem. Continually blank screen capture.Conduction
try to add sleep 5 and then make the screenshot.. maybe it needs some time to displayAdamo
Another great suggestion, but no matter how long I sleep, it just comes up empty.Conduction
Interesting note, though: When I add that .current_driver line, the whole process takes longer... But same result.Conduction
Note: There is two r for current_driverInunction
Haha so funny, after one year i tried to solve the same problem again.. and found my own answer ^^Adamo
@Lichtamberg I also face the same problem now. Can you tell me your solution? Thank before hand. :)Puentes
I just had to register the driver via Capybara.current_driver... I think at least.. Already some time ago :)Adamo

© 2022 - 2024 — McMap. All rights reserved.