如何在 Selenium WebDriver 中获取本机记录器 [英] How to obtain native logger in Selenium WebDriver

查看:37
本文介绍了如何在 Selenium WebDriver 中获取本机记录器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能以某种方式获得 Selenium WebDriver 使用的记录器?我想捕获发出的所有命令的副本(例如:打开、等待、单击等).特别是我正在寻找一个 java 解决方案,因为我正在将测试导出到 junit.

Is it possible to obtain the logger somehow that Selenium WebDriver uses? I want to capture a transcript of all the commands that were issued (eg: open, wait, click, etc). In particular I am looking for a java solution, as I am exporting the tests into junit.

我在他们的网站上找到了这段代码,但它在标准输出中什么也没显示

I found this code on their website, however it displays nothing on standard out

    DesiredCapabilities caps = DesiredCapabilities.firefox(); 
    LoggingPreferences logs = new LoggingPreferences(); 
    logs.enable(LogType.DRIVER, Level.FINEST); 
    caps.setCapability(CapabilityType.LOGGING_PREFS, logs); 
    driver = new FirefoxDriver(caps);

推荐答案

在您使用的驱动程序中启用日志记录,选择您感兴趣的日志类型和日志级别(我使用的是 FirefoxDriver,启用所有类型日志并收集所有日志消息)

Enable logging in the driver you're using, select which log types you're interested in and the log level (I'm using FirefoxDriver, enabling all types of logs and collecting all log messages)

LoggingPreferences logs = new LoggingPreferences();
logs.enable(LogType.BROWSER, Level.ALL);
logs.enable(LogType.CLIENT, Level.ALL);
logs.enable(LogType.DRIVER, Level.ALL);
logs.enable(LogType.PERFORMANCE, Level.ALL);
logs.enable(LogType.PROFILER, Level.ALL);
logs.enable(LogType.SERVER, Level.ALL);

DesiredCapabilities desiredCapabilities = DesiredCapabilities.firefox();
desiredCapabilities.setCapability(CapabilityType.LOGGING_PREFS, logs);

WebDriver driver = new FirefoxDriver(desiredCapabilities);

然后,运行测试后,您可以收集日志(我只收集 DRIVER 日志,但您可以对任何类型的日志执行相同操作)

Then, after running the test you can collect the logs (I'm only collecting the DRIVER logs, but you can do the same for any type of log)

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

for (LogEntry logEntry : logEntries) {
    System.out.println(logEntry.getMessage());
}

这篇关于如何在 Selenium WebDriver 中获取本机记录器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆