如何使用NetExport保存HAR文件?

问题描述:

我正在使用Visual Studio 2013和C#,试图看看它是如何工作的。 我想保存一个JSON文件并阅读它,但这是为了以后的一部分。如何使用NetExport保存HAR文件?

HAR文件NetExport应该创建的永远不会被保存。 我在这里做错了什么?

protected void Page_Load(object sender, EventArgs e) 
{ 
    FirefoxProfile profile = new FirefoxProfile(); 
    profile.AddExtension("netExport-0.9b7.xpi"); 
    profile.AddExtension("firebug-2.0.7-fx.xpi"); 
    // profile.SetPreference("app.update.enabled", false); 

    string domain = "extensions.firebug."; 
    profile.SetPreference(domain + "currentVersion", "2.0.7"); 
    profile.SetPreference(domain + "allPagesActivation", "on"); 
    profile.SetPreference(domain + "defaultPanelName", "net"); 
    profile.SetPreference(domain + "net.enableSites", true); 

    // Set default NetExport preferences 
    profile.SetPreference(domain + "netexport.alwaysEnableAutoExport", true); 
    profile.SetPreference(domain + "netexport.showPreview", false); 
    profile.SetPreference(domain + "netexport.defaultLogDir", 
     "C:\\Users\\MyName\\Desktop\\HAR\\"); 
    IWebDriver driver = new FirefoxDriver(profile); 
    System.Threading.Thread.Sleep(5000); 

    driver.Navigate().GoToUrl("http://www.google.com/"); 
    System.Threading.Thread.Sleep(5000); 

    driver.Quit(); 
} 

我有同样的问题。原因是出口可能需要一些时间。将你的Sleep放置成这样的循环(它是Java,但你可以很容易地将它翻译成C#),而不是等待一个固定的时间段:

// Count files in output directory 
final File harDir = new File(
    profile.getStringPreference("devtools.netmonitor.har.defaultLogDir", 
    "C:\\Users\\MyName\\Desktop\\HAR\\")); 
final int numFiles = harDir.listFiles().length; 

// Load test page 
driver.get("https://ixquick.com"); 

// Wait up to 3 min for new file in output directory 
for (int c=0; c<180; c++) { 
    if (harDir.listFiles().length > numFiles) { 
     break; 
    } 
    Thread.sleep(1000L); 
}