Executing JavaScript In HtmlUnit Driver For Selenium WebDriver Test

Now all of you are well aware about HtmlUnit Driver as we have created and executed example headless browser test In THIS POST and also learnt how to hide useless warning message logs from console In previous post. One another thing you need to learn before using It Is how to execute JavaScript with HtmlUnit driver. You can VIEW EXAMPLES of JavaScript executor to know Its
usage In selenium WebDriver tests.

By default, JavaScript Is not enabled In HtmlUnit driver. So If you will try to execute JavaScript In HtmlUnit driver test then It will show you error message In console and your test case will fail.

I have used JavaScript executor In bellow given example to get page title. Execute bellow given example to see error.

package Testing_Pack;

import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.openqa.selenium.JavascriptExecutor;
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 current page title using javascript executor.
  JavascriptExecutor javascript = (JavascriptExecutor) driver;
  String pagetitle=(String)javascript.executeScript("return document.title");  
  System.out.println("My Page Title Is  : "+pagetitle);    
 }
}


When you run above example, It will show you this error message : java.lang.UnsupportedOperationException: Javascript is not enabled for this HtmlUnitDriver instance.

We can resolve above error In two different ways.
1. Passing true keyword with HtmlUnitDriver on Initialization as bellow.
HtmlUnitDriver driver = new HtmlUnitDriver(true);

2. Using setJavascriptEnabled(true) method as bellow.
HtmlUnitDriver driver = new HtmlUnitDriver();
driver.setJavascriptEnabled(true);

Full example with enabled JavaScript for HtmlUnit driver Is as bellow. Now It will be executed successfully.

package Testing_Pack;

import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.openqa.selenium.JavascriptExecutor;
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 {
  //Enabling JavaScript on Initialization of HtmlUnitDriver.
  driver = new HtmlUnitDriver(true);  
  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 current page title using javascript executor.
  JavascriptExecutor javascript = (JavascriptExecutor) driver;
  String pagetitle=(String)javascript.executeScript("return document.title");  
  System.out.println("My Page Title Is  : "+pagetitle);    
 }
}

Above example will print page title In console which Is retrieved using JavaScript executor. Next post will show you how to execute headless browser test In different browsers.

1 comment:

  1. Need Help . My Code runs fine with Webdriver but with html unit driver i get Error message not able to locate element . Locators i tried all of them . Javascript is also enabled

    ReplyDelete