6

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.

Community
  • 1
  • 1
fearless_fool
  • 33,645
  • 23
  • 135
  • 217

2 Answers2

1

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
Jeff Devine
  • 596
  • 2
  • 8
  • 3
    Indeed, that works. Now it would be interesting to know what the salient difference is between your example and mine. Even though you haven't answered "what am I doing wrong", you get the check mark. – fearless_fool Nov 04 '13 at 12:42
0

I had similar issue when testing rails application with rspec, capybara and poltergeist. Spec failed when trying to find element when the body was empty.

It turned out, that the problem was in my rails code. It caused an error, the page did not render, and capybara received an empty page. And I had nothing in stack trace.

Timothy Kovalev
  • 315
  • 1
  • 7