Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Export HAR using chromedriver

Is it possible to export HAR using chromedriver similar to what I can do with netexpert+firebug with Firefox?

like image 841
Priyadarshi Kunal Avatar asked Aug 06 '13 08:08

Priyadarshi Kunal


People also ask

What is Export HAR?

HAR (HTTP Archive) is a file format used by several HTTP session tools to export the captured data. This can be highly useful in troubleshooting complex issues by obtaining additional information about the network requests that are generated in the browser while an issue occurs.

How do I create a HAR file?

To generate the HAR file for SafariOpen the Develop menu and select Show Web Inspector. Click the Network tab and complete the activity that is causing issues. Click the Export icon on the far right of the network tab and save the HAR file. Send us the file via your support ticket.

How do I open a .HAR file in Chrome?

Open chrome browser. right click anywhere on a page > inspect elements > go to network tab > drag and drop the . har file You should see the logs.


3 Answers

Yes, using BrowsermobProxy you can generate HAR file using chromedriver.

Here is a script in python to programatically generate HAR file using Selenium, BrowserMob Proxy and chromedriver. Python Packages for selenium and browsermob-proxy are needed to run this script.

from browsermobproxy import Server
from selenium import webdriver
import os
import json
import urlparse

server = Server("path/to/browsermob-proxy")
server.start()
proxy = server.create_proxy()

chromedriver = "path/to/chromedriver"
os.environ["webdriver.chrome.driver"] = chromedriver
url = urlparse.urlparse (proxy.proxy).path
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--proxy-server={0}".format(url))
driver = webdriver.Chrome(chromedriver,chrome_options =chrome_options)
proxy.new_har("http://stackoverflow.com", options={'captureHeaders': True})
driver.get("http://stackoverflow.com")    
result = json.dumps(proxy.har, ensure_ascii=False)
print result
proxy.stop()    
driver.quit()
like image 156
Paras Dahal Avatar answered Sep 21 '22 16:09

Paras Dahal


You can enable performance log via chromedriver and analyze the network traffic to build HAR on your own.

like image 33
Xiaoming Avatar answered Sep 21 '22 16:09

Xiaoming


Please checkout the code at

https://gist.github.com/Ankit3794/01b63199bd7ed4f2539a088463e54615#gistcomment-3126071

Steps:

Initiate ChromeDriver instance with enabling Logging Preference

DesiredCapabilities capabilities = DesiredCapabilities.chrome();
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("ignore-certificate-errors");
chromeOptions.addArguments("disable-infobars");
chromeOptions.addArguments("start-maximized");

// More Performance Traces like devtools.timeline, enableNetwork and enablePage
Map<String, Object> perfLogPrefs = new HashMap<>();
perfLogPrefs.put("traceCategories", "browser,devtools.timeline,devtools");
perfLogPrefs.put("enableNetwork", true);
perfLogPrefs.put("enablePage", true);
chromeOptions.setExperimentalOption("perfLoggingPrefs", perfLogPrefs);

// For Enabling performance Logs for WebPageTest
LoggingPreferences logPrefs = new LoggingPreferences();
logPrefs.enable(LogType.PERFORMANCE, Level.ALL);
capabilities.setCapability("goog:loggingPrefs", logPrefs);
capabilities.merge(chromeOptions);

Get "message" JSONObject from Performance Logs

private static JSONArray getPerfEntryLogs(WebDriver driver) {
    LogEntries logEntries = driver.manage().logs().get(LogType.PERFORMANCE);
    JSONArray perfJsonArray = new JSONArray();
    logEntries.forEach(entry -> {
        JSONObject messageJSON = new JSONObject(entry.getMessage()).getJSONObject("message");
        perfJsonArray.put(messageJSON);
    });
    return perfJsonArray;
}

Get HAR by passing PerfLogs

public static void getHAR(WebDriver driver, String fileName) throws IOException {
    String destinationFile = "/HARs/" + fileName + ".har";
    ((JavascriptExecutor) driver).executeScript(
            "!function(e,o){e.src=\"https://cdn.jsdelivr.net/gh/Ankit3794/chrome_har_js@master/chromePerfLogsHAR.js\",e.onload=function(){jQuery.noConflict(),console.log(\"jQuery injected\")},document.head.appendChild(e)}(document.createElement(\"script\"));");
    File file = new File(destinationFile);
    file.getParentFile().mkdirs();
    FileWriter harFile = new FileWriter(file);
    harFile.write((String) ((JavascriptExecutor) driver).executeScript(
            "return module.getHarFromMessages(arguments[0])", getPerfEntryLogs(driver).toString()));
    harFile.close();
}
like image 31
Ankit Patel Avatar answered Sep 21 '22 16:09

Ankit Patel