Selenium 捕获 console logs (Java)

目录

启用日志记录功能


有时候在进行自动化测试的时候控制台输出会帮忙定位问题,所以捕获控制台输出就显得很重要了~

以下以selenium 4为例:

我们可以使用driver.manage().logs().get(LogType.BROWSER)代码在Selenium中检索日志,该代码将返回一个包含所有控制台日志的LogEntries对象。

启用日志记录功能

在捕获日志之前,我们将在驱动程序实例中添加日志记录功能。

java 复制代码
ChromeOptions options = new ChromeOptions();
 
LoggingPreferences logPrefs = new LoggingPreferences();
logPrefs.enable(LogType.BROWSER, Level.ALL);
options.setCapability(options.LOGGING_PREFS, logPrefs);
WebDriver driver = new ChromeDriver(options);

然后我们可以使用getLog()方法轻松地获取控制台日志。

java 复制代码
LogEntries entry = driver.manage().logs().get(LogType.BROWSER);

这将返回一个日志条目列表,然后我们可以对其进行迭代并打印到控制台。

java 复制代码
// Retrieving all logs
List<LogEntry> logs = entry.getAll();
 
// Printing details separately
for (LogEntry e : logs) {
    System.out.println("Message: " + e.getMessage());
    System.out.println("Level: " + e.getLevel());
}

getMessage()打印控制台上显示的消息。

getLevel()打印消息的级别。它可以是INFO、WARNING、SEVERE等。

我们还可以使用getTimestamp()来获取在浏览器控制台上打印消息的时间戳。

java 复制代码
import java.util.List;
import java.util.logging.Level;
 
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
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.testng.annotations.Test;
 
public class CodekruTest {
 
    @Test
    public void getLogs() {
 
        // pass the path of the chromedriver location in the second argument
        System.setProperty("webdriver.chrome.driver", "E:\\chromedriver.exe");
         
        ChromeOptions options = new ChromeOptions();
        LoggingPreferences logPrefs = new LoggingPreferences();
        logPrefs.enable(LogType.BROWSER, Level.ALL);
        options.setCapability(options.LOGGING_PREFS, logPrefs);
         
        WebDriver driver = new ChromeDriver(options);
        driver.get("https://testkru.com/TestUrls/TestConsoleLogs");
 
        LogEntries entry = driver.manage().logs().get(LogType.BROWSER);
 
        // Retrieving all logs  
        List<LogEntry> logs = entry.getAll();
 
        // Printing details separately
        for (LogEntry e : logs) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("Level: " + e.getLevel());
            System.out.println("Timestamp: "+ e.getTimestamp());
        }
 
    }
 
}

输出:

bash 复制代码
Message: https://testkru.com/TestUrls/TestConsoleLogs 102:30 "This is a console log by using the console.log() method"
Level: INFO
Timestamp: 1673764474531
Message: https://testkru.com/TestUrls/TestConsoleLogs 103:30 "This is an info log by using the console.info() method"
Level: INFO
Timestamp: 1673764474531
Message: https://testkru.com/TestUrls/TestConsoleLogs 104:30 "This is a debug log by using the console.debug() method"
Level: FINE
Timestamp: 1673764474531
Message: https://testkru.com/TestUrls/TestConsoleLogs 105:29 "This is a warning log by using the console.warn() method"
Level: WARNING
Timestamp: 1673764474531
Message: https://testkru.com/TestUrls/TestConsoleLogs 106:29 "This is an error log by using the console.error() method"
Level: SEVERE
Timestamp: 1673764474531
相关推荐
Wpa.wk1 天前
Docker原理和使用场景(网络模式和分布式UI自动化环境部署)
linux·经验分享·分布式·测试工具·docker·性能监控
我送炭你添花1 天前
软件测试为何不可或缺?——以复杂宏系统与 PTZ 控制为例,深度解析 pytest 的实战价值与不可替代性
python·测试工具·pytest
深蓝电商API1 天前
Selenium 截图与元素高亮定位技巧
爬虫·python·selenium
我的xiaodoujiao2 天前
使用 Python 语言 从 0 到 1 搭建完整 Web UI自动化测试学习系列 43--添加allure测试报告显示信息和其他封装方法
python·学习·测试工具·allure
夜郎king2 天前
Java搭配Selenium:实现网页访问与自动截图的实战指南及常见问题解析
selenium·java 网页截图·selenium截图实现
深蓝电商API2 天前
Selenium 爬取 Canvas 渲染的数据图表
爬虫·python·selenium
可可南木2 天前
3070文件格式--10--testorder文件格式详解
功能测试·测试工具·pcb工艺
深蓝电商API2 天前
Selenium 动作链 ActionChains 高级用法
爬虫·python·selenium
我的xiaodoujiao2 天前
使用 Python 语言 从 0 到 1 搭建完整 Web UI自动化测试学习系列 44--Pytest框架钩子函数
python·学习·测试工具·pytest