OscarXie.net

关注质量与体验——电子商务与自动化测试

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

*这是有关Web测试可扩展性系列的第二篇文章,第一篇是关于使用自定义的IHttpBody类扩展Web测试。*

一个Web应用程序捕获错误并将用户重定向到一个"对不起,出现一个错误..."页面是一个普遍经历。遗憾的是,这些错误页通常返回一个"200 OK"状态代码而不是在400或500区域的错误代码。当创建和运行Web负载测试时而它又允许Web应用程序出现被忽视的错误这种现象需要密切注意。

下面是名叫ValidateResponseUrl的检查响应URLs 来捕捉重定向到错误页的有效性验证规则示例:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using Microsoft.VisualStudio.TestTools.WebTesting;

namespace OcracokeSamples {
    public class ValidateResponseUrl : ValidationRule {
        private string _urlStringToFind = String.Empty;
        private bool _failIfFound = true;

        [Description("If true, validation fails if the specified string is found in the response URL.  If false, validation fails if the specified string is not found in the response URL.")]
        public bool FailIfFound {
            get { return _failIfFound; }
            set { _failIfFound = value; }
        }

        [Description("The string to search for in the response URL.  For example, enter 'Error.aspx' if you want to make sure the server does not redirect to that error page.")]
        public string UrlStringToFind {
            get { return _urlStringToFind; }
            set { _urlStringToFind = value; }
        }

        public override string RuleName {
            get { return "Validate Response URL"; }
        }

        public override string RuleDescription {
            get { return "Verifies the response URL.  This rule can be used to make sure a redirect to an error page does not occur, for example."; }
        }

        public override void Validate(object sender, ValidationEventArgs e) {
            //make sure the string to find has a value
            if (String.IsNullOrEmpty(_urlStringToFind)) {
                throw new ArgumentException("The UrlStringToFind property cannot be null or empty string.");
            }

            bool found = e.Response.ResponseUri.OriginalString.IndexOf(_urlStringToFind, StringComparison.OrdinalIgnoreCase) != -1;
            string foundMessage = found ? "was" : "was not";

            //set the result message that will appear in the web test viewer details tab and the load test error table
            e.Message = String.Format("The string '{0}' {1} found in the response URL.", _urlStringToFind, foundMessage);

            //set whether the validation passed or failed
            e.IsValid = found != _failIfFound;
        }
    }
}

正如您看到,它不需要多的代码来实现这个非常有用的验证规则。我只需添加一个新类到我的测试项目,使其从有效性规则继承并实现 RuleName 和验证成员。RuleDescription 属性和 [ 说明 ] 属性通过在添加有效性规则对话框 (如下所示)及其属性窗口显示的提供帮助文本使自定义验证规则易于使用,而它们完全是可选的。

查看 Validate 方法,您可能搔头疑惑于WebTestResponse.ResponseUri和WebTestRequest.Url有多么的不同。答案是当请求的 FollowRedirects 属性被设置为 True,验证并提取规则自动从原始请求移至后面的重定向请求。

在Web测试中使用自定义有效性规则,您必须首先让它存在于您的测试项目中。如果测试项目中包含规则类,编译项目后该规则将显示在添加有效性规则对话框。但是,如果这个规则是一个单独类库项目的一部分则可以共享给多个测试项目,只要编译类库项目并添加引用到您的测试项目中。现在,当您右键单击一个请求并选择添加验证规则,您可以看到您的规则在添加有效性规则对话框下。

Add Validation Rule dialog

您可以看到这样的例子如果服务器重定向到error.aspx将引起请求验证失败。如果您有多个错误页面,您可以将FailIfFound 属性设置为False 并在UrlStringToFind 属性中输入正确的响应URL ,这样如果在响应URL中找不到字符串则验证失败。

请让我知道您一般对关于有效性规则或自定义验证规则有哪些问题或评论。我也将关注下一个Web测试可扩展性示例的意见。

                             JoshCh发布于星期三,2005年11月02日下午2点17

原文地址:http://blogs.msdn.com/joshch/archive/2005/11/02/48...

posted on 2007-11-03 13:05  oscarxie  阅读(767)  评论(4编辑  收藏  举报