Get Chrome's Console Log

Get chrome's console log

I don't know C# but here's Java code that does the job, I hope you can translate it to C#

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.logging.LogEntries;
import org.openqa.selenium.logging.LogEntry;
import org.openqa.selenium.logging.LogType;
import org.openqa.selenium.logging.LoggingPreferences;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class ChromeConsoleLogging {
private WebDriver driver;

@BeforeMethod
public void setUp() {
System.setProperty("webdriver.chrome.driver", "c:\\path\\to\\chromedriver.exe");
DesiredCapabilities caps = DesiredCapabilities.chrome();
LoggingPreferences logPrefs = new LoggingPreferences();
logPrefs.enable(LogType.BROWSER, Level.ALL);
caps.setCapability(CapabilityType.LOGGING_PREFS, logPrefs);
driver = new ChromeDriver(caps);
}

@AfterMethod
public void tearDown() {
driver.quit();
}

public void analyzeLog() {
LogEntries logEntries = driver.manage().logs().get(LogType.BROWSER);
for (LogEntry entry : logEntries) {
System.out.println(new Date(entry.getTimestamp()) + " " + entry.getLevel() + " " + entry.getMessage());
//do something useful with the data
}
}

@Test
public void testMethod() {
driver.get("http://mypage.com");
//do something on page
analyzeLog();
}
}

Pay attention to setUp method in above code. We use LoggingPreferences object to enable logging. There are a few types of logs, but if you want to track console errors then LogType.BROWSER is the one that you should use. Then we pass that object to DesiredCapabilities and further to ChromeDriver constructor and voila - we have an instance of ChromeDriver with logging enabled.

After performing some actions on page we call analyzeLog() method. Here we simply extract the log and iterate through its entries. Here you can put assertions or do any other reporting you want.

My inspiration was this code by Michael Klepikov that explains how to extract performance logs from ChromeDriver.

Save the console.log in Chrome to a file

I needed to do the same thing and this is the solution I found:

  1. Enable logging from the command line using the flags:

    --enable-logging --v=1

This logs everything Chrome does internally, but it also logs all the console.log() messages as well. The log file is called chrome_debug.log and is located in the User Data Directory which can be overridden by supplying --user-data-dir=PATH (more info here).


  1. Filter the log file you get for lines with CONSOLE(\d+).

Note that console logs do not appear with --incognito.

How to read from Chrome's console in JavaScript

You can't. What's in the console can't be read from JavaScript.

What you can do is hook the console.log function so that you store when it logs :

console.stdlog = console.log.bind(console);
console.logs = [];
console.log = function(){
console.logs.push(Array.from(arguments));
console.stdlog.apply(console, arguments);
}

console.logs contains all what was logged. You can clean it at any time by doing console.logs.length = 0;.

You can still do a standard, non storing, log by calling console.stdlog.

How to get Chrome browser console log [INFO] entries with Selenium

thank you for your answer but unfortunately none of them worked for me, I have managed to come up with a solution for my issue using:

    ChromeOptions options = new ChromeOptions();
options.setCapability(ChromeOptions.CAPABILITY, getCap());
WebDriver driver = new ChromeDriver(options);

Alongside with the custom made method:

private static DesiredCapabilities getCap() {
DesiredCapabilities caps = DesiredCapabilities.chrome();
LoggingPreferences logPrefs = new LoggingPreferences();
logPrefs.enable(LogType.PERFORMANCE, Level.INFO);
logPrefs.enable(LogType.PROFILER, Level.INFO);
logPrefs.enable(LogType.BROWSER, Level.INFO);
logPrefs.enable(LogType.CLIENT, Level.INFO);
logPrefs.enable(LogType.DRIVER, Level.INFO);
logPrefs.enable(LogType.SERVER, Level.INFO);
caps.setCapability(CapabilityType.LOGGING_PREFS, logPrefs);
return caps;
}

And finally had to make a filter method as the Selenium one was not working properly for me and was returning all of the entries:

private static List<LogEntry> filterLog(LogEntries entries) {
List<LogEntry> logs = new ArrayList<>();
for (LogEntry entry : entries) {
if(entry.getLevel().toString().equals(INFO)) {
logs.add(entry);
}
}
return logs;
}

In Chrome, how can I get the javascript console output to stdout/stderr

  • Enable logging from the command line using the flags:

    --enable-logging --v=stderr

This logs everything Chrome does internally, but it also logs all the console.log() messages as well. The log file is called chrome_debug.log and is located in the User Data Directory.

How to get console logs on Chrome mobile without usb debug?

Found it! So, it's actually a Chrome on iOS only feature.

URL: chrome://inspect

That URL won't show anything on Chrome for Android, which tripped me up. But, on iOS it shows a "Start Logging" button.

Sample Image

Answer from: https://stackoverflow.com/a/55433616/2096769



Related Topics



Leave a reply



Submit