Sha256: e045e154592042fab1c299b91b87cb5c74988914497afffb651482c97c7e3801

Contents?: true

Size: 1.4 KB

Versions: 1

Compression:

Stored size: 1.4 KB

Contents

require "undead/command"
require "undead/errors"
require "json"
require "time"

module Undead
  class Browser
    ERROR_MAPPINGS = {
      'Poltergeist.JavascriptError'   => Undead::JavascriptError,
      'Poltergeist.FrameNotFound'     => Undead::FrameNotFound,
      'Poltergeist.InvalidSelector'   => Undead::InvalidSelector,
      'Poltergeist.StatusFailError'   => Undead::StatusFailError,
      'Poltergeist.NoSuchWindowError' => Undead::NoSuchWindowError,
    }

    attr_reader :server, :client, :logger

    def initialize(server, client, logger = nil)
      @server = server
      @client = client
      @logger = logger
    end

    def visit(url)
      command 'visit', url
    end

    def body
      command 'body'
    end

    def js_errors=(val)
      @js_errors = val
      command 'set_js_errors', !!val
    end

    def debug=(val)
      @debug = val
      command 'set_debug', !!val
    end

    def command(name, *args)
      cmd = Undead::Command.new(name, *args)
      log cmd.message

      response = server.send(cmd)
      log response

      json = JSON.load(response)

      if json['error']
        klass = ERROR_MAPPINGS[json['error']['name']] || Undead::BrowserError
        raise klass.new(json['error'])
      else
        json['response']
      end
    rescue Undead::DeadClient
      restart
      raise
    end

    private

    def log(message)
      logger.puts message if logger
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
undead-0.2.0 lib/undead/browser.rb