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
相关推荐
Lossya3 小时前
【自动化测试】UI自动化的分类、如何选择合适的自动化测试工具以及其中appium的设计理念、引擎和引擎如何工作
自动化测试·测试工具·ui·appium·自动化
waterHBO13 小时前
python 爬虫 selenium 笔记
爬虫·python·selenium
chenjingming6661 天前
windows使用tcpdump.exe工具进行抓包教程
网络·测试工具·tcpdump
小码哥说测试1 天前
软件测试技术之 GPU 单元测试是什么!
自动化测试·功能测试·测试工具·jmeter·单元测试·集成测试·postman
全能全知者2 天前
不废话简单易懂的Selenium 页面操作与切换
python·selenium·测试工具·网络爬虫
测试19982 天前
使用Selenium进行网页自动化
自动化测试·软件测试·python·selenium·测试工具·自动化·测试用例
做一道光2 天前
1、QAC静态测试常用操作
软件测试·测试工具·静态测试
假女吖☌2 天前
postman接口关联
测试工具·postman
测试杂货铺2 天前
selenium元素定位:元素点击交互异常解决方法
自动化测试·软件测试·python·selenium·测试工具·职场和发展·单元测试
讓丄帝愛伱3 天前
PostMan使用变量
测试工具·postman