gum*_*ruh 3 java firefox logging selenium unit-testing
我在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
Run Code Online (Sandbox Code Playgroud)
有解决这个问题的线索吗?
您必须将记录器固定在内存中或设置logging.properties配置文件。从java.util.logging.Logger文档中:
可以通过调用getLogger工厂方法之一获取Logger对象。这些将创建新的Logger或返回合适的现有Logger。重要的是要注意,如果没有保留对Logger的强引用,则getLogger工厂方法之一返回的Logger随时可能被垃圾回收。
返回新的记录器时,日志级别由LogManager确定,默认情况下,LogManager使用logging.properties文件中的设置。在您的示例中,可能会看到以下内容:
这是一个示例测试案例,以证明这一点:
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();
}
}
Run Code Online (Sandbox Code Playgroud)
将输出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);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1268 次 |
| 最近记录: |