javaseleniumplayframeworkfluentlenium

Play 2.5 and Fluentlenium: how to turn HtmlUnit warnings off


I am trying to write Selenium Tests using WithBrowser class of the PlayFramework 2.5.

Something like this:

public class BrowserFunctionalTest extends WithBrowser {

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

However, I want to be able to set custom Error Handlers for at least CSS errors, since they spam my console. And since they come from boostrap I cannot get rid of them.

I tried to set the loglevel of the logger like this:

 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 documentation tells me to override the getDefaultDriver method, but that does not seem to be applicable here. And I cannot get my hands on the WebClient directly, since there is no getter for the field.


Solution

  • When using WithBrowser helper class, you can override provideBrowser to customize how the TestBrowser is configured. There are some other details, but the code below pretty much shows how to do it:

    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());
        }
    } 
    

    Now, since you already have access to the WebClient, you can follow the instructions from this discussion:

    Turning HtmlUnit Warnings off