Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Minimal Capybara / Poltergeist test returning empty page

It appears I'm retracing the steps taken in the SO post: Capybara, Poltergeist and Phantomjs and giving an empty response in body. (Mark this as a duplicate if you want, but I'm including a minimal standalone test case and version numbers.)

questions

Am I doing anything obviously wrong? Is there another minimal test I can run that might help isolate the problem?

file: pgtest.rb
require 'rubygems'
require 'capybara'
require 'capybara/dsl'
require 'capybara/poltergeist'

module PGTest
  include Capybara::DSL
  extend self

  def test
    Capybara.register_driver :poltergeist do |app|
      Capybara::Poltergeist::Driver.new(app)
    end

    Capybara.current_driver = :poltergeist
    session = Capybara::Session.new(:poltergeist)

    visit "http://www.google.com"
    sleep 5
    puts session.html
  end
end

PGTest.test

When invoked as follows, it prints an empty page:

$ ruby pgtest.rb
<html><head></head><body></body></html>

environment

  • OS X 10.8.5 (12F45)
  • ruby 2.0.0p247 (2013-06-27) [x86_64-darwin12.4.0]
  • phantomjs 1.9.2
  • capybara (2.1.0)
  • poltergeist (1.4.1)

absolving phantomjs

It's worth noting that I can use phantomjs extract the html from www.google.com:

file: pjs_dump.js
var page = require('webpage').create();
page.open("http://www.google.com", function () {
    var html = page.evaluate(function () {
        return document.documentElement.outerHTML;
    });
    console.log(html);
    phantom.exit();
});

When I run 'phantomjs pjs_dump.js', it prints the html from www.google.com, so phantomjs appears to be working properly.

like image 870
fearless_fool Avatar asked Oct 20 '13 11:10

fearless_fool


1 Answers

This does the trick for me:

require 'rubygems'
require 'capybara'
require 'capybara/dsl'
require 'capybara/poltergeist'

Capybara.run_server = false
Capybara.current_driver = :poltergeist

class PGTest
  include Capybara::DSL

  def test
    visit "http://www.google.com"
    puts page.body
  end
end

PGTest.new.test
like image 110
Jeff Devine Avatar answered Oct 05 '22 23:10

Jeff Devine