zoukankan      html  css  js  c++  java
  • [翻译]自定义提取规则通过索引提取窗体字段

    *这是Web测试可扩展性系列的第三篇文章。第一篇文章是关于为编码的Web测试创建自定义的IHttpBody类和第二篇文章是关于用于捕获指向错误页的重定向的自定义有效性规则。*

    通过提取一个页面上的每个隐藏字段大Web测试上下文中的ExtractHiddenFields 规则出现在大多数的Web测试工作中。这些隐藏字段的命名约定一般是$HIDDENx.y,其中x是ExtractHiddenFields (通常为"1")上的上下文参数名,y 是隐藏的字段名称。此约定适合大多数 web 页,但它不能区分具有相同名称的多个隐藏字段,比如一个页面可能有多个窗体。幸运的是,这种情况下可以被具有以下自定义提取规则和一些对Web 测试小的修改支持。

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

    namespace OcracokeSamples {
        public class ExtractFormFieldWithIndex : ExtractionRule {
            private string _name;
            private int _index = 1;

            [Description("The name of the form field to extract.")]
            public string Name {
                get { return _name; }
                set { _name = value; }
            }

            [Description("The index of the form field.  For example, specifying '2' would " +
                "extract the value of the second form field with the given name.")]
            public int Index {
                get { return _index; }
                set { _index = value; }
            }

            public override string RuleName {
                get { return "Extract Form Field with Index"; }
            }

            public override string RuleDescription {
                get {
                    return "Extracts a form field from a page based on the order it appears.  " +
                    "Useful when a page contains multiple forms with fields of the same name.";
                }
            }

            public override void Extract(object sender, ExtractionEventArgs e) {
                //if the response is not HTML, display an error
                if (!e.Response.IsHtml) {
                    e.Success = false;
                    e.Message = "The response did not contain HTML.";
                    return;
                }

                string formFieldValue = null;
                int currentIndex = 0;

                //examine each input tag
                foreach (HtmlTag tag in e.Response.HtmlDocument.GetFilteredHtmlTags("input")) {
                    if (String.Equals(tag.GetAttributeValueAsString("name"), _name, StringComparison.OrdinalIgnoreCase)) {
                        currentIndex++;

                        if (currentIndex == _index) {
                            formFieldValue = tag.GetAttributeValueAsString("value");
                            //if the form field was found, but had no value property, set the value to empty string
                            if (formFieldValue == null) {
                                formFieldValue = String.Empty;
                            }

                            break;
                        }
                    }
                }

                if (formFieldValue != null) {
                    e.WebTest.Context.Add(this.ContextParameterName, formFieldValue);
                    e.Success = true;
                    return;
                } else {
                    e.Success = false;
                    e.Message = String.Format("Form field named '{0}' with index '{1}' was not found.", _name, _index);
                }
            }
        }
    }

    如果您阅读过我的早期发布的有关创建自定义验证规则,您将认识到创建一个自定义提取规则是几乎相同。您可以简单地创建一个从ExtractionRule 派生类并在提取方法中实现您的提取逻辑。提取方法中的ExtractionEventArgs 参数提供对WebTestResponse 包含从服务器接收到所有响应数据的访问。设置e.Success报告规则是否成功或失败以及e.Message 允许您提供一个将显示在Web 测试结果查看器或监视负载测试中的错误信息。

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

    Add Extraction Rule dialog

    要使用此提取规则,您输入一个窗体字段名称并指定索引,以确定哪个匹配项要提取 (1 为第一个,2 表示第二个,等) 该窗体域。一旦您为您正在提取的窗体字段指定一个上下文参数名并将这个提取规则添加到一个请求,您只需要更新与这个窗体字段对应的参数。为这个新的上下文参数绑定参数值以此来替代$HIDDENx.y。

    就是这样。有任何关于实现自定义提取规则或使用这个示例规则的问题吗?

              JoshCh发布于星期四,2005年11月17日下午5点04

    原文地址:http://blogs.msdn.com/joshch/archive/2005/11/17/49...

    OscarXie.net

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

  • 相关阅读:
    ReenTrantLock可重入锁(和synchronized的区别)总结
    什么是死锁?如何解决死锁?【转载】
    android studio 完整安装教程,已完全实践过
    foxmail创建163公司企业邮箱的时候会出现ERR Unable to log on
    Android 开发 res里面的drawable(ldpi、mdpi、hdpi、xhdpi、xxhdpi)
    android studio 突然出现Gradle project sync failed 错误
    android 开发edittext获取焦点时hint消失
    Z480联想笔记本突然没有了声音
    android聊天,存储聊天记录sqlite
    adb shell出现error错误
  • 原文地址:https://www.cnblogs.com/oscarxie/p/953865.html
Copyright © 2011-2022 走看看