如何在json扩展中保存har文件

时间:2018-02-05 12:25:11

标签: java har jsonparser

我试图以json格式保存har文件。 ?但是,当我这样做时,我正在解析异常。我能够以.json格式保存但是当我在线检查json格式时,我得到了解析器错误。对此提出任何建议都会有所帮助。请在下面找到我的代码。

 // 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();

我尝试使用和不使用UTF-8它会做同样的事情。请在下面找到解析错误的图片。

enter image description here

0 个答案:

没有答案