我有一个Java项目,它使用selenium和ChromeDriver在我的网站上自动测试。我已经为我的日志文件配置了 log4j2 和 slf4j。但是,驱动程序拒绝登录我的文件,而是使用控制台:
Starting ChromeDriver 2.30.477700 (0057494ad8732195794a7b32078424f92a5fce41) on port 41019
Only local connections are allowed.
log4j:WARN No appenders could be found for logger (org.apache.http.client.protocol.RequestAddCookies).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Apr 20, 2018 12:31:42 AM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
我必须在日志记录设置中进行哪些调整,以便 Web 驱动程序登录到我的日志记录文件而不是控制台?对不起,我对伐木的了解有限。解释我做错了什么将不胜感激。
我在 log4j2.xml 中尝试了以下附加器:
<logger name="org.openqa.selenium" level="all">
<AppenderRef ref="selenium"/>
</logger>
<logger name="webdriver.chrome" level="all">
<AppenderRef ref="webdriverChrome"/>
</logger>
<logger name="org.apache.http.client.protocol.RequestAddCookies" level="all">
<AppenderRef ref="webdriverChrome"/>
</logger>
<logger name="org.seleniumhq.selenium.selenium-chrome-driver" level="all">
<AppenderRef ref="webdriverChrome"/>
</logger>
我的马文绒球.xml:
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.7</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.22</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.22</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-firefox-driver</artifactId>
<version>3.4.0</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>htmlunit-driver</artifactId>
<version>2.24</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-chrome-driver -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-chrome-driver</artifactId>
<version>3.4.0</version>
</dependency>
我找到了这个半生不熟的解决方案。初始化 web/chrome 驱动程序时使用以下代码:
Properties log4jProp = new Properties();
log4jProp.setProperty("log4j.rootLogger", "WARN");
PropertyConfigurator.configure(log4jProp);
System.setProperty("webdriver.chrome.logFile", ".//log//webdriverChrome_1.log");
System.setProperty("webdriver.chrome.driver", ".\path\to\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(waitInMilliseconds, TimeUnit.MILLISECONDS);
它仅删除日志记录框架的警告。我在一定程度上感到满意。如果有人知道将控制台消息的其余部分放入记录器的答案,将不胜感激。
到目前为止的结果:
Starting ChromeDriver 2.30.477700 (0057494ad8732195794a7b32078424f92a5fce41) on port 41019
Only local connections are allowed.
Apr 20, 2018 12:31:42 AM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS