I'm trying to save har file in json format. ?However i'm getting parsing exception when i do that. I'm able to save in .json format but when i check the json format online i'm getting parser error. Any advise on this would be helpful. Please find my code below.
// start the proxy
BrowserMobProxy proxy = new BrowserMobProxyServer();
proxy.start(0);
// get the Selenium proxy object
Proxy seleniumProxy = ClientUtil.createSeleniumProxy(proxy);
// configure it as a desired capability
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.PROXY, seleniumProxy);
// start the browser up
System.setProperty("webdriver.chrome.driver","C:\\Users\\chromedriver.exe");
WebDriver driver = new ChromeDriver(capabilities);
// enable more detailed HAR capture, if desired (see CaptureType for the complete list)
proxy.enableHarCaptureTypes(CaptureType.REQUEST_CONTENT, CaptureType.RESPONSE_CONTENT);
// create a new HAR
proxy.newHar("https://www.google.com");
// get the HAR data
Har har = proxy.getHar();
driver.get("https://www.google.com");
String sFileName = "C:\\AutomationScripts\\harfile.har";
JSONArray array=new JSONArray();
File harFile = new File(sFileName);
try {
OutputStream out = new FileOutputStream("C:\\log.json");
Writer w = new OutputStreamWriter(out, "UTF-8");
har.writeTo(harFile);
har.writeTo(w);
System.out.println ("created har file");
} catch (IOException ex) {
System.out.println (ex.toString());
System.out.println("Could not find file " );
}
proxy.stop();
i tried with and without UTF-8 it does the same thing. Please find the below image for the parse error.