Export HAR using chromedriver

后端 未结 3 2044
栀梦
栀梦 2021-02-14 17:33

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

相关标签:
3条回答
  • 2021-02-14 18:22

    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();
    }
    
    0 讨论(0)
  • 2021-02-14 18:29

    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()
    
    0 讨论(0)
  • 2021-02-14 18:30

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

    0 讨论(0)
提交回复
热议问题