java - 如何在java中从控制台隐藏不必要的Selenium调试消息

标签 java selenium

我使用 Intellij 和 jdk 14 来构建 seleninum 测试。我只想要控制台输出作为测试报告、结果或错误。我尝试了在 google 或 stackoverflow 上找到的许多方法,例如设置日志级别、grep 控制台,但这些消息仍然出现。有什么办法可以解决这个问题吗?这是输出:

16:44:14.551 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.maxCachedByteBuffersPerChunk: 1023
16:44:14.559 [main] DEBUG io.netty.buffer.ByteBufUtil - -Dio.netty.allocator.type: pooled
16:44:14.560 [main] DEBUG io.netty.buffer.ByteBufUtil - -Dio.netty.threadLocalDirectBufferSize: 0
16:44:14.560 [main] DEBUG io.netty.buffer.ByteBufUtil - -Dio.netty.maxThreadLocalCharBufferSize: 16384
Starting ChromeDriver 83.0.4103.39 (ccbf011cb2d2b19b506d844400483861342c20cd-refs/branch-heads/4103@{#416}) on port 7431
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
16:44:15.681 [Forwarding newSession on session null to remote] DEBUG io.netty.channel.DefaultChannelId - -Dio.netty.processId: 17484 (auto-detected)
16:44:15.683 [Forwarding newSession on session null to remote] DEBUG io.netty.util.NetUtil - -Djava.net.preferIPv4Stack: false
16:44:15.683 [Forwarding newSession on session null to remote] DEBUG io.netty.util.NetUtil - -Djava.net.preferIPv6Addresses: false
16:44:15.689 [Forwarding newSession on session null to remote] DEBUG io.netty.util.NetUtil - Loopback interface: lo (Software Loopback Interface 1, 127.0.0.1)
16:44:15.689 [Forwarding newSession on session null to remote] DEBUG io.netty.util.NetUtil - Failed to get SOMAXCONN from sysctl and file \proc\sys\net\core\somaxconn. Default: 200
16:44:15.696 [Forwarding newSession on session null to remote] DEBUG io.netty.channel.DefaultChannelId - -Dio.netty.machineId: fc:aa:14:ff:fe:e6:1d:11 (auto-detected)
16:44:15.758 [AsyncHttpClient-3-1] DEBUG io.netty.buffer.AbstractByteBuf - -Dio.netty.buffer.checkAccessible: true
16:44:15.758 [AsyncHttpClient-3-1] DEBUG io.netty.buffer.AbstractByteBuf - -Dio.netty.buffer.checkBounds: true
16:44:15.759 [AsyncHttpClient-3-1] DEBUG io.netty.util.ResourceLeakDetectorFactory - Loaded default ResourceLeakDetector: io.netty.util.ResourceLeakDetector@3d005e97
16:44:15.777 [AsyncHttpClient-3-1] DEBUG org.asynchttpclient.netty.channel.NettyConnectListener - Using new Channel '[id: 0x09b55496, L:/127.0.0.1:54163 - R:localhost/127.0.0.1:7431]' for 'POST' to '/session'
16:44:15.829 [AsyncHttpClient-3-1] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.maxCapacityPerThread: 4096
16:44:15.829 [AsyncHttpClient-3-1] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.maxSharedCapacityFactor: 2
16:44:15.829 [AsyncHttpClient-3-1] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.linkCapacity: 16
16:44:15.830 [AsyncHttpClient-3-1] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.ratio: 8
16:44:20.511 [AsyncHttpClient-3-1] DEBUG org.asynchttpclient.netty.handler.HttpHandler - 

Request DefaultHttpRequest(decodeResult: success, version: HTTP/1.1)
POST /session HTTP/1.1
User-Agent: selenium/4.0.0-alpha-6 (java windows)
Content-Length: 365
Content-Type: application/json; charset=utf-8
host: localhost:7431
accept: */*

最佳答案

您真正需要做的是在控制台日志处理程序(负责将所有日志消息打印到控制台)上设置日志级别。然而,在使用 BasicConfigurator 时执行此操作将非常棘手,您最好改为在属性文件中指定日志配置(这更加灵活)。

我建议通过 log4j logging configuration tutorial 进行工作- 这将帮助您准确地组合出您想要的配置,并且应该证明您的时间投资是值得的。但是,如果您想快速完成此操作,请尝试将以下内容添加到文件 log4j.properties ( example taken from here ):

# Root logger option
log4j.rootLogger=INFO, stdout

# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

然后更改代码以执行以下操作:

Properties props = new Properties();
props.load(new FileInputStream("/my/path/to/log4j.properties"));
PropertyConfigurator.configure(props);

关于java - 如何在java中从控制台隐藏不必要的Selenium调试消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62468001/

相关文章:

node.js - nightwatch.js 中 page_objects 中的多级部分

java - 为什么findElement(By by)的返回类型是WebElement?

java - 如何从最后一行到第一行读取文本文件

java - 无效标志 : -jar after upgrading java package to 1. 8.0_211

java - 为什么SortedSet接口(interface)中指定的是subSet方法而不是Set?

java - 用于许多测试的 geckodriver - Java

testing - Selenium + 无法使用 Flash 消息验证操作是否成功

java - 检查 pop 是否用 Selenium (Java) 关闭

java - Android TextWather 对象。 java 匿名类是如何工作的?

java - 正则表达式测试字符串是否包含数字