Capture Network Traffic with Selenium

Today we’re examining the captureNetworkTraffic selenium command, a little known gem in Selenium’s command list.

Selenium offers a feature, captureNetworkTraffic, which allows you to intercept the network traffic as seen by the browser running your test.
The response includes headers, status codes, timings and ajax requests. It’s especially useful if you’re debugging requests in your browser.

An example on how to use this in ruby:


require "rubygems"
gem "selenium-client"
require "selenium/client"
gem 'test-unit'
require 'test/unit'

gem "testingbot"
require "testingbot"

class ExampleTest < TestingBot::TestCase
  attr_reader :browser

  def setup
    @browser = Selenium::Client::Driver.new \
        :host => "hub.testingbot.com",
        :port => 4444,
        :browser => "firefox",
        :version => "8",
        :platform => "WINDOWS",
        :url => "http://www.google.com",
        :timeout_in_second => 60

    browser.start_new_browser_session(:captureNetworkTraffic => true)
  end

  def teardown
    browser.close_current_browser_session
  end

  def test_command
    browser.open "/"
    p browser.browser_network_traffic
  end
end

This will open Google in Firefox 8 and return the network traffic. An example of a response would be:

"403 GET http://localhost:5555/favicon.ico1333 bytes 94ms 
(2011-12-21T15:53:06.352+0100 - 2011-12-21T15:53:06.446+0100
 Request Headers - Host => localhost:5555 - 
User-Agent => Mozilla/5.0 (Windows NT 6.1; WOW64; rv:8.0.1) Gecko/20100101 
Firefox/8.0.1 - Accept => image/png,image/*;q=0.8,*/*;q=0.5 - 
Accept-Language => en-us,en;q=0.5 - Accept-Encoding => gzip, deflate - Accept-Charset => ISO-8859-1,utf-8;q=0.7,*;q=0.7 - 
Proxy-Connection => keep-aliveResponse Headers - Date => Wed, 21 Dec 2011 14:53:06 GMT - 
Server => Jetty/5.1.x (Windows 7/6.1 x86 java/1.6.0_26 - Content-Type => text/html - 
Content-Length => 1333 - Via => 1.1 (jetty)

This command is available on our Grid and can be used to debug requests and calculate how long a request takes.

No Comments

Leave a Reply

Your email address will not be published. Required fields are marked *