title: Extracting Network Requests via Har file using Selenium, Maven, Browsermob and Browserstack
body: <p>I'm attempting to test our Analytics using Browsermob in our selenium setup.</p>
  
  <p>However when I run the tests, the Har file that I am attempting to create is either not created at all or there is no information within it to parse and display in the command prompt.</p>
  
  <pre><code>package pageObjects;
  
  import java.io.IOException;
  import java.net.MalformedURLException;
  import java.net.URL;
  import java.net.UnknownHostException;
  import java.rmi.UnexpectedException;
  
  import org.junit.Assert;
  import org.openqa.selenium.Capabilities;
  import org.openqa.selenium.WebDriver;
  import org.openqa.selenium.ie.InternetExplorerDriver;
  import org.openqa.selenium.remote.CapabilityType;
  import org.openqa.selenium.remote.DesiredCapabilities;
  import org.openqa.selenium.remote.RemoteWebDriver;
  import org.testng.annotations.BeforeClass;
  import org.testng.annotations.Parameters;
  
  import net.lightbody.bmp.BrowserMobProxy;
  import net.lightbody.bmp.BrowserMobProxyServer;
  import net.lightbody.bmp.core.har.Har;
  
  
  
  public class setUp {
  
      protected static ThreadLocal&lt;WebDriver&gt; webDriver = new ThreadLocal&lt;WebDriver&gt;();
  
      public static final String WebURL = Credentials.PropertyFile.getURL();
      public static final String Environment = System.getProperty("Environment");
      static public BrowserMobProxy proxy = new BrowserMobProxyServer(); // getting browsermob proxy
  
      public static final String ANSI_RESET = "\u001B[0m";
      public static final String ANSI_BLACK = "\u001B[30m";
      public static final String ANSI_RED = "\u001B[31m";
      public static final String ANSI_GREEN = "\u001B[32m";
      public static final String ANSI_YELLOW = "\u001B[33m";
      public static final String ANSI_BLUE = "\u001B[34m";
      public static final String ANSI_PURPLE = "\u001B[35m";
      public static final String ANSI_CYAN = "\u001B[36m";
      public static final String ANSI_WHITE = "\u001B[37m";
  
       public WebDriver getWebDriver() {
              return webDriver.get();
          }
  
  
      @BeforeClass (alwaysRun = true)
      @Parameters(value = { "browser", "version", "os", "os_version", "project", "build" })
      protected void setCapabilities(String browser, String version, String os, String os_version, String project,
              String build) throws MalformedURLException, UnexpectedException {
  
          DesiredCapabilities capability = new DesiredCapabilities();
          capability.setCapability("browserName", browser);
          capability.setCapability("browserVersion", version);
          capability.setCapability("os", os);
          capability.setCapability("os_version", os_version);
          capability.setCapability("project", project);
          capability.setCapability("build", build);
          capability.setCapability("browserstack.debug", "true");
          capability.setCapability("browserstack.console", "warnings");
          capability.setCapability("resolution", "1920x1080");
          capability.setCapability("browserstack.local", "true");
          capability.setCapability("browserstack.localIdentifier", "Test123");
          capability.setCapability("browserstack.safari.enablePopups", "true");
          capability.setCapability("browserstack.safari.allowAllCookies", "true");
          capability.setCapability(CapabilityType.SUPPORTS_JAVASCRIPT, "true");
          capability.setCapability(CapabilityType.ACCEPT_SSL_CERTS, "true");
          capability.setCapability(CapabilityType.ACCEPT_INSECURE_CERTS, "true");
          capability.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, "true");
          capability.setCapability("browserstack.ie.enablePopups", "true");
          capability.setCapability("browserstack.edge.enablePopups", "true");
          capability.setCapability("unexpectedAlertBehaviour", "accept");
          capability.setCapability("ignoreProtectedModeSettings", "true");
          capability.setCapability("enablePersistentHover", "true");
          capability.setCapability(InternetExplorerDriver.IGNORE_ZOOM_SETTING, "true");
          capability.setCapability("browserstack.use_w3c", "true");
          capability.setCapability("browserstack.maskSendKeys", "true");
  
          Credentials.PropertyFile.setCapabilities(capability);
  
          webDriver.set(new RemoteWebDriver(new URL(Credentials.PropertyFile.getBrowserstackURL()), capability));
          EnvironmentErrors();
  
  
  
      }
  
      // Sending the Test Setup for Staging or Production //
  
      public static void TestSetup() throws InterruptedException, UnknownHostException {
  
          Capabilities cap = ((RemoteWebDriver) webDriver.get()).getCapabilities();
          String browserName = cap.getBrowserName().toString();
  
          proxy.newHar("Analytics"); // creating new HAR
  
          webDriver.get().get(WebURL);
          Thread.sleep(10000);
  
          System.out.println(ANSI_WHITE + webDriver.get().getTitle() + ("  Being tested on: ") + ANSI_GREEN + Environment
                  + ANSI_WHITE + (" and ") + ANSI_GREEN + browserName + (" browser!"));
          webDriver.get().manage().window().maximize();
          Thread.sleep(5000);
  
          // Retrieve Har
                  Har har = proxy.getHar();
  
                  // Write Har to string
                  java.io.StringWriter writer = new java.io.StringWriter();
                  try {
                      har.writeTo(writer);
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
  
                  String harAsString = writer.toString();
                  System.out.println(IndexElements.ANSI_BLUE + "=======CONSOLE=======" + harAsString);
  
      }
  
      // Environment Error Messages//
      public static void EnvironmentErrors() {
          try {
              // Verify if the environment field is empty, if so then print out line.
              if (Environment == null) {
  
                  Assert.fail(ANSI_RED
                          + "Please ensure that you have filled in the correct Environment to test. Use -DEnvironment=XXXXXX (Staging or Production) within your terminal code");
  
              } else if (Environment.equals("")) {
  
                  Assert.fail(ANSI_RED + "Please ensure that you have filled in the correct Environment to test.");
              }
          } catch (Exception Nothing) {
              // Do nothing
  
          }
      }
  
  }
  </code></pre>
  
  <p></p>
  
  <pre><code>package Sonata_Portal_Enterprise;
  
  import java.net.UnknownHostException;
  
  import org.openqa.selenium.Proxy;
  import org.openqa.selenium.remote.CapabilityType;
  import org.openqa.selenium.remote.DesiredCapabilities;
  import org.openqa.selenium.remote.RemoteWebDriver;
  import org.testng.annotations.AfterClass;
  import org.testng.annotations.AfterSuite;
  import org.testng.annotations.BeforeClass;
  import org.testng.annotations.BeforeSuite;
  import org.testng.annotations.Test;
  
  import net.lightbody.bmp.client.ClientUtil;
  import net.lightbody.bmp.proxy.CaptureType;
  import pageObjects.indexPage;
  
  @SuppressWarnings("unused")
  public class testIndex extends indexPage {
  
      @BeforeSuite(alwaysRun = true)
  
      public static void main() throws InterruptedException, UnknownHostException {
  
          DesiredCapabilities capability = new DesiredCapabilities();
          Proxy seleniumProxy = ClientUtil.createSeleniumProxy(proxy);
          capability.setCapability(CapabilityType.PROXY, seleniumProxy);
          proxy.setHarCaptureTypes(CaptureType.getAllContentCaptureTypes());
          proxy.enableHarCaptureTypes(CaptureType.getAllContentCaptureTypes());
          proxy.start();
      }
  
      @BeforeClass(alwaysRun = true)
      public static void setUp() throws InterruptedException, UnknownHostException {
  
          TestSetup();
          //String session = ((RemoteWebDriver) webDriver.get()).getSessionId().toString();
          //System.out.println(session);
  
      }
  
      @Test(groups = { "iFrame" }, priority = 1)
  
      public void iFrameVerification() throws InterruptedException {
  
          iFrame();
      }
  
      @Test(groups = { "Hero Elements" }, priority = 2)
  
      public void HeroVerification() {
          HeaderVerify();
          DelayIDVerify();
          LogSwitchVerify();
          RewardStickerVerify();
          AklamioLinkVerify();
      }
  
      @Test(groups = { "How It Works" }, priority = 3)
  
      public void HIWVerification() {
  
          HIWVerify();
      }
  
      @Test(groups = { "Products" }, priority = 4)
  
      public void ProductVerification() {
  
          ProductVerify();
      }
  
      @AfterClass(alwaysRun = true)
      public void TearDown() throws InterruptedException {
          webDriver.get().quit();
  
      }
  
      @AfterSuite(alwaysRun = true)
      public void proxyDown() throws InterruptedException {
  
          proxy.stop();
      }
  }
  </code></pre>
  
  <p>So when running all of this, it runs fine, but I am getting an empty har file in the console.</p>
  
  <pre><code>=======CONSOLE======={"log":{"version":"1.2","creator":{"name":"BrowserMob Proxy","version":"2.1.5","comment":""},"pages":[{"id":"Analytics","startedDateTime":"2019-02-21T14:34:09.546Z","title":"Analytics","pageTimings":{"comment":""},"comment":""}],"entries":[],"comment":""}}
  </code></pre>