<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Jayant Sarda on Medium]]></title>
        <description><![CDATA[Stories by Jayant Sarda on Medium]]></description>
        <link>https://medium.com/@jayant.sarda?source=rss-4307bc3df443------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*AzmIEAvBARQPoNeREoe3cQ.png</url>
            <title>Stories by Jayant Sarda on Medium</title>
            <link>https://medium.com/@jayant.sarda?source=rss-4307bc3df443------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 08:08:17 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@jayant.sarda/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[BrowserMob( Embedded Mode) Integration with Webdriver/Selenium (Java)]]></title>
            <link>https://techblog.people.inc/selenium-browsermob-integration-c35f4713fb59?source=rss-4307bc3df443------2</link>
            <guid isPermaLink="false">https://medium.com/p/c35f4713fb59</guid>
            <category><![CDATA[request-parameters]]></category>
            <category><![CDATA[browsermob]]></category>
            <category><![CDATA[network-calls]]></category>
            <category><![CDATA[selenium-webdriver]]></category>
            <category><![CDATA[proxy]]></category>
            <dc:creator><![CDATA[Jayant Sarda]]></dc:creator>
            <pubDate>Mon, 28 Aug 2017 18:10:31 GMT</pubDate>
            <atom:updated>2017-08-28T18:10:31.273Z</atom:updated>
            <content:encoded><![CDATA[<p>We would assume that you have basic knowledge of Webdriver and BrowserMob proxy,but we will add a bit of a refresher just to make it easy for you the causal reader.</p><p><a href="https://github.com/lightbody/browsermob-proxy"><strong>BrowserMob</strong></a> Proxy allows you to manipulate HTTP requests and responses, capture HTTP content, and export performance data as a <a href="http://www.softwareishard.com/blog/har-12-spec/">HAR file</a>. BMP works well as a standalone proxy server, but it is especially useful when embedded in Selenium tests.</p><p><a href="https://github.com/SeleniumHQ/selenium"><strong>WebDriver</strong></a> is a tool for automating web application testing, and in particular to verify that they work as expected</p><p>In this blog we will try to focus on the integrating BrowserMob with Webdriver so that we can record the network calls being made by a browser while webdriver tests are run in a HAR file for futher consumption.<br><strong>Benefits</strong>: <br>1. User can verify all the network request and post parameter to a server making sure that application is passing right values across.<br>2. User can verify the response from a server and confirm server is responding as expected.<br>3. User can validate all the request/response time lines in order to see the sequence of actions.</p><p>BMP allows you to validates all the parameters in an automated fashion which user generally performs using chrome’s debug tools in developer mode -&gt; Network Tab as below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mAR42O8NYpkZ-ae9cJ3siA.png" /></figure><p>Now since you have understood the need of this integration, lets jump on the details of implementation.</p><p><strong>Assumptions</strong>: <br>1. User is running webdriver in Java - Maven project.<br>2. User has already have your webdriver scripts running and have all required dependencies added.<br>3. Using Junit/testNG unit testing framework(just to use the example at the end of article, otherwise its not mandatory).</p><p><strong>Integration</strong>.<br><strong><em>Step 1</em></strong> :Add below maven dependency to you root maven project:</p><pre>&lt;dependency&gt;<br>        &lt;groupId&gt;net.lightbody.bmp&lt;/groupId&gt;<br>        &lt;artifactId&gt;browsermob-core&lt;/artifactId&gt;<br>        &lt;version&gt;2.1.4&lt;/version&gt;<br>        &lt;scope&gt;test&lt;/scope&gt;<br> &lt;/dependency&gt;</pre><p><strong><em>Step 2</em></strong> : Get proxy server running</p><pre>public BrowserMobProxy getProxyServer() {<br>     BrowserMobProxy proxy = new BrowserMobProxyServer();<br>     proxy.setTrustAllServers(true); <br>// above line is needed for application with invalid certificates <br>     proxy.start();<br>     return proxy;<br>}</pre><p>Above method creates a new BrowserMob proxy server and returns the instance to calling method.<br>snippet :</p><pre>proxy.setTrustAllServers(true); </pre><p>Can be omitted if your application is making calls for servers with valid certificates or if there are no “https” calls</p><p><strong><em>Step 3</em></strong> : Returning a Selenium proxy object generated from BMP server created in step 2</p><pre>public Proxy getSeleniumProxy(BrowserMobProxy proxyServer) {<br>  Proxy seleniumProxy = ClientUtil.createSeleniumProxy(proxyServer);<br>  try {<br>  String hostIp = Inet4Address.getLocalHost().getHostAddress());<br>  seleniumProxy.setHttpProxy(hostIp+”:” + proxyServer.getPort());<br>  seleniumProxy.setSslProxy(hostIp+”:” + proxyServer.getPort());<br>  } catch (UnknownHostException e) {<br>      e.printStackTrace();<br>      Assert.fail(“invalid Host Address”);<br>    }<br>return seleniumProxy;<br>}</pre><p>In above method we wrap the browserMob proxy server into to a selenium proxy object which can later be passed as browser capability.<br>The code “Inet4Address.getLocalHost().getHostAddress())” will return you the ip of the current host where this code is running (your local machine or the Jenkins slave from where the automation gets triggered ). This is true only in case of embedded mode of browserMob integration.</p><p>Code below:</p><pre>seleniumProxy.setHttpProxy(hostIp+”:” + proxyServer.getPort());<br>seleniumProxy.setSslProxy(hostIp+”:” + proxyServer.getPort());</pre><p>we need to specify the selenium proxy instance to use this <em>hostIp</em> and <em>port</em> for both http and https calls.</p><p><strong><em>Step 4</em></strong> : Now we have our selenium proxy instance ready which can be passed as capability to your driver instance as below:</p><pre>DesiredCapabilities capabilities = new DesiredCapabilities();<br>Proxy seleniumProxy = getSeleniumProxy(getProxyServer());<br>capabilities.setCapability(CapabilityType.PROXY, seleniumProxy);</pre><p>At this point if you run your test with proxy, all your browser’s network call should get routed via the BrowserMob proxy (IP and port). Which can be verified by the console log of your test execution. Your console output should be something like below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/798/1*f4VDdiDpZoILtbQT4sDPuw.png" /></figure><p><strong><em>Step 5</em></strong> : HAR creation</p><p>Now since we have our webdriver instance ready with proxy setting, we need to use it in out test and capture the HAR (having network calls).To begin with this, the very first thing we need to do is to create a new HAR for the proxy. This can be done by calling <em>newHar() </em>method on the browserMob proxy instance as below:</p><pre>proxy.newHar();</pre><p>Once the HAR is created we need to perform the action for which we want to record th network calls, example: loading a webpage (google.com)</p><pre>driver.get(&quot;http://www.google.com&quot;);</pre><p>once this call is made, the proxy will record all the calls being made in the HAR file in tmp directory.</p><p><strong><em>Step 6 </em></strong>: HAR validation/printing</p><p>Once the action is done, you can read/print/validate all the requests and responses being made with <em>proxy.getHar().getLog() </em>.</p><pre>List&lt;HarEntry&gt; entries = proxy.getHar().getLog().getEntries();<br>for(HarEntry entry :entries){<br>System.out.println(entry.getRequest().getUrl());<br>}</pre><p>Now you have access to all these calls and you can validate which ever call you wish to by filtering from the list.</p><p><strong><em>Step 7 </em></strong>: Stopping the proxyserver<br>Its important to close the proxy server created for else you will face issues while running multiple tests and leaving lot of open proxies</p><pre>proxy.stop();</pre><p><strong>Standalone Example</strong>: Below is one standalone script for all the above we discussed:</p><pre>package com.dotdash.sample;<br>import java.net.Inet4Address;<br>import java.net.UnknownHostException;<br>import java.util.List;<br>import org.junit.Assert;<br>import org.junit.Test;<br>import org.openqa.selenium.Proxy;<br>import org.openqa.selenium.WebDriver;<br>import org.openqa.selenium.chrome.ChromeDriver;<br>import org.openqa.selenium.remote.CapabilityType;<br>import org.openqa.selenium.remote.DesiredCapabilities;<br>import net.lightbody.bmp.BrowserMobProxy;<br>import net.lightbody.bmp.BrowserMobProxyServer;<br>import net.lightbody.bmp.client.ClientUtil;<br>import net.lightbody.bmp.core.har.HarEntry;</pre><pre>public class Sample {<br>@Test<br>public void test() throws Exception {<br>System.setProperty(&quot;webdriver.chrome.driver&quot;, &quot;&lt;path to your chromedriver exe&gt;&quot;);<br>DesiredCapabilities capabilities = new DesiredCapabilities();<br>BrowserMobProxy proxy = getProxyServer(); //getting browsermob proxy<br>Proxy seleniumProxy = getSeleniumProxy(proxy);<br>capabilities.setCapability(CapabilityType.PROXY, seleniumProxy);<br>WebDriver driver = new ChromeDriver(capabilities);<br>proxy.newHar(); // creating new HAR<br>driver.get(&quot;http://www.google.com&quot;);<br>List&lt;HarEntry&gt; entries = proxy.getHar().getLog().getEntries();<br>for (HarEntry entry : entries) {<br>System.out.println(entry.getRequest().getUrl());<br>}<br>proxy.stop();<br>driver.close();<br>}</pre><pre>public Proxy getSeleniumProxy(BrowserMobProxy proxyServer) {<br>Proxy seleniumProxy = ClientUtil.createSeleniumProxy(proxyServer);<br>try {<br>String hostIp = Inet4Address.getLocalHost().getHostAddress();<br>seleniumProxy.setHttpProxy(hostIp + &quot;:&quot; + proxyServer.getPort());<br>seleniumProxy.setSslProxy(hostIp + &quot;:&quot; + proxyServer.getPort());<br>} catch (UnknownHostException e) {<br>e.printStackTrace();<br>Assert.fail(&quot;invalid Host Address&quot;);<br>}<br>return seleniumProxy;<br>}</pre><pre>public BrowserMobProxy getProxyServer() {<br>BrowserMobProxy proxy = new BrowserMobProxyServer();<br>proxy.setTrustAllServers(true);<br>proxy.start();<br>return proxy;<br>}<br>}</pre><p>Console log for above script should look like below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*m6KEBJxQizm3MjvE73USiw.png" /></figure><p><strong>Tips</strong>:</p><ol><li>You can decide what kind of network calls you need to capture by using proxy.setHarCaptureTypes method . Example below:</li></ol><pre>proxy.setHarCaptureTypes(CaptureType.REQUEST_HEADERS,CaptureType.RESPONSE_HEADERS);<br>//or <br>proxy.setHarCaptureTypes(CaptureType.REQUEST_CONTENT,CaptureType.RESPONSE_CONTENT);</pre><p>2. If user wish to start the proxy at a specific port instead of random port, it can be done as below:</p><pre>proxy.start(9090);</pre><p>3 .For validating HARs further there are opensource projects to make HAR validations easier.For example: <a href="https://github.com/janodvarko/harviewer">https://github.com/janodvarko/harviewer</a></p><p><strong>IMPORTANT NOTE</strong>: One thing to remember here is that proxy tests are always slower than non-proxy tests. So you may want to add this capability to your driver instance conditionally based on some flag or parameter.</p><p>Happy Learning!!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c35f4713fb59" width="1" height="1" alt=""><hr><p><a href="https://techblog.people.inc/selenium-browsermob-integration-c35f4713fb59">BrowserMob( Embedded Mode) Integration with Webdriver/Selenium (Java)</a> was originally published in <a href="https://techblog.people.inc/">People Inc. Tech Blog</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>