Is there a way to capture browser logs while running automated test cases with Selenium? I found an article on how to capture JavaScript errors in Selenium. But that is just
Driver manager logs can be used to get console logs from browser and it will help to identify errors appears in console.
import org.openqa.selenium.logging.LogEntries;
import org.openqa.selenium.logging.LogEntry;
public List<LogEntry> getBrowserConsoleLogs()
{
LogEntries log= driver.manage().logs().get("browser")
List<LogEntry> logs=log.getAll();
return logs;
}
Add cast RemoteWebDriver
to driver
initialize and you will have the .setLogLevel
method:
import java.util.logging.Level;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.remote.RemoteWebDriver;
public class PrintLogTest {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "/Users/.../chromedriver");
WebDriver driver = new ChromeDriver();
//here
((RemoteWebDriver) driver).setLogLevel(Level.INFO);
driver.get("https://google.com/");
driver.findElement(By.name("q")).sendKeys("automation test");
driver.quit();
}
}
Example output:
Jun 15, 2020 4:27:04 PM org.openqa.selenium.remote.RemoteWebDriver log
INFO: Executing: get [430aec21a9beb6340a4185c4ea6a693d, get {url=https://google.com/}]
Jun 15, 2020 4:27:06 PM org.openqa.selenium.remote.RemoteWebDriver log
INFO: Executed: [430aec21a9beb6340a4185c4ea6a693d, get {url=https://google.com/}]
Jun 15, 2020 4:27:06 PM org.openqa.selenium.remote.RemoteWebDriver log
INFO: Executing: findElement [430aec21a9beb6340a4185c4ea6a693d, findElement {using=name, value=q}]
Jun 15, 2020 4:27:06 PM org.openqa.selenium.remote.RemoteWebDriver log
INFO: Executed: [430aec21a9beb6340a4185c4ea6a693d, findElement {using=name, value=q}]
...
...
At least I've tried it on ChromeDriver()
and FirefoxDriver()
and it working fine.
As a non-java selenium user, here is the python equivalent to Margus's answer:
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
class ChromeConsoleLogging(object):
def __init__(self, ):
self.driver = None
def setUp(self, ):
desired = DesiredCapabilities.CHROME
desired ['loggingPrefs'] = { 'browser':'ALL' }
self.driver = webdriver.Chrome(desired_capabilities=desired)
def analyzeLog(self, ):
data = self.driver.get_log('browser')
print(data)
def testMethod(self, ):
self.setUp()
self.driver.get("http://mypage.com")
self.analyzeLog()
Reference
Edit: Keeping Python answer in this thread because it is very similar to the Java answer and this post is returned on a Google search for the similar Python question
In a more concise way, you can do:
LogEntries logs = driver.manage().logs().get(LogType.BROWSER);
For me it worked wonderfully for catching JS errors in console. Then you can add some verification for its size. For example, if it is > 0, add some error output.
Before launching webdriver, we just set this environment variable to let chrome generate it:
export CHROME_LOG_FILE=$(pwd)/tests/e2e2/logs/client.log
A less elegant solution is taking the log 'manually' from the user data dir:
Set the user data dir to a fixed place:
options = new ChromeOptions();
capabilities = DesiredCapabilities.chrome();
options.addArguments("user-data-dir=/your_path/");
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
Get the text from the log file chrome_debug.log located in the path you've entered above.
I use this method since RemoteWebDriver
had problems getting the console logs remotely. If you run your test locally that can be easy to retrieve.