Browser memory leak automation using Selenium and Chrome Dev Tools

Sohan Soni :

We have existing web test cases written in Java using selenium-chrome-driver. Now we want to check for browser memory leaks after those tests are performed.

Manually, I do it by using Chrome Dev Tools - memory tab. Take heap dump,before I start the test, perform tests and then again take heap dump. Compare those two heap dumps which gives heap delta.

I am not able to find selenium-chrome-dev-tools API using which I could start the Chrome Dev Tool memory profiler (and perhaps some other tools), run my WebDriver tests (instantiating a Chrome browser instance, manipulating DOM elements, etc.), and then stop the profiler, then inspect the profiler's results to see if there are any memory leaks.

Is this concept even feasible or am I way out to lunch? Why/why not?

On the other hand, I have came across https://github.com/samccone/drool using which, I could get this information, but issue with that is I will have to rewrite all my existing java selenium tests in javascript, unless there is I can integrate drool with existing selenium tests.

Please suggest.

Note: Similar question is already asked under Chrome Dev Tools API & Selenium WebDriver but I don't see much helpful answer yet, so re posting again with more details.

Sohan Soni :

Selenium supports org.openqa.selenium.JavascriptExecutor. We can get the value of window.performance.memory.usedJSHeapSize at any stage during testing. Below is the code.

  public static void reportMemoryUsage(WebDriver webDriver, String message) {
    ((JavascriptExecutor) webDriver).executeScript("window.gc()");
    try {
        TimeUnit.SECONDS.sleep(2);
    } catch (InterruptedException e) {
        LOGGER.error(e.getLocalizedMessage());
    }
    Double usedJSHeapSize = (Double) ((JavascriptExecutor) webDriver)
            .executeScript("return window.performance.memory.usedJSHeapSize/1024/1024");
    LOGGER.info("Memory Usage at " + message + " - " + usedJSHeapSize + " MB ");
 }

Call this method from your test suite, one at beginning of the test and one at the end. Difference between two usedJSHeapSize values will give the memory leak.

I am forcing garbage collection before taking usedJSHeapSize, to make sure the there is no garbage information collected. To enable gc function on window, you will have to set the -js-flags=--expose-gc option.

ChromeOptions options = new ChromeOptions();
options.addArguments("-js-flags=--expose-gc");
WebDriver webDriver = new ChromeDriver(options);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=90357&siteId=1