Jean-Pierre Delacre
Jean-Pierre Delacre

Reputation: 186

Detailed timings with Selenium and browermob proxy gives empty har file

maybe someone can help me, I've searched for hours now and couldn't find a solution, on any site/blog/FAQ/...

I'm trying to get detailed timings for a page, with Selenium and Browsermob proxy. But the HAR file generated has always empty pageTitle, pageTimings and entries, like:

{"log":{"version":"1.2","creator":{"name":"BrowserMob Proxy","version":"2.0"},"pages":[{"id":"assertselenium.com","startedDateTime":"2014-08-26T15:45:49.134+0000","title":"","pageTimings":{}}],"entries":[]}}

When I see the tutorials, it looks sooo easy! But not for me. I'm behind a corporate proxy, maybe that's what causes the issues...

My code is (modified a hundred times...):

import java.io.File;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;

import net.lightbody.bmp.proxy.ProxyServer;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;

public class Example {

    public static void main(String[] args) throws Exception {

        ProxyServer server = new ProxyServer(4444);

        HashMap<String, String> options = new HashMap<String, String>();

        server.start();
        server.setOptions(options);
        server.setCaptureHeaders(true);
        server.setCaptureContent(true);

        Proxy proxy = new Proxy();

        // configure it as a desired capability
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability(CapabilityType.PROXY, proxy);

        FirefoxProfile profile = new FirefoxProfile();
        profile.setAcceptUntrustedCertificates(true);
        profile.setAssumeUntrustedCertificateIssuer(true);
        profile.setPreference("network.proxy.http", "[MY_PROXY_HERE]");
        profile.setPreference("network.proxy.http_port", 8080);
        profile.setPreference("network.proxy.ssl", "[MY_PROXY_HERE]");
        profile.setPreference("network.proxy.ssl_port", 8080);
        profile.setPreference("network.proxy.type", 1);
        profile.setPreference("network.proxy.no_proxies_on", "");
        capabilities.setCapability(FirefoxDriver.PROFILE, profile);

        // start the browser up
        WebDriver driver = new FirefoxDriver(capabilities);
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

        // create a new HAR with the label "apple.com"
        server.newHar("assertselenium.com");

        // open yahoo.com
        driver.get("http://assertselenium.com");
        // driver.get("http://assertselenium.com/2012/10/30/transformation-from-manual-tester-to-a-selenium-webdriver-automation-specialist/");

        driver.findElement(By.id("searchform"))
                .findElement(By.className("field"))
                .sendKeys(new String[] { "test selenium!" });

        driver.findElement(By.id("searchform"))
                .findElement(By.className("submit")).click();

        // new PerformanceTiming((JavascriptExecutor) driver, server.getHar());

        ((JavascriptExecutor) driver)
                .executeScript("var performance = window.performance || {};"
                        + "var timings = performance.timing || {};"
                        + "return timings;");

        server.getHar().writeTo(
                new File("C:/prj/SeleniumTest/harfiles/har.txt"));

        server.stop();
        driver.quit();

    }

}

(of course the [MY_PROXY_HERE] is only not to give the real name :))

Thank you !

Upvotes: 1

Views: 1877

Answers (1)

ColinMcC
ColinMcC

Reputation: 258

Still don't have 50 rep so answer rather than comment ....

Just wondering what it is you're after from the performance timing? As you can retrieve data from the Navigation Timing API directly from your browser instance? This gives you a detailed breakdown of the page load performance times. If your dev team are willing to insert marks, you can also use it to capture timings for embedded AJAX calls and suchlike. (Which is what I'm doing)

Getting the data back is just a simple JavaScript injection. In my case (Python - with 'driver' being my webdriver instance) - driver.execute_script('return window.performance.timing'). Which returns a dictionary object with the full page load timing breakdown.

Just asking as it seems a lot simpler than messing around with proxies ...

Upvotes: 2

Related Questions