Last weekend took part in the Qclub Baidu Technology Salon , listened to Baidu Sun Jingwei said the Web automation test , speak very good, and then in the group discussion was fortunate to sit together. One of the things we're talking about is that Ajax applications are more difficult to test than the original non-Ajax program, and there are two main reasons.
First: Ajax makes it impossible to work on a purely recording basis, because recording an operation, this return takes 5 seconds, and the next time it will be more or less.
Second: Some test tools are based on the original non-AJAX program developed , and did not do the corresponding upgrade.
However, there are also many tools that support Ajax testing, and today we'll look at how the famous Selenium test ajax programs.
Preparing AJAX Programs
Here we use ASP. NET MVC, and you can actually use any program to emulate an Ajax call.
A. Prepare a page where we use jquery.
?
12345678910111213 |
< input id = "GetContent" value = "Get" type = "button" onclick = "GetAjaxContent();" />
< div id = "results" style = "border:solid 1px red; display:none;" > </ div > < script type = "text/javascript" > function GetAjaxContent() { $.ajax({ url: "Home/GetAjaxContent", success: function (html) { $("#results").show().append(html); } }); } </ script > |
B. Prepare the daemon and add the following code to HomeController. ? 1 2 3 4 5 public Contentresult getajaxcontent () {thread.sleep (9000), return new Contentresult {Content = Hello world}; } c. Effect when I
B. Prepare the daemon and add the following code to HomeController.
?
12345 |
public ContentResult GetAjaxContent() { Thread.Sleep(9000); return new ContentResult { Content = "Hello World" }; } |
C. Effects when we click Get, the page appears Hello world.
Using Selenium for AJAX testing
If you don't use a selenium, please refer to my article Web test: Selenium using
If we click the Get button to determine if the page is going to return "Hello world", it will go wrong, because it is not immediately returned.
At this point we need to use Selenium's Waitforcondition method, which will not stop to determine whether a JavaScript expression is true. Resumes execution until it returns true.
We can see that we need to write the JS expression, if we need to judge the complexity of the expression is also a very troublesome thing, and this method through my use, it is not good for IE support. So is there a better way?
A better way to tell if an Ajax call is over
In fact, the biggest problem in our testing of AJAX programs is to decide whether the Ajax call is over or not, as above we're judging if "Hello world" appears in the page and it proves that Ajax is over, so what's a better way? The JavaScript class library We used to use is jquery, how can we tell if the Ajax call to jquery is over?
The study found that when there is no Ajax call, Jquery.active=0.
So, we can use this universal expression to determine whether Ajax returns.
Selenium. Waitforcondition ("Selenium.browserbot.getCurrentWindow (). jquery.active = = 0", "50000");
Is the whole world a lot quieter?
Now I've collected the status of AJAX activities that determine other class libraries
JQuery: "Jquery.active"
Prototype: "Ajax.activerequestcount"
Dojo: "Dojo.io.XMLHTTPTransport.inFlight.length"
ASP. NET AJAX:
JavaScript:
function isInAsyncPostBack () {
Instance = Sys.WebForms.PageRequestManager.getInstance ();
return Instance.get_isinasyncpostback ();
}
C#
Selenium. Waitforcondition ("!selenium.browserbot.getcurrentwindow (). isInAsyncPostBack ()", "1000″");
Translated from: http://www.ltesting.net
Selenium Test Ajax Program (GO)