Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
Simplify AjaxCounterBean
Browse files Browse the repository at this point in the history
Based on revised code from http://stackoverflow.com/a/4411986/14379

Also removing setTimeout support: the timeoutCounter is not used, and
  the wrapper code is causing problems.
  • Loading branch information
seanf committed Dec 1, 2015
1 parent 5598c36 commit 1ec6644
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 52 deletions.
25 changes: 4 additions & 21 deletions functional-test/src/main/java/org/zanata/page/AbstractPage.java
Expand Up @@ -185,30 +185,13 @@ public boolean apply(WebDriver input) {
logFinished(msg);
}

/**
* Wait for any AJAX and timeout requests to return.
*/
public void waitForAbsolutePageSilence() {
waitForPageSilence(true);
}

/**
* Wait for any AJAX (but not timeout) requests to return.
*/
public void waitForPageSilence() {
waitForPageSilence(false);
}

/**
* Wait for any AJAX/timeout requests to return.
*/
private void waitForPageSilence(boolean includingTimeouts) {
final String script;
if (includingTimeouts) {
script = "return XMLHttpRequest.active + window.timeoutCounter";
} else {
script = "return XMLHttpRequest.active";
}
public void waitForPageSilence() {
// TODO wait for any short-lived timeouts to expire (eg less than 1000 ms)
// but not multi-second timeouts (eg global faces messages)
final String script = "return XMLHttpRequest.active";
// Wait for AJAX/timeout requests to be 0
waitForAMoment().withMessage("page silence").until(new Predicate<WebDriver>() {
@Override
Expand Down
49 changes: 18 additions & 31 deletions zanata-war/src/main/java/org/zanata/action/AjaxCounterBean.java
Expand Up @@ -34,40 +34,27 @@
@Name("ajaxCounter")
@Slf4j
public class AjaxCounterBean {
// NB this doesn't handle setInterval or setTimeout
// TODO handle XHR.abort (NB: readystatechange listener triggers on Chrome 45, despite MDN docs)
// http://stackoverflow.com/questions/4410218/trying-to-keep-track-of-number-of-outstanding-ajax-requests-in-firefox
private static final String AJAX_COUNTER_SCRIPT = "<script type=\"application/javascript\">" +
private static final String AJAX_COUNTER_SCRIPT = "<script type=\"application/javascript\">\n" +
"(function(xhr) {\n" +
" if (xhr.active === undefined) {\n" +
" xhr.active = 0;\n" +
" var pt = xhr.prototype;\n" +
" pt._send = pt.send;\n" +
" pt.send = function() {\n" +
" XMLHttpRequest.active++;\n" +
" this._onreadystatechange = this.onreadystatechange;\n" +
" this.onreadystatechange = function(e) {\n" +
" if ( this._onreadystatechange ) {\n" +
" var fn = this._onreadystatechange.handleEvent || this._onreadystatechange;\n" +
" fn.apply(this, arguments);\n" +
" }\n" +
" if ( this.readyState == 4 ) {\n" +
" XMLHttpRequest.active--;\n" +
" }\n" +
" };\n" +
" this._send.apply(this, arguments);\n" +
" if (xhr.active === undefined) {\n" +
" xhr.active = 0;\n" +
" var pt = xhr.prototype;\n" +
" var _send = pt.send;\n" +
" pt.send = function() {\n" +
" xhr.active++;\n" +
" this.addEventListener('readystatechange', function(e) {\n" +
" if ( this.readyState == XMLHttpRequest.DONE ) {\n" +
" setTimeout(function() {\n" +
" xhr.active--;\n" +
" }, 1);\n" +
" }\n" +
" });\n" +
" _send.apply(this, arguments);\n" +
" }\n" +
" }\n" +
" window.timeoutCounter = 0;\n" +
" window.originalSetTimeout = window.setTimeout;\n" +
" window.setTimeout = function(func, delay, params) {\n" +
" window.timeoutCounter++;\n" +
" window.originalSetTimeout(window.timeoutCallback, delay, [func, params]);\n" +
" }\n" +
" window.timeoutCallback = function(funcAndParams) {\n" +
" window.timeoutCounter--;\n" +
" func = funcAndParams[0];\n" +
" params = funcAndParams[1];\n" +
" func(params);\n" +
" }\n" +
" }\n" +
"})(XMLHttpRequest);\n" +
"</script>\n";

Expand Down

0 comments on commit 1ec6644

Please sign in to comment.