Selenium test Ajax Program

Source: Internet
Author: User

I attended the qclub's Baidu technology salon last weekend. After listening to Baidu's sun Jingwei, I spoke a lot about Web automated testing. Then I had the honor to sit together during the group discussion. The content we discuss is the Ajax application.ProgramIt is more difficult to test than the original non-Ajax program. There are two main reasons.

First, Ajax makes work impossible based on the pure recording method. Because of recording an operation, it will take 5 seconds to return this operation, and more or less will be returned next time.

Second, some test tools are developed based on non-Ajax programs and are not upgraded later.

However, there are also many tools that support Ajax testing. Today, let's take a look at how the famous Selenium tests Ajax programs.

Prepare Ajax programs

Here we use ASP. net mvc. In fact, you can use any program to simulate an Ajax call.

A. Prepare a page. Here we use jquery.

<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 background program andCodeAdd homecontroller.

 
Public contentresult getajaxcontent () {thread. Sleep (9000); return New contentresult {content = "Hello World "};}

C. effect when we click get, the page displays Hello world.

Use selenium for Ajax Testing

If you do not use selenium, please refer to my articleArticleWeb Testing: Selenium usage

If we click the get button and directly judge whether the page returns "Hello World", this will cause an error because no response is returned now.

In this case, we need to use the waitforcondition method of Selenium. This method will constantly judge whether a javascript expression is true. The execution will continue only when the return value is true.

 

We can see that we need to write JS expressions. If we need to make a complicated judgment, some expressions are also very troublesome. At the same time, this method has been used by me, it does not support ie well. Is there any better way?

Better Method to Determine whether Ajax call is over

In fact, the biggest trouble for testing Ajax programs is to determine whether the Ajax call is over like above. The above is to determine whether "Hello World" appears on the page to prove that AJAX is over, so what is a better method? The javascript class library we often use is jquery. How can we determine that jquery's Ajax call is over?

After research, we found that jquery. Active = 0.

Therefore, we can use this omnipotent expression to determine whether Ajax returns.

Selenium. waitforcondition ("Selenium. browserbot. getcurrentwindow (). jquery. Active = 0", "50000 ");

Is the whole world much quieter?

 

I have collected the following information to determine the Ajax activity status of 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 ″);

 

 

 

Finally, make a small advertisement, recruit web testers, have Web Testing experience, good English skills, clear thinking, be able to understand customer needs (not dependent on developers) and write good test cases. Programming experience is preferred, Web Service testing experience is preferred, work attitude is required to be serious, positive and good scholars. The salary range is 5-8 K. If you are interested or have an Introducer within two weeks of employment, please contact me and my mailboxWangdeshui@gmail.com

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.