0

I'm trying to read sources of page traversed during selenium replay via browsermob proxy but I always get an empty har file.

Selenium 3.141 browsermob-core 2.1.5 jar

Below is my code.

If I set

WebDriver driver = new ChromeDriver(options); page gets loaded

WebDriver driver = new ChromeDriver(capabilities); page fails to load and return page isn't loading

Proxy is getting launched correctly as per the logs

/*******************************/

[RemoteTestNG] detected TestNG version 6.14.3

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".

SLF4J: Defaulting to no-operation (NOP) logger implementation

SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.

net.lightbody.bmp.BrowserMobProxyServer@61322f9dBrowserMobProxy 59858is port number

Proxy(manual, http=10.88.16.64:59858, ssl=10.88.16.64:59858)seleniumProxy

/********************************/

Any suggestions would be helpful.

public void test() throws Exception 
    {

        BrowserMobProxy proxy = getProxyServer(); //getting browsermob proxy
        System.out.println(proxy+"BrowserMobProxy");
        Proxy seleniumProxy = getSeleniumProxy(proxy);
        System.out.println(seleniumProxy+"seleniumProxy");
        DesiredCapabilities capabilities = new DesiredCapabilities().chrome();


        capabilities.setCapability(CapabilityType.PROXY, seleniumProxy);
        capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
        System.setProperty("webdriver.chrome.driver", "C:\\APMWS\\testmvnproject\\src\\drivers\\chromedriver76.exe");
         ChromeOptions options = new ChromeOptions();
         WebDriver driver = new ChromeDriver(options);

        proxy.newHar(); // creating new HAR
        driver.get("https://google.com");
        driver.navigate().to("https://pizzahut.com"); //example

        List<HarEntry> entries = proxy.getHar().getLog().getEntries();
        for (HarEntry entry : entries) 
        {
            HarRequest request = entry.getRequest();
            HarResponse response = entry.getResponse();
            System.out.println(request.getUrl()+" : "+response.getStatus()+","+entry.getTime()+"ms");
        }
    proxy.stop();
    driver.close();
    }

    public Proxy getSeleniumProxy(BrowserMobProxy proxyServer) 
    {
        Proxy seleniumProxy = ClientUtil.createSeleniumProxy(proxyServer);
        System.out.println(seleniumProxy.getHttpProxy());
    try
    {
        String hostIp = Inet4Address.getLocalHost().getHostAddress();
        System.out.println(hostIp);
        seleniumProxy.setHttpProxy(hostIp + ":" + proxyServer.getPort());
        seleniumProxy.setSslProxy(hostIp + ":" + proxyServer.getPort());
        System.out.println(proxyServer.getPort()+"is port number");
    } 
    catch (UnknownHostException e) 
    {
        e.printStackTrace();
        System.out.println("invalid host");
    }
    return seleniumProxy;
}

public BrowserMobProxy getProxyServer() 
{
    BrowserMobProxy proxy = new BrowserMobProxyServer();
    proxy.setTrustAllServers(true);
    proxy.start(0);
    return proxy;
}

Expected Result: Write traces run via proxy to HAR file

Actual Result: Proxy gets set, page gets launched but HAR file is empty.

sridattas
  • 329
  • 3
  • 12

1 Answers1

0

You can get rid of the following: SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".

If you are running Maven just put this in your POM file.

<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-simple -->
<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-simple</artifactId>
  <version>1.7.25</version>
  <scope>test</scope>
</dependency>

This is how I start the Har file in Chrome.

  // start the proxy
        proxy = new BrowserMobProxyServer();
        proxy.start(0);
        // get the Selenium proxy object
        org.openqa.selenium.Proxy seleniumProxy = 
        ClientUtil.createSeleniumProxy(proxy);
        ChromeOptions options = new ChromeOptions();
        options.setCapability(CapabilityType.PROXY, seleniumProxy);
        options.setExperimentalOption("useAutomationExtension", false);
        options.addArguments("--start-maximized");
        driver = new ChromeDriver(options);