Sha256: d6b9784ff7851b7ebeeae9e1052c38cd4954bff6bb7b298bd3d39981c99945ec

Contents?: true

Size: 1.53 KB

Versions: 12

Compression:

Stored size: 1.53 KB

Contents

require 'selenium/webdriver/support'

module BrowserMob
  module Proxy

    #
    # WebDriver event listener that assumes the following:
    #
    # driver.get - new HAR
    # driver.click - new page
    # driver.navigate.back - new page
    # driver.navigate.forward - new page
    #

    class WebDriverListener < Selenium::WebDriver::Support::AbstractEventListener
      attr_reader :hars

      def initialize(client, opts = {})
        @client = client
        @hars = []

        @new_har_opts = {}
        @new_har_opts[:capture_headers] = true if opts[:capture_headers]
      end

      def reset
        @hars.clear
      end

      def before_navigate_to(url, driver)
        save_har unless @hars.empty? # first request
        @client.new_har("navigate-to-#{url}", @new_har_opts)
      end

      def before_navigate_back(driver = nil)
        name = "navigate-back"
        name << "-from-#{driver.current_url}" if driver

        @client.new_page name
      end

      def before_navigate_forward(driver = nil)
        name = "navigate-forward"
        name << "-from-#{driver.current_url}" if driver

        @client.new_page name
      end

      def before_click(element, driver)
        name = "click-element-#{identifier_for element}"
        @client.new_page name
      end

      def before_quit(driver)
        save_har
      end

      private

      def save_har
        @hars << @client.har
      end

      def identifier_for(element)
        # can be ovverriden to provide more meaningful info
        element.ref
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 2 rubygems

Version Path
browsermob-proxy-0.1.8 lib/browsermob/proxy/webdriver_listener.rb
browsermob-proxy-0.1.8.rc1 lib/browsermob/proxy/webdriver_listener.rb
browsermob-proxy-0.1.7 lib/browsermob/proxy/webdriver_listener.rb
browsermob-proxy-0.1.6 lib/browsermob/proxy/webdriver_listener.rb
browsermob-proxy-0.1.5 lib/browsermob/proxy/webdriver_listener.rb
browsermob-proxy-0.1.4 lib/browsermob/proxy/webdriver_listener.rb
browsermob-proxy-0.1.3 lib/browsermob/proxy/webdriver_listener.rb
browsermob-proxy-0.1.2 lib/browsermob/proxy/webdriver_listener.rb
browsermob-proxy-0.1.1 lib/browsermob/proxy/webdriver_listener.rb
browsermob-proxy2-0.1.0 lib/browsermob/proxy/webdriver_listener.rb
browsermob-proxy-0.0.9 lib/browsermob/proxy/webdriver_listener.rb
browsermob-proxy-0.0.8 lib/browsermob/proxy/webdriver_listener.rb