java - Play 2.5 和 Fluentlenium : how to turn HtmlUnit warnings off

标签 java selenium playframework fluentlenium

我正在尝试使用 PlayFramework 2.5 的 WithBrowser 类编写 Selenium 测试。

类似这样的事情:

public class BrowserFunctionalTest extends WithBrowser {

   @Test
   public void runInBrowser() {
      browser.goTo("/");
      assertNotNull(browser.$("title").getText());
  }
}

但是,我希望能够至少为 CSS 错误设置自定义错误处理程序,因为它们会向我的控制台发送垃圾邮件。由于它们来自 boostrap,我无法摆脱它们。

我尝试像这样设置记录器的日志级别:

 java.util.logging.Logger.getLogger("com.gargoylesoftware.htmlunit").setLevel(java.util.logging.Level.SEVERE);    
System.getProperties().put("org.apache.commons.logging.simplelog.defaultlog", "fatal");

Fluentlenium 文档告诉我重写 getDefaultDriver 方法,但这似乎不适用于此处。而且我无法直接获取 WebClient,因为该字段没有 getter。

最佳答案

使用WithBrowser帮助程序类时,您可以重写provideBrowser来自定义TestBrowser的配置方式。还有一些其他细节,但下面的代码几乎展示了如何做到这一点:

import static  org.junit.Assert.*;

import com.gargoylesoftware.htmlunit.WebClient;
import org.junit.Test;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import play.test.Helpers;
import play.test.TestBrowser;
import play.test.WithBrowser;

public class BrowserFunctionalTest extends WithBrowser {

    // Just to make the WebClient visible at the test. Of course, this
    // could be an independent class to be reused by other tests or you
    // can create your own class that extends WithBrowser and hide all
    // the details from your tests.
    public static class CustomHtmlUnitDriver extends HtmlUnitDriver {
        @Override
        public WebClient getWebClient() {
            return super.getWebClient();
        }
    }

    @Override
    protected TestBrowser provideBrowser(int port) {
        // Here you need to create the TestBrowser for the class above.
        TestBrowser browser = Helpers.testBrowser(CustomHtmlUnitDriver.class, port);
        CustomHtmlUnitDriver driver = (CustomHtmlUnitDriver)browser.getDriver();
        WebClient client = driver.getWebClient();

        // do whatever you want with the WebClient

        return browser;
    }

    @Test
    public void runInBrowser() {
        browser.goTo("/");
        assertNotNull(browser.$("title").getText());
    }
} 

现在,由于您已经可以访问 WebClient,因此您可以按照此讨论中的说明进行操作:

Turning HtmlUnit Warnings off

关于java - Play 2.5 和 Fluentlenium : how to turn HtmlUnit warnings off,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37821376/

相关文章:

java - Wiquery DatePicker显示时间,验证失败

Java泛型和数组构造

python - 在服务器上运行 Django-selenium 项目

java - 在 Selenium webdriver 中跳过 SVG 条形图上的元素

selenium - 有没有办法通过 Selenium/GhostDriver 查看 PhantomJS console.log 消息?

scala - PlayFramework 2.5 : SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder"

java - 将 mysql 连接到 javafx 应用程序

java - 将以相同名称开头的字符串连接在一起

routes - 如何向 play 框架中的所有模板添加附加参数

scala - 如何在 play 2.5 中测试使用自定义解析器的 Controller 方法?