zoukankan      html  css  js  c++  java
  • [翻译]用于捕获指向错误页的重定向的自定义有效性规则

    *这是有关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...

    OscarXie.net

    关注质量与体验——电子商务与自动化测试
    http://www.cnblogs.com/oscarxie/

  • 相关阅读:
    asp.net发送email
    把GridView控件完全放入UpdatePanel中时,实现了点击编辑、更新等按钮时,页面不再刷新,对话框不起作用
    【原】 POJ 2352 Stars 树状数组 解题报告
    【原】 POJ 2739 Sum of Consecutive Prime Numbers 筛素数+积累数组 解题报告
    【原】 POJ 2262 Goldbach's Conjecture 筛素数 解题报告
    【原】 POJ 2593 Max Sequence 动态规划 解题报告
    【原】 POJ 2159 Tree Recovery 解题报告
    【原】 POJ 3067 Japan 2D树状数组+逆序数 解题报告
    【原】 POJ 2299 UltraQuickSort 逆序数 解题报告
    【原】 POJ 2499 Binary Tree 优化经典 解题报告
  • 原文地址:https://www.cnblogs.com/oscarxie/p/947809.html
Copyright © 2011-2022 走看看