Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't HtmlUnitDriver execute JavaScript?

I got the following problem: I am running a JUnit testCase with Selenium 2.9 using HtmlUnitDriver with Browserversion Firefox_3_6. JavaScript is enabled. Now when it should call and execute the following javaScript function it does nothing:

function openIdsDocument()
{
    var windowBounds = getWindowBounds();
    var XMLHTTP = getAjaxRequestObject("XYZ.do?availableWidth="+windowBounds.width+"&availableHeight="+windowBounds.height, "", true);
    if (XMLHTTP != null)
    {
            XMLHTTP.onreadystatechange = function alertAJAXResponse()
            {
                    if (XMLHTTP.readyState == 4)
                    {
                            window.location.href = getContextPath() + "ABC.do";
                    }
            };
            XMLHTTP.send("timestamp=" + <%=System.currentTimeMillis()%>);
    }
    getLoadingState();
}

I want to get to ABC.do

If I execute my test with the FirefoxDriver it works.

Is there a way to get this working with HtmlUnitDriver? My test works if I manually call driver.get("http://host/ABC.do") but that cannot be the right way to do this.

like image 445
Tarken Avatar asked Oct 28 '11 07:10

Tarken


People also ask

How do I enable JavaScript in HtmlUnit?

Enable/Disable JavaScript support You can change this to silently (HtmlUnit will still log the exceptions) ignore them (like in real browsers) by setting the option throwExceptionOnScriptError to false. final WebClient webClient = new WebClient(); webClient. getOptions. setThrowExceptionOnScriptError(false);

Which is the default status of JavaScript in HtmlUnit driver?

Constructs a new instance with JavaScript disabled, and the default BrowserVersion.

Which engine is used to run JavaScript code in HtmlUnit?

2. About HtmlUnit. HtmlUnit is a GUI-less browser – a browser intended to be used programmatically and not directly by a user. The browser supports JavaScript (via the Mozilla Rhino engine) and can be used even for websites with complex AJAX functionalities.

What is the use of HTMLUnitDriver in selenium?

HtmlUnitDriver is headless driver providing non-GUI implementation of Selenium WebDriver. It is based on HtmlUnit, fastest and light-weight browser implemented in Java.


1 Answers

You can enable JavaScript by doing either

  • new HtmlUnitDriver(true);
  • driver.setJavascriptEnabled(true);

What you need to do is to wait until the JavaScript is executed after get(url).

You can use Thread.sleep() method for adding some delay.

HtmlUnitDriver driver = new HtmlUnitDriver(BrowserVersion.FIREFOX_3_6);
driver.setJavascriptEnabled(true);
driver.get(url);

Thread.sleep(100);

runTest();

Update

As @Corey indicated in the comments, it could be nicer to use Explicit and Implicit Waits instead of Thread.sleep(). As I don't use them these days, I cannot confirm, though. It would be great if someone test them and update this answer.

like image 167
Sanghyun Lee Avatar answered Oct 15 '22 02:10

Sanghyun Lee