Minimal Capybara / Poltergeist test returning empty page
Asked Answered
W

2

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.

Watthour answered 20/10, 2013 at 11:56 Comment(0)
C
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
Confidence answered 30/10, 2013 at 17:25 Comment(1)
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.Watthour
D
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.

Dannydannye answered 28/5, 2015 at 9:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.