I am binding the beforeunload
event of a page to make a quick synchronous ajax call (not best practice but it is essentially a ping so we are trying it as an idea).
$(window).bind('beforeunload', function(){
makeAjaxPUTcall();
//return 'Send a message!';
});
This works fine when built, deployed to a test environment and tested manually.
I was then trying to add some selenium tests (ChromeDriver
) to automate the testing. The Ajax call does not seem to get made. Setting a breakpoint on the browser seems to show the code running through the Ajax call and if I uncomment the return I will get an (unwanted) alert before the page unloads. I have, for sake of argument, bound the javascript function to the blur event of the page controls
$(':input').on('blur', function() { makeAjaxPUTcall(); });
and it works without problem in selenium (as well as manual testing).
When run in the beforeunload
event logging seems to show that the call never hits server.
The javascript function is like this
function makeAjaxPUTcall(){
$.ajax({
type : 'PUT',
async: false,
url: urlVal,
data: null,
processData: false,
dataType: 'json',
contentType: 'application/json',
success: function() {}
});
}
So in summary from where I'm standing I've proved, in various ways, that the code to make the REST call is working client side and server side. I have also 'proved' by manual dev testing that these calls are triggered when a page navigation takes place manually. I have also proved that the beforeunload
event is being triggered in the selenium tests (by adding a return string and seeing an alert pop up).
I have also converted the PUT to a GET and after putting a breakpoint in the test code navigated a different tab to the url provided and proved that it is triggered and does hit a breakpoint in the (java) controller code.
It SEEMS to me that the issue must be something to do with the way Selenium is handling the navigation ? Can anyone point me in the next direction ?!