Sunday, June 16, 2019

Throttling network in Selenium tests using BrowserMob Proxy


Contents:

1) Introduction
2) Understanding network bandwidth and latency
3) Demo

1. Introduction

It's a good testing practice to avoid as many external dependencies as possible. We want to run everything on localhost and avoid test failures caused by things outside our application scope (environment issues, network problems, unavailable web services).

Unfortunately, our clients don't access applications in a sterile environment. Many of them use rather slow GPRS/3G Internet connection with significant latency caused by cellular interface. Hence we need to check how our systems cope with slow Internet connection.

When it comes to manual verification the choice is straightforward. Chrome Dev Tools have powerful network throttling functionalities. Sometimes though, we want to create an automated functional test which checks that everything works fine with limited bandwidth. How to do it?

The easiest solution is to set up a proxy through which we would pass network traffic. BrowserMob Proxy is a great tool that integrates nicely with Selenium and has very easy to use Java API. BrowserMob Proxy allows you to manipulate HTTP requests and responses, capture HTTP content, and export performance data as a HAR file. I've described the performance measuring feature in a separate post.

2) Understanding network bandwidth and latency

Before I'll show you how to throttle network in traditional demo let's clarify what exactly network latency and bandwidth. 

Latency is the amount of time it takes for data to travel from source to destination. It is dependent on the distance that data must travel through cords, networks, radio interfaces and the like to reach its destination. If you are an online gamer you probably check your ping. That's also your network latency.

Bandwidth is the rate of data transfer for a fixed period of time. The wider the communication band, the more data that can flow through it simultaneously. If you buy an Internet connection you most likely pay for maximum bandwidth (like 200 Mb/s).

3) Demo

As usual, I have prepared a demo on my GitHub project. I'm going to use FluentLenium Selenium extension.

At first, we would like to start a BrowserMob Proxy and change default configuration:
a) By settings 300ms network latency
b) By settings 1Mb/s (1 000 000 bytes/s) download and upload limit
c) By changing HTTP request user name header to custom value (this simplifies debugging)

Next, we need to create a Proxy object that will be used in tests. BrowserMob Proxy gives as a very useful static method createSeleniumProxy() which simplifies setup.  Next lines setup proxy IPv4 localhost urls.

Now we just need to modify Selenium Capabilities. Chrome example below.

And here is final test that checks my blog with 1Mb/s network bandwidth.


Happy proxying :)

No comments:

Post a Comment