I need to test for ga call using automation.
get(LogType.PERFORMANCE).getAll();
gives set of logs, but couldn't find the call request url.
Please help in finding the ga call.
Asked
Active
Viewed 1.0k times
4

fabdurso
- 2,366
- 5
- 29
- 55

Arun Prasanth.G
- 51
- 1
- 1
- 5
-
2For network capture either you can use BrowserMob proxy or set desired capabilities to get network logs. You can find the details here: https://wiki.saucelabs.com/plugins/servlet/mobile#content/view/72000725 (sauce labs) – Magesh Jul 10 '18 at 12:36
2 Answers
4
First, configure your webdriver with below code:
LoggingPreferences preferences = new LoggingPreferences();
preferences.enable(LogType.PERFORMANCE, Level.ALL);
ChromeOptions option = new ChromeOptions();
option.setCapability(CapabilityType.LOGGING_PREFS, preferences);
option.setCapability( "goog:loggingPrefs", preferences ); // for new chrome
WebDriver driver = new ChromeDriver(option);
Before closing the browser, use below code to capture the logs in file:
OutputStream logfile = new FileOutputStream(new File("A text file path where you can save the logs"),true);
PrintStream printlog = new PrintStream(logfile);
LogEntries logs = driver.manage().logs().get(LogType.PERFORMANCE);
for (LogEntry entry : logs) {
if(entry.toString().contains("\"type\":\"XHR\"") & entry.toString().contains("\"url\":\"https://example.com/")) {
printlog.append(new Date(entry.getTimestamp()) + " " + entry.toString() +" "+ System.getProperty("line.separator"));
printlog.append(System.getProperty("line.separator"));
printlog.append(System.getProperty("line.separator"));
}
}
printlog.close();
After this code you can close the driver. You will get all the call logs in the file.

Ankit Gupta
- 776
- 5
- 12
-
with the latest chrome driver update, we need to use 'goog:loggingPrefs' in place of 'CapabilityType.LOGGING_PREFS'. For more info https://stackoverflow.com/questions/53049026/selenium-chrome-performance-logs-not-working – Binnu Jesudasan Gudapati Jun 29 '19 at 19:25
-
1Yes, Thanks @BinnuJesudasanGudapati with the latest Chrome we need to add below code :option.setCapability( "goog:loggingPrefs", preferences ); editing this code. – Ankit Gupta Jul 02 '19 at 12:00
3
To get all performance information and network calls in Selenium using Python, I use execute_script
to make a JS call that will return the performance logs:
# Gets network information, including network calls
def GetNetworkResources(driver):
Resources = driver.execute_script("return window.performance.getEntries();")
for resource in Resources:
print(resource['name'])
return Resources
If you need a solution other than Python I can update my answer.

PixelEinstein
- 1,713
- 1
- 8
- 17
-
Does this work with browsers other than Chrome, or is this specific to ChromeDriver? – Seanonymous Oct 01 '18 at 23:07
-
1It should work with most browsers, I believe both `IE` and `Firefox` have `performance.getEntries()` capability. – PixelEinstein Oct 01 '18 at 23:17
-