我用Firefox 48尝试了Selenium 3.0.1.
我已经尝试过以下代码:
java.util.logging.Logger.getLogger(“com.gargoylesoftware.htmlunit”).setLevel(Level.OFF);
java.util.logging.Logger.getLogger(“org.apache.commons.httpclient”).setLevel(Level.OFF);
java.util.logging.Logger.getLogger(ProtocolHandshake.class.getName()).setLevel(Level.OFF);
但是一旦我在Netbeans下进行常规测试,……日志仍然会出现:
Dec 02, 2016 9:17:53 AM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Attempting bi-dialect session, assuming Postel's Law holds true on the remote end
Dec 02, 2016 9:17:57 AM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
解决这个问题的任何线索?
解决方法:
您必须将记录器固定在内存中或设置logging.properties配置文件.来自java.util.logging.Logger文档:
Logger objects may be obtained by calls on one of the getLogger factory methods. These will either create a new Logger or return a suitable existing Logger. It is important to note that the Logger returned by one of the getLogger factory methods may be garbage collected at any time if a strong reference to the Logger is not kept.
返回新记录器时,日志级别由LogManager确定,LogManager默认使用logging.properties文件中的设置.在您的示例中,可以看到以下内容:
>调用getLogger创建一个新的记录器并从LogManager设置级别.
>您的代码将记录器级别设置为OFF.
> G.C.运行并销毁您的记录器以及刚刚应用的设置.
> Selenium调用getLogger并创建一个新的记录器并从LogManager设置级别.
以下是一个示例测试用例来证明这一点:
public static void main(String[] args) {
String name = "com.gargoylesoftware.htmlunit";
for (int i = 0; i < 5; i++) {
System.out.println(Logger.getLogger(name).getLevel());
Logger.getLogger(name).setLevel(Level.OFF);
System.runFinalization();
System.gc();
System.runFinalization();
Thread.yield();
}
}
这将输出null而不是OFF.
如果您通过持有强引用来固定记录器,则步骤#3永远不会发生,而Selenium应该找到您创建的记录器,其级别设置为OFF.
private static final Logger[] pin;
static {
pin = new Logger[]{
Logger.getLogger("com.gargoylesoftware.htmlunit"),
Logger.getLogger("org.apache.commons.httpclient"),
Logger.getLogger("org.openqa.selenium.remote.ProtocolHandshake")
};
for (Logger l : pin) {
l.setLevel(Level.OFF);
}
}
标签:java,unit-testing,logging,firefox,selenium
来源: https://codeday.me/bug/20190828/1754906.html