Time for action – capturing network traffic - Selenium

Imagine that you wanted to see if there was anything on the page that was not found. This could be images, CSS files, or JavaScript files. These things are not visible when working with a page, and it can be interesting with unexpected bugs. We will now see how we can create a HAR file and then capture it.

Since the HAR will return the JSON we need, we just need to parse the JSON returned to get what we want.

  1. Using the project we created previously, we are going to add a few more lines to get what we want.

  2. We need tell Selenium WebDriver that we have a proxy that it has to use. We do that with:

  3. We need to tell the proxy to create a new HAR file for us. We do this by adding the following line:

    Proxy.newHar("PageName"); // PageName is the name of the page we want to capture
  4. We then need to load a page, we can do this by clicking on a link calling get().

  5. Now we need to call proxy.getHar(). This will return the HAR that we wanted.

  6. Your code should look like this:

  7. And your HAR, once put through a JSON Viewer should look like this:

What just happened?

We have just managed to capture the network traffic while we are running our tests. This can be really useful if you want to see if there are any 404 responses when we are loading our application under test. This can be useful if you are moving things about or if you are doing a smoke test after you have deployed your application to production.

Pop quiz - capturing Network Traffic

  1. What is the name of the JSON format for showing network traffic?

  2. What is the call that tells BrowserMob Proxy to start recording the traffic?

Have a go hero - doing more with BrowserMob Proxy

Now that we have managed to get the proxy started and managed to record the network traffic that is going through to the browser, let us have a look at getting the proxy to slow down the time that a response takes to get through. BrowserMob Proxy supports this and Intellij will be able to help with what parts of the API to use.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Selenium Topics