0

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!

A. Mitrea
  • 13
  • 3
  • Both, unitDriver.getPafeSource() and page.getWebReponse() are returning host page content (see http://www.gwtproject.org/doc/latest/DevGuideOrganizingProjects.html#DevGuideHostPage). This is the actual content. But I suspect you want to check some elements after bootstraping. In this case you should use DOM related API from HtmlUnit/WebClient (e.g. page.asText, page.getByXpath) – Alexander Leshkin Nov 05 '16 at 07:12
  • Thanks for the answer, Alexander. You are right, I am looking for DOM elements in the page, but the problem is that I am not getting anything from it when calling unitDriver.findElementByClassName("listGrid"), for example. Also, the page.asText() method only provides me the page title, so that's not very useful as well. – A. Mitrea Nov 08 '16 at 08:49

1 Answers1

0

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.

A. Mitrea
  • 13
  • 3