14

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.

2
  • Did you add some timespan to wait after the the page is requested that contains this java script. HtmlUnitDriver seams need more explicite waits then FireFoxDriver? Commented Oct 28, 2011 at 9:24
  • I set the driver to implicitly wait for 10 seconds: driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); already tried other methods of waiting, effect is the same Commented Oct 28, 2011 at 10:00

5 Answers 5

21

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.

Sign up to request clarification or add additional context in comments.

8 Comments

don't use static sleeps
@CoreyGoldberg Care to explain why?
it's generally an anti-pattern in tests
Yeah, I can guess the reason, but I think it's better to use it than giving up on writing a test because it's generally an anti-pattern. Of course, it would be nice if there was an alternative.
Great. What is it? I'm not being offensive. I really want to know.
|
6

You need to initialize the HtmlUnitDriver with enable javascript true

new HtmlUnitDriver(true);

4 Comments

Hi Ralph I have JavaScript enabled with the following code: driver.setJavascriptEnabled(true); Does this make a difference?
@Tarken: I don't know - test it!
If I do it that way I am no longer emulating Firefox. Since I have to change driver = new HtmlUnitDriver(BrowserVersion.Firefox_3_6) to driver = new HtmlUnitDriver(true). If I do that I cannot pass the browsercompatibility check anymore.
For me, new HtmlUnitDriver(true) and setJavascriptEnabled(true) does NOT give same results. First one ignores JS errors, second one throws an exception.
4

If you wish to set the BrowserVersion as well as enable Javascript with HtmlUnitDriver, your initialization needs to look like the following (as there is no way to do both via the constructor):

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

This will allow you to use the browser definition of your choice and use Javascript.

1 Comment

That is what I was doing from the beginning and that didn't work.
3

You may need to do this:

WebDriver driver = new HtmlUnitDriver(BrowserVersion.INTERNET_EXPLORER_8);
((HtmlUnitDriver) driver).setJavascriptEnabled(true);

Comments

2

Well, There is an easy way to enable browser capability and javascript, you can do the following:

Webdriver driver = new HtmlUnitDriver(BrowserVersion.Chrome,true);

True specifies that javascript should be enabled. @Glenn Nelson,

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.