Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get chrome's console log

I want to build an automation testing, so I have to know the errors that appear in the console of chrome.

there is an option to get the error lines that appear in the console?

In order to see the console: right click somewhere in the page, click "inspect element" and then go to "console".

like image 925
Alon Shmiel Avatar asked Aug 15 '13 20:08

Alon Shmiel


People also ask

How do I export Chrome console logs?

Chrome console log Select the 'Console'' tab and make sure the option 'Preserve log' is checked. Reproduce the issue. You'll see data being collected in the console window. Right click on any log statement in the console window, and click Save As… to save the log file to your computer.

Why can't I see console log in Chrome?

log() outputs the result in the “console” of its browser.To be able to see the console output you should right click on the page and select inspect element and then go to the console tab(At least that's what you do in Chrome).


2 Answers

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.

like image 50
JacekM Avatar answered Sep 22 '22 06:09

JacekM


You can get logs this way:

Driver().Manage().Logs.GetLog(); 

By specifying what log you are interested in you can get the browser log, that is:

Driver().Manage().Logs.GetLog(LogType.Browser); 

Also remember to setup your driver accordingly:

ChromeOptions options = new ChromeOptions(); options.SetLoggingPreference(LogType.Browser, LogLevel.All); driver = new ChromeDriver("path to driver", options); 
like image 31
Agent Shoulder Avatar answered Sep 25 '22 06:09

Agent Shoulder