We've upgraded our jQuery version to 2.1.3 and it looks like the AJAX requests are now getting lost and never returning. I've attempted HTMLUnit 2.15 and then after upgrading to Java7 attempted with HTMLUnit 2.16 with the same results.
JavaScript:
viewRecipients = function(rowId) {
$j.ajax({
"type" : "POST",
"async" : true,
"url" : /viewRecipients.htm',
"data" : {
"recordId" : rowId
},
"beforeSend" : function() {
$j('body').append('BEFORE_SEND');
},
"success" : function(data, textStatus, jqXHR){
$j('body').append('SUCCESS: ' + jqXHR.status + ' ' + textStatus);
},
"error" : function(jqXHR, textStatus) {
$j('body').append('ERROR: ' + jqXHR.status + ' ' + textStatus);
},
"complete" : function(jqXHR, textStatus) {
$j('body').append('COMPLETE: ' + jqXHR.status + ' ' + textStatus);
}
});
};
HTMLUnit:
@Test
public void checkInOrdersList() throws Exception {
String ORDER_ID = "123_456";
final WebClient webClient = new WebClient(BrowserVersion.CHROME);
webClient.setCssErrorHandler(new SilentCssErrorHandler());
webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
webClient.getOptions().setThrowExceptionOnScriptError(false);
HtmlPage page = (HtmlPage) webClient.getPage(WebTestConstants.ORDERS_URL);
List<HtmlAnchor> viewDestAnchors = page.getAnchorByText("destinations");
assertFalse("No destination links", viewDestAnchors.isEmpty());
for (HtmlAnchor viewDestAnchor : viewDestAnchors) {
page = viewDestAnchor.click();
page.getWebClient().wait(15000);
}
assertTrue("Destination not in orders list", page.asXml().contains(ORDER_ID));
}
The BEFORE_SEND is appended to the HTML, but no other status. I've attempted
page.getWebClient().waitForBackgroundJavaScript(15000);
and
page.getWebClient().setAjaxController(new NicelyResynchronizingAjaxController());
even looped through with synchronized and wait (as recommended)
http://htmlunit.sourceforge.net/faq.html#AJAXDoesNotWork
And the request response never comes back.
So ... after about 2 weeks of trial and error I found that adding dataType : "json" to the AJAX request works.
I also changed
page.getWebClient().wait(15000);
to
page.getWebClient().waitForBackgroundJavaScriptStartingBefore(500);
Last edit: Jeremiah Muela 2015-05-14
Can you please elaborate, how did you add "dataType: 'json'"?
This still could be a bug.
I do think this is still a bug because the first example works in the browser, but not in HTMLUnit. The second example (posted below) works in the Browser (Chrome & Firefox) and HTMLUnit 2.16.
Thanks :-)
Working AJAX request/response is now:
JavaScript (notice the dataType is being set in the jQuery AJAX JSON):
HTMLUnit (IDK if the waitForBackgroundJavaScriptStartingBefore is needed, but it works):
Last edit: Jeremiah Muela 2015-05-14
I tried with a small application, and it works fine (see below), I guess there is an issue of the request sent.
Please provide external website to check, or compare the headers sent by HtmlUnit vs real browsers.
~~~~~~~~~
<script src="jquery-2.1.3.js"></script> <script> function test() { $.ajax({ "type" : "POST", "async" : true, "url" : 'test.xml', "data" : { "recordId" : 1234 }, "beforeSend" : function() { alert('beforeSend'); }, "success" : function(data, textStatus, jqXHR){ alert('SUCCESS: ' + jqXHR.status + ' ' + textStatus); }, "error" : function(jqXHR, textStatus) { alert('ERROR: ' + jqXHR.status + ' ' + textStatus); }, "complete" : function(jqXHR, textStatus) { alert('COMPLETE: ' + jqXHR.status + ' ' + textStatus); } }); } </script>~~~~~~~~~~
No feedback from the reporter, without a real testcase we can't work on this.