1

I have written a class for getting the har data from the firefox browser.

I want to get the har data in JSON format to show properly.

My Code :

ProxyServer server = new ProxyServer(4444);
    server.start();

    //captures the moouse movements and navigations
    server.setCaptureHeaders(true);
    server.setCaptureContent(true);

    // get the Selenium proxy object
    Proxy proxy = server.seleniumProxy();

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

    // start the browser up
    WebDriver driver = new FirefoxDriver(capabilities);

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

    // open yahoo.com
    driver.get("http://yahoo.com");

    // get the HAR data
    net.lightbody.bmp.core.har.Har har = server.getHar();

May anyone help me with getting the HAR data in JSON format and in string too !

4

1 回答 1

1

Sorry for the late answer.

Hoping it can help someone else here.

You can use a StringWriter to write the Har to String. So you can get the har as string. And, you can parse that string using Gson to get the JSON object.

net.lightbody.bmp.core.har.Har har = server.getHar();

java.io.StringWriter writer = new java.io.StringWriter();
try {
            har.writeTo(writer);
        } catch (IOException e) {
            e.printStackTrace();
        }

String harAsString = writer.toString();

com.google.gson.JsonElement harAsJson = new com.google.gson.Gson().toJsonTree(writer.toString());
于 2017-05-19T10:40:18.917 回答