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.
You may need to do this:
Well, There is an easy way to enable browser capability and javascript, you can do the following:
True specifies that javascript should be enabled. @Glenn Nelson,
You need to initialize the
HtmlUnitDriver
with enable javascripttrue
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):This will allow you to use the browser definition of your choice and use Javascript.
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.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.