Testing AJAX Applications with VSTS 2008
来源:互联网 发布:外研社unipus软件 编辑:程序博客网 时间:2024/06/07 05:33
在下面这篇文章中,Jeff Levinson介绍了如何在VSTS 2008中对AJAX应用程序进行测试。
http://visualstudiomagazine.com/articles/2007/10/18/testing-ajax-applications-with-vsts-2008.aspx
我觉得奥妙在于下面这里(一个特殊的标头 request的Header)
AJAX测试步骤中,返回的结果是比较奇怪的,如下面这样
Jeff还介绍了如何编写一个自定义的验证规则,来AJAX操作确认返回的结果是否符合期望值
using System;using System.Collections.Generic;using System.Linq;using System.Text;using Microsoft.VisualStudio.TestTools.WebTesting;using System.ComponentModel;using System.Text.RegularExpressions;namespace AjaxValidation{ [DisplayName("AJAX Field Validation")] public class AjaxValidationRule: ValidationRule { private string _control = "";private string _value = ""; [Description("Control To Validate"), DisplayName("Control Name")] public string ControlName { get { return _control; } set { _control = value; } } [Description("Value of the control to validate."), DisplayName("Value")] public string Value { get { return _value; } set { _value = value; } } public override void Validate(object sender, ValidationEventArgs e) { Regex r = new Regex("(?id=/"" + _control + "/".*>)(? .*)<"); MatchCollection m = r.Matches(e.Response.BodyString); for (int i = 0; i < m.Count; i++) { if (m[i].Groups["value"] != null) { string foundValue = m[i].Groups["value"].Captures[0].Value; e.IsValid = (foundValue == _value); break; } } } }}
或者
using System;using System.Collections.Generic;using System.Linq;using System.Text;using Microsoft.VisualStudio.TestTools.WebTesting;using System.ComponentModel;using System.Text.RegularExpressions;namespace AjaxValidation{ [DisplayName("AJAX Field Validation")] public class AjaxValidationRule: ValidationRule { private string _control = ""; [Description("Control To Validate"), DisplayName("Control Name")] public string ControlName { get { return _control; } set { _control = value; } } private string _value = ""; [Description("Value of the control to validate."), DisplayName("Value")] public string Value { get { return _value; } set { _value = value; } } public override void Validate(object sender, ValidationEventArgs e) { Regex r = new Regex("id=/"" + _control + "/".*>" + _value + "<"); MatchCollection m = r.Matches(e.Response.BodyString); e.IsValid = (m.Count > 0); } }}
将以上代码单独用一个ClassLibrary封装起来,并在TestProject中添加对其的引用,然后就可以设置下面的验证规则了
下面是演示了如果返回的字符串不满足要求的话测试会报告错误
看起来很不错对吧?
但是要注意一个情况:对于纯客户端操作,例如自己写javascript对某些元素进行操作,就不可能被记录到了。因为此时并不会发生请求(不管是POST还是GET)
上面演示的操作为什么能捕捉到,是因为虽然是AJAX,但仍然发生了对服务器的请求。其实是一个XMLHttpRequest的异步请求。
本文是使用博客同步和管理系统自动于2009/7/16 16:10:55 从 陈希章@博客园 同步过来的。原文地址: http://www.cnblogs.com/chenxizhang/archive/2009/07/16/1524958.html ,发表于2009/7/16 8:10:00.
- Testing AJAX Applications with VSTS 2008
- Testing ASP.NET Applications with NUnitASP and NUnit
- An Introduction to Testing Web Applications with twill and Selenium
- Robotium_Automated UI testing for Android applications with Robotium
- Recording and Performance testing Android applications with HP Loadrunner VuGen
- Penetration Testing : Applications
- How to Develop Web Applications with Ajax, Pt. 1
- Ajax Testing
- VSTS
- vsts
- VSTS
- VSTS
- Web Testing: Complete guide on testing web applications
- Building Applications with APEX
- Professional Pen Testing for Web Applications
- Performance Testing Guidance for Web Applications
- Unit testing with CPPUnit
- Testing with untrusted Https
- July 1st Wednesday (七月 一日 水曜日)
- 关于 c# 中SQL 事务的回滚
- lua c函数的堆栈变化标记
- July 2nd Thursday (七月 二日 木曜日)
- July 3th Friday (七月 三日 金曜日)
- Testing AJAX Applications with VSTS 2008
- PHP base64_decode+gzinflate压缩编码和解码代码
- July 6th Monday (七月 六日 月曜日)
- PHP5.3新特性之改进的性能
- 总结
- July 7th Tuesday (七月 七日 火曜日)
- July 8th Wednesday (七月 八日 水曜日)
- July 9th Thursday (七月 九日 木曜日)
- 程序员做久了是不是会很麻木?