[英]Selenium - How to log every step that is being performed?
I'm trying to find a way to log every action performed by the selenium driver
object. 我正在尝试找到记录selenium driver
对象执行的每个操作的方法。 Log4j
is the commonly suggested solution. Log4j
是常用的解决方案。 However, dedicated log statements are needed to add to the log as below - 但是,需要专用的日志语句添加到日志中,如下所示 -
driver.findElement(By.name("opt1")).sendKeys("km");
log.debug("selecting distance unit");
driver.findElement(By.name("opt2")).sendKeys("10");
log.debug("selecting distance value");
So i have to have log statements wherever i need to log. 所以我必须在任何需要记录的地方都有日志声明。 Is there anything which tracks the actions of the selenium driver and gives a general log? 有没有什么跟踪硒驱动程序的行为并给出一般日志?
Extend the WebDriver class you are using and override the log function, this function gets called before and after each function call. 扩展您正在使用的WebDriver类并覆盖日志函数,在每次函数调用之前和之后调用此函数。
Example: 例:
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.remote.SessionId;
public class MyWebDriver extends ChromeDriver {
@Override
protected void log(SessionId sessionId, String commandName, Object toLog, When when) {
System.out.println("LOG: sessionId: " + sessionId + " when: " + when + " commandName: " + commandName + " toLog: " + toLog);
super.log(sessionId, commandName, toLog, when);
}
}
This code will give you output that look like this: 此代码将为您提供如下所示的输出:
LOG: sessionId: 70521460-9ccf-4619-97ea-e7be4717a9b8 when: BEFORE commandName: findElement toLog: [70521460-9ccf-4619-97ea-e7be4717a9b8, findElement {using=xpath, value=//*[@hint='Username']}]
LOG: sessionId: 70521460-9ccf-4619-97ea-e7be4717a9b8 when: AFTER commandName: findElement toLog: [70521460-9ccf-4619-97ea-e7be4717a9b8, findElement {using=xpath, value=//*[@hint='Username']}]
LOG: sessionId: 70521460-9ccf-4619-97ea-e7be4717a9b8 when: BEFORE commandName: findElement toLog: [70521460-9ccf-4619-97ea-e7be4717a9b8, findElement {using=xpath, value=//*[@id='Password']}]
LOG: sessionId: 70521460-9ccf-4619-97ea-e7be4717a9b8 when: AFTER commandName: findElement toLog: [70521460-9ccf-4619-97ea-e7be4717a9b8, findElement {using=xpath, value=//*[@id='Password']}]
if you are planning to use selenium server observe the nodes terminal once execution starts, it logs each and every step in much detailed fashion. 如果您计划在执行开始时使用selenium服务器观察节点终端,则会以非常详细的方式记录每个步骤。 browserstack uses this technique I guess. 我想, browserstack使用这种技术。
Selenium has its own log system, you should import LoggingPreferences
and do as below Selenium有自己的日志系统,您应该导入LoggingPreferences
并执行以下操作
LoggingPreferences logs = new LoggingPreferences();
logs.enable(LogType.BROWSER, Level.ALL);
there are different LogType
like PERFORMANCE, SERVER, DRIVER, ....
which you can enable, disable or set level for it. 有不同的LogType
如PERFORMANCE, SERVER, DRIVER, ....
你可以启用,禁用或设置它的级别。 for more info take a look at the answer in the following link 有关更多信息,请查看以下链接中的答案
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.