1

I trace my HTTP-requests with BrowserUp (https://github.com/browserup/browserup-proxy) Proxy and get a HAR Object from the Server. Sadly BrowserUp does not contain a method to write this Object to File, which is why i use the same Method as BrowserMob does. Problem is: the file that comes out of it, does not seem to be formatted correctly (Gatling throws an exception for the time Format at index 23 and if i convert it to jmx jmeter says it does not contain http requests)

My Code:

    // Start the BrowserMob proxy
    BrowserUpProxy server = new BrowserUpProxyServer();
    server.setHarCaptureTypes(CaptureType.REQUEST_CONTENT);
    NativeResolver resolver = new NativeResolver();
    resolver.getHostRemappings();

    System.out.println("resolver:" + resolver);

    server.setHostNameResolver(resolver);

    server.start();
    server.newHar();

    // Get selenium proxy
    Proxy proxy = ClientUtil.createSeleniumProxy(server);

    GeckoDriverService geckoservice = new GeckoDriverService.Builder()
            .usingDriverExecutable(new File("C:/ProgrammeZwei/geckodriver.exe")).usingAnyFreePort()
            .usingAnyFreePort().build();
    try {
        geckoservice.start();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    // Configure desired capability for using proxy server with WebDriver
    FirefoxOptions options = new FirefoxOptions();
    options.setProxy(proxy);
    options.addPreference("network.proxy.allow_hijacking_localhost", true);

    // Set up driver
    WebDriver driver = new FirefoxDriver(geckoservice, options);

    driver.get("https://www.google.com");

    Har har = server.getHar();

    List<HarEntry> entries = server.getHar().getLog().getEntries();
    for (HarEntry entry : entries) {

        Date date = entry.getStartedDateTime();


        System.out.println("startedtime:" + date.toString() + " request:" + entry.getRequest().getMethod() + ":"
                + entry.getRequest().getUrl());

    }

    String pfad = System.getProperty("user.dir") + File.separator + System.currentTimeMillis() + "MiniTest.har";

    File harFile = new File(pfad);

    ObjectMapper mapper = new ObjectMapper();
    try {
        mapper.writeValue(harFile, har.getLog());

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    // Close the browser
    driver.quit();

a snippet of the console output:

    startedtime:Wed Aug 07 17:46:26 CEST 2019 request:GET:https://www.google.com/
    startedtime:Wed Aug 07 17:46:26 CEST 2019 request:GET:https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png
    startedtime:Wed Aug 07 17:46:26 CEST 2019 request:GET:https://www.google.com/gen_204?atyp=i&ct=rfl&cad=&ei=UvJKXYvRDY3awALA4qDYCg&zx=1565192786421
    startedtime:Wed Aug 07 17:46:26 CEST 2019 request:GET:https://www.google.com/gen_204?atyp=i&ct=rfl&cad=&ei=UvJKXYvRDY3awALA4qDYCg&zx=1565192786442

so i guess all the data is correctly gathered in the har-object. Do you have an idea on how to write this to file?

  • In the previous version of the BrowserUp Proxy, we had a couple bugs where the output violated the har spec in some small ways, like putting a string where an int was expected--that sort of thing. BrowserMob had them too. Can you try the very latest version, if that's not what you have? Also, feel free to request a method for writing the har to file here: https://github.com/browserup/browserup-proxy/issues. We have a good-sized roadmap, so it might not be immediate, but it seems like a reasonable feature to ask for. – ebeland Aug 07 '19 at 23:31
  • If the time format does not match the spec, feel free to report that as an issue, although we did review the spec when we cleaned up the last issues so here's to hoping. – ebeland Aug 07 '19 at 23:45
  • i requested an enhancement and i suggested a quickfix by annotating the har-classes with some json annotations. maybe this way the fix might be faster :) – CursedJuggernaut Aug 08 '19 at 11:16
  • I am using browser up proxy. But getting empty HAR. What could be the reason. I have used the code provided on the browser up proxy github page – UmeshPathak Jul 13 '20 at 12:04

1 Answers1

2

Since BrowserUp Proxy does not provide a way to do this yet, i build a method that works for me:

    private void writeHAR(File harFile, HarLog log) throws IOException {
    String version = log.getVersion();

    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSS'Z'");

    JsonGenerator jsonGenerator = new JsonFactory().createGenerator(harFile, JsonEncoding.UTF8);

    ObjectMapper objectMapper = new ObjectMapper().setSerializationInclusion(JsonInclude.Include.NON_NULL);
    jsonGenerator.setCodec(objectMapper);
    jsonGenerator.useDefaultPrettyPrinter();

    // Begin File
    jsonGenerator.writeStartObject();

    // Begin log
    jsonGenerator.writeFieldName("log");

    // Begin log object
    jsonGenerator.writeStartObject();

    jsonGenerator.writeFieldName("version");
    jsonGenerator.writeObject(version);

    jsonGenerator.writeFieldName("creator");
    jsonGenerator.writeObject(log.getCreator());

    jsonGenerator.writeFieldName("pages");
    // pages field contains an array of objects
    jsonGenerator.writeStartArray();

    // create the objects in the pages array
    for (HarPage page : log.getPages()) {
        jsonGenerator.writeStartObject();

        jsonGenerator.writeFieldName("startedDateTime");

        jsonGenerator.writeObject(dateFormat.format(page.getStartedDateTime()));

        jsonGenerator.writeFieldName("id");
        jsonGenerator.writeObject(page.getId());

        jsonGenerator.writeFieldName("title");
        jsonGenerator.writeObject(page.getTitle());

        jsonGenerator.writeFieldName("pageTimings");
        jsonGenerator.writeObject(page.getPageTimings());

        jsonGenerator.writeEndObject();
    }

    // end of pages array
    jsonGenerator.writeEndArray();

    jsonGenerator.writeFieldName("entries");
    // Begin of entris array
    jsonGenerator.writeStartArray();
    // write object for each entry
    for (HarEntry entry : log.getEntries()) {
        jsonGenerator.writeStartObject();

        jsonGenerator.writeFieldName("startedDateTime");
        jsonGenerator.writeObject(dateFormat.format(entry.getStartedDateTime()));

        jsonGenerator.writeFieldName("time");
        jsonGenerator.writeObject(entry.getTime());

        jsonGenerator.writeFieldName("request");
        jsonGenerator.writeObject(entry.getRequest());

        jsonGenerator.writeFieldName("response");
        jsonGenerator.writeObject(entry.getResponse());

        jsonGenerator.writeFieldName("timings");
        // object timings has multiple fields
        jsonGenerator.writeObject(entry.getTimings());

        jsonGenerator.writeFieldName("serverIPAddress");
        jsonGenerator.writeObject(entry.getServerIPAddress());
        jsonGenerator.writeFieldName("connection");
        jsonGenerator.writeObject(entry.getConnection());
        jsonGenerator.writeFieldName("pageref");
        jsonGenerator.writeObject(entry.getPageref());

        // end entry object
        jsonGenerator.writeEndObject();

    }

    // end of entries Array
    jsonGenerator.writeEndArray();

    // end of log object
    jsonGenerator.writeEndObject();

    // close File
    jsonGenerator.close();
}

this is the issue i posted on the BrowserUp github (since i´m not sure if my method will work for everyone): https://github.com/browserup/browserup-proxy/issues/143