前一阵和开发的同事一起测试某个系统的性能。此系统是发送Ajax请求到后台,再调用第三方的某项服务。第三方服务的性能由不得我们控制,因此开发人员做了一下改进,超时则直接返回。于是在loadrunner这边,结果就好看的多了。当时我并没在意,后来一琢磨,这岂不是说loadrunner不检查Ajax内容就通过了?为了验证这个论断,做了个简单的示例。
首先是写个前端的html,发送Ajax到后台,再写个servlet处理请求,返回不同的内容。然后用loadrunner录制脚本并回放,以验证对Ajax内容的检查状况。
新建一个web项目,添加一个html文件和一个servlet
html内容如下:
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>用户注册</title> </head> <body> <script language="JavaScript"> var flag; function createXHR(){ if (window.XMLHttpRequest){ return new XMLHttpRequest(); }else{ return new ActiveXObject("Microsoft.XMLHTTP"); } } function checkUser(username){ if(!username==""){ var xhr=createXHR(); xhr.onreadystatechange = function(){ if (xhr.readyState == 4){ if (xhr.status == 200){ var text = xhr.responseText;// 接收返回内容 if (text == "Existed"){ flag = false; document.getElementById("msg").innerHTML = "用户名重复,无法注册!"; }else{ flag = true; document.getElementById("msg").innerHTML = "恭喜!此用户名可以注册。"; } } } }; //发送用户名到servlet xhr.open("POST","CheckName?username="+username,true); xhr.send(null); document.getElementById("msg").innerHTML = "正在验证……"; }else flag=false; } </script> <form action="CheckName" method="post" onsubmit="return flag"> 用户名:<input type="text" name="username" onblur="checkUser(this.value)"><span id="msg"></span><br> 密 码:<input type="password" name="password"><br> <input type="submit" value="注册"> </form> </body> </html>这是一个简单的模拟注册的页面,ajax发送输入的用户名到后台。
servlet处理代码如下:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub try { Thread.sleep(500); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } String username = request.getParameter("username"); PrintWriter out=response.getWriter(); if(username.equals("zhangsan")){ out.print("Existed"); }else{ out.print("Registered Successfully"); } }
loadrunner录制的脚本如下:
Action() { web_reg_find("Text=用户注册",LAST); web_url("aaa", "URL=http://192.168.1.102:8080/aaa/", "Resource=0", "RecContentType=text/html", "Referer=", "Snapshot=t1.inf", "Mode=HTML", LAST); //web_reg_find("Text=Existed",LAST); web_custom_request("CheckName", "URL=http://192.168.1.102:8080/aaa/CheckName?username=zhangsan", "Method=POST", "Resource=0", "Referer=http://192.168.1.102:8080/aaa/", "Snapshot=t2.inf", "Mode=HTML", "EncType=", LAST); //web_reg_find("Text=Registered Successfully",LAST); web_custom_request("CheckName_2", "URL=http://192.168.1.102:8080/aaa/CheckName?username=lisi", "Method=POST", "Resource=0", "Referer=http://192.168.1.102:8080/aaa/", "Snapshot=t3.inf", "Mode=HTML", "EncType=", LAST); web_submit_data("CheckName_3", "Action=http://192.168.1.102:8080/aaa/CheckName", "Method=POST", "Referer=http://192.168.1.102:8080/aaa/", "Snapshot=t4.inf", "Mode=HTML", ITEMDATA, "Name=username", "Value=lisi", ENDITEM, "Name=password", "Value=123456", ENDITEM, LAST); return 0; }注释中的两行是手工加的,加上后就可以对Ajax返回的内容进行检测了。
原文地址:http://blog.csdn.net/zjc/article/details/41988667