Integrate BrowserMob Proxy with Selenium to Test Web Page Performance
While automating our Selenium tests, we might need to test our web page’s performance. While Selenium does not allow this out of the box, there are some tools we can use for this purpose. BrowserMob Proxy is one such tool. And the great thing about it is, you can easily pair it up with your Selenium tests. In this article, we are going to discuss the same. We will see how we can use BrowserMob Proxy with Selenium to test web page performance.
Apart from capturing performance data, BrowserMob can also manipulate browser behavior. And in case you want to simulate network traffic and latency, it lets you do that as well. All these great features comes at zero extra cost as it is an open source tool.
BrowserMob exports performance data in HAR format. We can run it in Embedded Mode or as a Standalone proxy server. But for our example here, we will run it in Embedded Mode. The reason for this is simple. We may not want to start BrowserMob manually every time our Selenium tests starts. So to avoid this manual intervention, we will start it using Embedded Mode.
Starting BrowserMob in Embedded Mode is simple. It’s just a matter of adding few lines of code. But in return, we get lot of benefits out of this. It provides a great deal of flexibility in terms of how we want to use it. For example, it can capture performance data for each of our tests.
OK, enough with the talks. We will see the practical part now.
Scenario: We will capture performance data of Google’s Privacy Policy web page. For that, we have already written a Selenium test method in Java. Now, we’ll add necessary code for BrowserMob proxy integration.
Using BrowserMob Proxy with Selenium
- Add below dependency in your Maven file. If you are not using Maven, you can download BrowserMob’s JAR and reference it in your project.
12345<dependency><groupId>net.lightbody.bmp</groupId><artifactId>browsermob-core</artifactId><version>2.1.2</version></dependency> - Since we will start BrowserMob proxy along with Selenium, we need to change our setUp method as below.
1234567891011public void setUp() throws Exception {server = new BrowserMobProxyServer();server.start();Proxy proxy = ClientUtil.createSeleniumProxy(server);DesiredCapabilities seleniumCapabilities = new DesiredCapabilities();seleniumCapabilities.setCapability(CapabilityType.PROXY, proxy);System.setProperty("webdriver.chrome.driver", "PATH TO CHROMEDRIVER");driver = new ChromeDriver(seleniumCapabilities);baseUrl = "https://www.google.co.in/";driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);} - At the end of our test, we will stop BrowserMob proxy with below code.
1234public void tearDown() throws Exception {server.stop();driver.quit();} - BrowserMob stores all the content in HAR file. So at the start of our test, we will need to create a new HAR file. We can do this with below code.
1server.newHar("Google Privacy Policy Testing"); - Also at the end of test, we need to store the HAR file. For this, just add below code at the end of your test.
12Har har = server.getHar();har.writeTo(new File("googlePrivacy.har")); - After adding these lines in your test, your test method should look like below.
12345678910111213public void googlePrivacy() throws Exception {// Start capturing HAR fileserver.newHar("Google Privacy Policy Testing");// Test methodserver.newHar("Google Privacy Testing");driver.get(baseUrl + "/?gfe_rd=cr&ei=bVDPVoe8HbDT8gfE-IGgDA&gws_rd=ssl");driver.findElement(By.linkText("Privacy")).click();// Store HAR fileHar har = server.getHar();har.writeTo(new File("googlePrivacy.har"));} - That’s it! You can now run your test. Once done, you should be able to see HAR file in your specified directory.
Viewing performance results
- Now that you have HAR file, you can use different tools to view its content. Check out below screenshot from HTTP Archive Viewer, a Google Chrome plugin to view HAR files.
As you can see from above tutorial, using BrowserMob Proxy with Selenium is simple. Give it a try in your project and share your feedback with us in comments.
The knowledge of technology you have been sharing thorough this post is very much helpful to develop new idea. here by i also want to share this.
Great post! I am actually getting ready to across this information, It’s very helpful for this blog.Also great with all of the valuable information you have Keep up the good work you are doing well.
What version of Selenium were you using for this example? It is now Sept 2017 and there are guava version incompatibilities with Selenium and BrowserMob.
can you give us link to project please ?
THis post is so good.