Sha256: d86cf53d755afa1ac9965ea87241814d20556b4992474048f14aaa9029b9f69a

Contents?: true

Size: 1.27 KB

Versions: 3

Compression:

Stored size: 1.27 KB

Contents

require 'phantomjs'
require 'tempfile'

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

3 entries across 3 versions & 1 rubygems

Version Path
phantom-ruby-browser-0.1.1 lib/phantom_ruby_browser.rb
phantom-ruby-browser-0.1.0 lib/phantom_ruby_browser.rb
phantom-ruby-browser-0.0.2 lib/phantom_ruby_browser.rb