简体   繁体   English

Selenium - 如何记录正在执行的每个步骤?

[英]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. 有不同的LogTypePERFORMANCE, SERVER, DRIVER, ....你可以启用,禁用或设置它的级别。 for more info take a look at the answer in the following link 有关更多信息,请查看以下链接中的答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Selenium Webdriver“Actions”的执行速度有多快? - How fast are Selenium Webdriver “Actions” performed? 登录后如何管理 j_username 值? - How is j_username value being managed once login is performed? 如何在硒与黄瓜的步骤中断言 - How to make assertions in a step of cucumber with selenium 如何保持硒步保持等待1小时? - How to keep selenium step to be keep waiting for 1 hour? 找到硒按钮但未执行单击 - Selenium button is found but click is not performed 如何配置Selenium的日志级别? - How to configure log level for Selenium? 将控制台日志添加到 allure 报告中的每个步骤?(Java、Selenium、TestNG、Cucumber) - Add console log to each step in allure report?(Java, Selenium, TestNG, Cucumber) 如何为自定义消息处理器实现自定义SamplingService?在检索元素之后和执行序列之前进行日志记录 - How to implement a custom SamplingService for a custom message processor?log after that an element is retrieved and before the sequence is performed Selenium代码以捕获执行操作时的时间 - Selenium code to capture time when action is performed 在元素上执行的Selenium Java单击不起作用 - Selenium Java click performed on element did not work
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM