2

我正在尝试以 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,它做同样的事情。请在下图中查找解析错误。

在此处输入图像描述

4

0 回答 0