Kapibara, Poltergeist i Phantomjs i dając pustą odpowiedź w ciele

Dostaję pusty dokument od fantomów. Próbuję użyć Capybary i Poltergeista do ustawienia sterownika fantomowego dla Kapibary.

Utworzyłem moduł w następujący sposób i umieściłem go w pliku, który wymaga połączenia.

require 'capybara/poltergeist'

  module Parser
    module JSParser
      include Capybara

      # Create a new PhantomJS session in Capybara
      def new_session
        # Register PhantomJS (aka poltergeist) as the driver to use
        Capybara.register_driver :poltergeist do |app|
          Capybara::Poltergeist::Driver.new(app, :debug => true)
        end

        # Use XPath as the default selector for the find method
        Capybara.default_selector = :xpath
        Capybara.javascript_driver = :poltergeist
        Capybara.current_driver = :poltergeist
        # Start up a new thread
        @session = Capybara::Session.new(:poltergeist)

        # Report using a particular user agent
        @session.driver.headers = { 'User-Agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X)' }

        # Return the driver's session
        @session
      end

      # Returns the current session's page
      def html
        @session.html
      end

    end
  end

Następnie załaduj stronę w następujący sposób:

class Loader
  include Parser::JSParser

  def load_page
    new_session
    visit "http://www.smashingmagazine.com"
    #let phantomjs take its time
    sleep 5
    puts "html=#{html}"  
  end
end

Następnie w końcu wywołując stronę load_page

Loader.new.load_page

Oto odpowiedź debugowania z poltergeist

poltergeist [1364758785355] state default -> loading
{"response"=>true}
{"name"=>"visit", "args"=>["http://www.smashingmagazine.com"]}
poltergeist [1364758794574] state loading -> default
{"response"=>{"status"=>"success"}}
{"name"=>"body", "args"=>[]}
{"response"=>"<html><head></head><body></body></html>"}

Jak widać, odpowiedź jest tylko pustym dokumentem zawierającym tylko znaczniki HTML, Head i Body, ale nic w tagu Body.

Co złego robię? Obserwując ruch sieciowy, otrzymuję pełną odpowiedź od hosta (w tym przypadku smashingmagazine.com). Po powrocie odpowiedzi nie wiem, co się dzieje. Czasami phantomjs również się zawiesza, a przy innych okazjach przechodzi przez puste ciało. Oto ostatnia linia, która jest drukowana na STDERR, gdy zawiesza się fantom

PhantomJS client died while processing {"name":"visit","args":["http://www.smashingmagazine.com"]}

questionAnswers(3)

yourAnswerToTheQuestion