0 votes
1 view
in Devops and Agile by (24.5k points)

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".

1 Answer

0 votes
by (62.1k points)

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(

@AfterMethod

public void tearDown() 

public void analyzeLog() 

LogEntries logEntries = driver.manage().logs().get(LogType.BROWSER);

for (LogEntry entry : logEntries) {

System.out.println(new Date(entry.getTimestamp()) + 

}

@Test

public void testMethod() 

driver.get(

}

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

After performing some actions on the page we call the analyzeLog() method.

Here we tend to merely extract the log and reiterate through its entries.

Here you'll place assertions or do the other reporting you would like.

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...