Sha256: 2b9b6f47bfb3034f18dc5c6afbf61f598347d55e3a5412a4e969970d0b2ec6c2

Contents?: true

Size: 1.25 KB

Versions: 1

Compression:

Stored size: 1.25 KB

Contents

require 'phantomjs'

class PhantomRubyBrowser

  attr_reader :phantomjs, :location

  def initialize(location)
    @location = location
    @phantomjs = Phantomjs
  end

  def content
    execute "var page = require('webpage').create();
    page.open('#{@location}', function() {
      setTimeout(function(){var js = page.evaluate(function () {
        return document;
      });
      console.log(js.all[0].outerHTML);
      phantom.exit();}, 1000);
    });"
  end

  def content_after_click(link_selector = 'a')
    execute "var page = require('webpage').create();
    page.open('#{@location}', function() {
      var coords = page.evaluate(function(){
        return document.querySelector('#{link_selector}').getBoundingClientRect();
      });
      page.sendEvent('click', coords.left, coords.top);
      var js = page.evaluate(function () {
        return document;
      });
      console.log(js.all[0].outerHTML);
      phantom.exit();
    });"
  end

  private

  def execute(commands)
    result = ""
    steps_file = Tempfile.new('prb_steps')
    begin
      steps_file.write(commands)
      steps_file.flush
      result = @phantomjs.run(steps_file.path)
    ensure
      steps_file.close
      steps_file.unlink
    end
    result
  end

  def prepare_steps

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
phantom-ruby-browser-0.0.1 lib/phantom_ruby_browser.rb