When using Selenium, it's important to note that it can only interact with the browser itself. This means that getting CPU usage from the server or JS heap size may not be possible through Selenium alone.
However, you can retrieve the Java heap size created by your script as it will be accessible to Selenium. For information about server usage, you may need to use another tool or directly inspect the server.
EDIT
Upon further consideration and research, it appears that you can obtain the JS heap size using Selenium's JavascriptExecutor. By accessing
window.performance.memory.usedJSHeapSize
, you can retrieve this value at any point during testing. Here is an example code snippet:
public static Double reportMemoryUsage(WebDriver webDriver, String message) {
((JavascriptExecutor) webDriver).executeScript("window.gc()");
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
LOGGER.error(e.getLocalizedMessage());
}
return Double usedJSHeapSize = (Double) ((JavascriptExecutor) webDriver)
.executeScript("return window.performance.memory.usedJSHeapSize/1024/1024");
LOGGER.info("Memory Usage at " + usedJSHeapSize + " MB.");
}
By calling this method at the start and end of your test suite, you can calculate the Heap space utilized by your Selenium script based on the difference in usedJSHeapSize values.
Prior to retrieving usedJSHeapSize, I include garbage collection to ensure accurate data. To enable the gc function on window, set the -js-flags=--expose-gc
option like so:
ChromeOptions options = new ChromeOptions();
options.addArguments("-js-flags=--expose-gc");
WebDriver webDriver = new ChromeDriver(options);
If you still require the Java heap size from your script, please let me know, and I can provide a solution for that as well.