_ruby
Example
First we'll need to download BrowserMob Proxy from here.
Then we can wire up our test script to pull in our requisite libraries (e.g., selenium-webdriver
to drive the browser, rspec/expectations
and it's matchers for our assertion, and browsermob/proxy
to control the proxy server).
# filename: bandwidth_limit.rb
require 'browsermob/proxy'
require 'selenium-webdriver'
require 'rspec/expectations'
include RSpec::Matchers
Now let's configure the proxy server and bind it to a Selenium browser profile.
def configure_proxy
server = BrowserMob::Proxy::Server.new('./browsermob-proxy-2.1.0-beta-3/bin/browsermob-proxy')
server.start
@proxy = server.create_proxy
profile = Selenium::WebDriver::Firefox::Profile.new
profile.proxy = @proxy.selenium_proxy
profile
end
Now we can create setup
, teardown
, and run
methods to handle our test configuration.
def setup
@driver = Selenium::WebDriver.for :firefox, profile: configure_proxy
@driver.manage.timeouts.page_load = 300 # seconds
@proxy.limit(upstream_kbps: 20, downstream_kbps: 30)
end
def teardown
@driver.quit
@proxy.close
end
def run
setup
yield
teardown
end
In setup
we set the default timeout for Selenium to 5 minutes (e.g. 300
seconds) and specify an upload limit of 20
kilobits per second and a download limit of 30
kilobits per second (which is similar to a dial-up connection).
Now let's write our test. A simple page load and copy assertion will suffice (to make sure the page actually loaded).
run do
@driver.get 'http://the-internet.herokuapp.com'
expect(@driver.find_element(class: 'heading').text).to eql('Welcome to the Internet')
end
Expected Behavior
If you save this file and run it (e.g., ruby bandwidth_limit.rb
from the command-line) here is what will happen:
- Proxy server starts
- Proxy server session created
- Browser opens (connecting to the proxy server session with a large default timeout)
- Proxy bandwidth constraints applied
- Visit the page (which loads slowly)
- Page eventually loads
- Grab the text from the page heading
- Perform an assertion against the text to make sure it loaded
- Browser closes
- Proxy session closes
Summary
It may not be clear in browsermob-proxy-rb
what options and functionality are available since the library is light on documentation. But the code is easy to read and has good comments. It's worth looking at the client class to see what's available.
Alternatively, you can BrowserMob Proxy through it's Rest API. You can find out more about that on the BrowserMob Proxy GitHub page.
Happy Testing!