rubyseleniumrspecproxybrowsermob-proxy

Why is Browsermob-Proxy-rb w/ Selenium failing to record HTTP traffic arbitrarily?


I've been working with the Browsermob Proxy, via the current release of the browsermob-proxy.rb gem, to record several RSpec tests.

Unfortunately, it seems to be randomly failing to record traffic. On some test runs, the traffic is recorded perfectly. On others, without any modification of the code or the environment, the har files it outputs are all but empty. The tests themselves pass without issue, but nothing is being recorded. The har files are always generated.

I know that all of my code is being run, via my logs. So I'm unsure where exactly the problem lies. I've included some of the relevant code below:

Har saving logic (this is called by an after(:all) block at the end of the tests)

  def save_har(test_name,har_dir)
    if @proxy.nil?
      log_proxy_helper "Proxy instance already stopped, cannot save 'har' file"
    else
      log_proxy_helper "Saving har to #{har_dir}"
      filename = ("#{test_name}.har").gsub(' ','_')
      path = File.expand_path(File.join(har_dir , filename))
      if path.match(/^C:\//)
        path.gsub!(/\//, "\\")
      end
      begin
        @proxy.har.save_to path
      rescue Exception => e
        log_proxy_helper "Error: #{e}"
      end
      log_proxy_helper "Saved har to #{path}"
    end
  end

The proxy-creation logic (in the same file and context as above)

  def start_proxy
    begin
      log_proxy_helper "Starting proxy using port #{proxy_port}"
      logs_path = 'logs/'
      logs_path = ENV['WORKSPACE'] + '/logs/' if ENV['WORKSPACE']
      @proxy_server = BrowserMob::Proxy::Server.new(browsermob_proxy_path,{:port => 9090,:log => false,:logs_path => logs_path, :use_little_proxy => false, :timeout => 30})
      begin
        @proxy_server.start
      rescue Exception => e
        log_proxy_helper "Error: #{e}"
      end
      @proxy = @proxy_server.create_proxy proxy_port
      @proxy.new_har(:capture_binary_content => true)
    log_proxy_helper "Server Started"
    end if check_proxy_configured 'Can\'t start proxy'
  end

An example of one of the almost-empty har files generated:

{
    "log": {
        "version": "1.2",
        "creator": {
            "name": "BrowserMob Proxy",
            "version": "2.1.0-beta-3-littleproxy",
            "comment": ""
        },
        "pages": [
            {
                "id": "Page 0",
                "startedDateTime": "2015-11-17T16:18:16.695-05:00",
                "title": "Page 0",
                "pageTimings": {
                    "onLoad": 2916,
                    "comment": ""
                },
                "comment": ""
            }
        ],
        "entries": [],
        "comment": ""
    }
}

Any ideas?

EDIT: After some more testing, I found that this only happens with Internet Explorer; Firefox and Chrome record properly


Solution

  • In case anyone searching finds this, it turned out that I had incorrectly set the proxy in the Internet Explorer Webdriver instantiation. A simple mistake leading to a lot of confusion.