A. Mitrea A. Mitrea - 23 days ago 16
Java Question

Cannot enable javascript for GWT application using Selenium WebClient or HtmlUnitDriver

I am trying to get the HTML page source in order to test the GUI for a GWT application using headless browser testing. First I tried to get the content of the page using HtmlUnitDriver, like the following:

DesiredCapabilities capabilities = DesiredCapabilities.htmlUnit();

//set an userId header
capabilities.setCapability("id", "userId");

HtmlUnitDriver unitDriver = new HtmlUnitDriver(capabilities);

//enabled javascript
unitDriver.setJavascriptEnabled(true);

unitDriver.get("http://localhost:portNo/example/");

String pageSource = unitDriver.getPageSource();


The second way of doing it was by using WebClient:

WebClient webClient = new WebClient();

//enabled javascript
webClient.getOptions().setJavaScriptEnabled(true);

//set an userId header
webClient.addRequestHeader("id", "userId");

HtmlPage page = webClient.getPage("http://localhost:portNo/example/");
String contentAsString = page.getWebResponse().getContentAsString();


Yet, in the both cases I get the same result. The result does not contain the actual content of the page, and it displays the following:

</iframe>
<!-- RECOMMENDED if your web app will not function without JavaScript enabled --> <noscript>

&lt;div style="width: 22em; position: absolute; left: 50%; margin-left: -11em; color: red; background-color: white; border: 1px solid red; padding: 4px; font-family: sans-serif"&gt;
Your web browser must have JavaScript enabled
in order for this application to display correctly.
&lt;/div&gt;

</noscript>
<iframe src="javascript:''" id="frameId" style="position:absolute;width:0;height:0;border:none" tabindex="-1">
</iframe>


Is there anything I can do/enable in order to get the actual content of the page? Thank you!

Answer

Resolved the problem by setting a timeout in order for the javascript files to load.

So, after doing this:

HtmlPage page = webClient.getPage("http://localhost:portNo/example/");
webClient.waitForBackgroundJavaScript(10000); //time in milliseconds

the page had the content and I can find the expected DOM elements.

Comments