Hide "com.gargoylesoftware.htmlunit.." Warnings Logs From Console In WebDriver

In previous post we learnt how to run your selenium test In HTML Unit driver or headless browser. If you are using HtmlUnit Driver to run your test, Sometimes you will see lots of warnings as a log with text like "com.gargoylesoftware.htmlunit.DefaultCssErrorHandler error". This Is
one kind of warning and we can eliminate It very easily.

We can use Java Logging API to eliminate this warning message from console. Main usage of this JAVA API Is to write execution log, reporting errors and warnings. You can set different levels of log based on your requirement. Here we will turn that log level to OFF using bellow given syntax.

//To hide warnings logs from execution console.
Logger logger = Logger.getLogger("");
logger.setLevel(Level.OFF);

Full selenium Html Unitdriver  test example to hide warning logs from execution console Is as bellow.
package Testing_Pack;

import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class htmlDriver {
 
 HtmlUnitDriver driver;
 String pagetitle;

 @BeforeTest
 public void setup() throws Exception {
  //Initializing HtmlUnitDriver.
  driver = new HtmlUnitDriver();
  driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
  
  //To hide warnings logs from execution console.
  Logger logger = Logger.getLogger("");
  logger.setLevel(Level.OFF);
  
  //Opening URL In HtmlUnitDriver.
  driver.get("http://www.google.com");
 }

 @AfterTest
 public void tearDown() throws Exception {
  //Closing HtmlUnitDriver.
  driver.quit();
 }

 @Test
 public void googleSearch() {
  
  //Get and print page title before search.
  pagetitle = driver.getTitle();
  System.out.println("Page title before search : "+pagetitle);
  
  //Search with Hello World on google.
  WebElement Searchbox = driver.findElement(By.xpath("//input[@name='q']"));
  Searchbox.sendKeys("Hello World");
  Searchbox.submit();
  
  //Get and print page title after search.
  pagetitle = driver.getTitle();
  System.out.println("Page title after search : "+pagetitle);
  
  //Get list of search result strings.
  List<WebElement> allSearchResults=driver.findElements(By.cssSelector("ol li h3>a"));

  //Iterate the above list to get all the search titles & links from that page.
  for(WebElement eachResult : allSearchResults) {
  System.out.println("Title : "+eachResult.getText()+", Link : "+eachResult.getAttribute("href"));
  }
 }
}

Now you will not see any "com.gargoylesoftware.htmlunit.DefaultCssErrorHandler error" log messages. This Is the way to eliminate any unwanted warnings from console. Next post will tell you how to execute javascript In HTMLUnit driver test.

2 comments: