luning (owner)

Revisions

gist: 111525 Download_button fork
public
Description:
waiting for ajax call in selenium test
Public Clone URL: git://gist.github.com/111525.git
waiting for ajax call in selenium test
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/* javascript code in client side */
// extend jQuery ajax with the capability of remembering the count of active ajax requests
$.activeAjaxRequestCount = 0;
 
$().ajaxSend(function() {
  $.activeAjaxRequestCount++;
  // register lazily to make sure it's the last handler of ajaxError event and get executed after all production handlers
  if(!$._ajaxErrorHandlerAdded) {
    $().ajaxError(function() {
      $.activeAjaxRequestCount--;
    });
    $._ajaxErrorHandlerAdded = true;
  }
})
.ajaxSuccess(function() {
  $.activeAjaxRequestCount--;
});
 
 
/* C# code in selenium tests */
 
// run this js code somehow before selenium test to turn off animation of jQuery
$.fx.off = true;
 
// an overload with 60 seconds as default timeout, this is the mostly used version
public void Click(string locator)
{
  Click(locator, 60000);
}
// this overload has more control on timeout
public void Click(string locator, int timeout)
{
  // decorate selenium call with waiting logic
  WaitComplete(() => selenium.Click(locator), timeout);
}
// generic decorating helper for selenium calls
private void WaitComplete(Action action, int timeout)
{
  // do default selenium action
  action();
  // wait for active ajax request count becomes zero, none ajax selenium action will return immediately
  selenium.WaitForCondition("selenium.browserbot.getCurrentWindow();window.jQuery.activeAjaxRequestCount===0;", timeout.ToString());
}
 
/* do the same trick to Select, Type, FireEvent and more selenium methods */