总结

Visual Studio的Web Performance Test是基于HTTP协议层的,它不依赖于浏览器,通过直接接收,发送HTTP包来和Web服务器交互。Web Performance Test发送和接收的一系列请求和响应之间存在相关性,例如,用户登录后,SID被传递给客户端,下一次请求时,需要把SID发送到服务器。因此,Web Perfomance Test 定义了多种提取规则,帮助从服务器响应中提取信息,用于之后的请求。或者保存起来,作为测试结果的一部分。

Web Performance Test提供多种提取规则,以下表格来自MSDN

提取规则的类型 说明
Selected Option 提取列表或组合框中的选定文本。
Tag Inner Text 从指定的 HTML 标记中提取内部文本。
Extract Attribute Value 从指定的 HTML 标记中提取特性的值。 有关以下内容的更多信息使用提取特性值规则的更多信息,请参见演练:向 Web 性能测试添加验证规则和提取规则。
Extract Form Field 提取响应中指定窗体字段的值。
Extract HTTP Header 提取 HTTP 标头的值。
Extract Regular Expression 从与正则表达式相匹配的响应中提取文本。
Extract Text 从响应中提取文本。
Extract Hidden Fields 从响应中提取所有的隐藏字段。

(1)(2)中,我们讲解了系统默认的一些提取规则,本文将讲解如何建立自定义提取规则,本文的代码可以从这里下载。

继承ExtractionRule

所有的提取规则,包括自定义规则都需要从ExtractionRule继承,该类在Microsoft.VisualStudio.QualityTools.WebTestFramework.dll中实现。

独立的Library

我们最好把自定义规则都放到一个独立的类库中,这样方便多个web performance test 工程引用。 web performance test 工程只要引用了该类库,在右键点击URL,选择Add Extraction Rule中,在打开的Add Extraction Rule窗口中,就可以看到所有的自定义提取规则,和用法系统默认的规则完全相同。

例子

本文继续沿用(2)中的例子,那是一个简单的算术站点:

在(2)中,我们发现Extract Regular Express规则不适合把“等于3。”中的数字提取出来,它提取的值将会包括整个文本。那么,本文将定义一个“Custom Extract Regular Express”,实现通过正则表达式提取其中的数字,而不是整个文本。

看代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using Microsoft.VisualStudio.TestTools.WebTesting;
using System.ComponentModel;
using System.Text.RegularExpressions;
using System.Web; namespace CustomExtractionRule
{
[DescriptionAttribute("Extracts the specificed group from mached regex")]
[DisplayNameAttribute("Custom Extract Regular Expression")]
public class CustomExtractRegularExpression : ExtractionRule
{ [DescriptionAttribute("Whether or not to perfom HTML decoding of extracted strings.")]
[DisplayNameAttribute("Html Decode")]
[DefaultValue(true)]
public bool HtmlDecode { get; set; } [DefaultValue(false)]
[DescriptionAttribute("Ignore case during search for matching text.")]
[DisplayNameAttribute("Ignore Case")]
public bool IgnoreCase { get; set; } [DefaultValue(0)]
[DescriptionAttribute("Indicates which occurrence of the string to extract. this is a zero-based index.")]
[DisplayNameAttribute("Index")]
public int Index { get; set; } [DescriptionAttribute("Specify the regular expression to search for.")]
[DisplayNameAttribute("Regular Expression")]
public string RegularExpression { get; set; } [DefaultValue(true)]
[DescriptionAttribute("If ture, the extraction rule fails if no value is found to extract.")]
[DisplayNameAttribute("Required")]
public bool Required { get; set; } [DefaultValue(0)]
[DescriptionAttribute("Indicates which group of the string to extract in matched regular expression. this is a zero-based index.")]
[DisplayNameAttribute("Group Index")]
public int GroupIndex { get; set; } public override void Extract(object sender, ExtractionEventArgs e)
{
String errormessage="";
String result = this.Extract(e.Response.BodyString, ref errormessage); if (!string.IsNullOrEmpty(result))
{
if (this.HtmlDecode)
{
result = HttpUtility.HtmlDecode(result);
}
e.WebTest.Context[this.ContextParameterName] = result;
}
else
{
e.Success = false;
e.Message = errormessage;
}
} internal String Extract(string document,ref string errormessage)
{
int startat = 0;
int num2 = 0;
RegexOptions options = RegexOptions.Multiline;
if (this.IgnoreCase)
{
options |= RegexOptions.IgnoreCase;
}
Regex regex = new Regex(this.RegularExpression, options);
Match selectedMatch=null;
while (startat < document.Length)
{
Match match = regex.Match(document, startat);
if (!match.Success)
{
break;
}
int num3 = match.Index + match.Length;
if (num2 == this.Index)
{
selectedMatch = match;
}
startat = num3;
num2++;
}
if (selectedMatch == null)
{
errormessage = "Matched string is not found";
return null;
} if (selectedMatch.Groups.Count - 1 < this.GroupIndex)
{
errormessage = "Matched group is not found";
return null;
} return selectedMatch.Groups[GroupIndex].Value;
}
}
}

1) 在CustomExtractRegularExpression 的类和属性上,我们用到了DisplayNameAttribute,DescriptionAttribute,DefaultValue这些Attribute,他们的作用是在VS的Add Extraction Rule窗口上配置提取规则时,定义规则的显示名和描述,以及每个属性的显示名,描述和默认值。

2)提取规则通过重载void Extract(object sender, ExtractionEventArgs e) 方法来实现。如果提取成功,把e.Success 设置为true,并且把提取的参数值保存在e.WebTest.Context[this.ContextParameterName]中;否则e.Success设置为false,并在e.Message中填入失败的消息。

3)Custom Extract Regular Expression规则,相对于Extract Regular Expression规则,我们增加了一个Group Index参数,允许用户从特定的正则表达式匹配中,选中匹配的group,关于正则表达式group,可以参考MSDN

应用自定义规则

现在,我们把规则添加到web performance test中,我们用Custom Extract Regular Expression来替换在(2)中我们使用的Extract Text规则来提取结果中的数值。属性配置如下:

注意,"Group Index”参数应该设置为1

本文由知平软件刘斌华原创,转载请注明出处。

知平软件致力于移动平台自动化测试技术的研究,我们希望通过向社区贡献知识和开源项目,来促进行业和自身的发展。

Visual Studio的Web Performance Test提取规则详解(3)的更多相关文章

  1. Visual Studio的Web Performance Test提取规则详解(2)

    总结 Visual Studio的Web Performance Test是基于HTTP协议层的,它不依赖于浏览器,通过直接接收,发送HTTP包来和Web服务器交互.Web Performance T ...

  2. Visual Studio的Web Performance Test提取规则详解(1)

    总结 Visual Studio的Web Performance Test是基于HTTP协议层的,它不依赖于浏览器,通过直接接收,发送HTTP包来和Web服务器交互.Web Performance T ...

  3. 转:Visual Studio进行Web性能测试- Part I

    原文作者:Ambily.raj Visual Studio是可以用于性能测试的工具之一.Visual Studio Test版或Visual Studio 2010旗舰版为自动化测试提供了支持.本文介 ...

  4. Visual Studio进行Web性能测试- Part I

    Visual Studio进行Web性能测试- Part I 2012-08-29 08:01 by 知平软件, 5356 阅读, 9 评论, 收藏, 编辑 原文作者:Ambily.raj Visua ...

  5. Visual Studio 使用 Web Deploy 发布远程站点

    Ø  简介 本文介绍 Visual Studio 如何使用 Web Deploy发布远程站点,有时候我们开发完某个功能时,需要快速将更改发布至服务器.通常 Visual Studio 可以采用两种方式 ...

  6. Visual Studio进行Web性能测试- Part II

    Visual Studio进行Web性能测试- Part II 2012-08-31 14:34 by 知平软件, 7557 阅读, 5 评论, 收藏, 编辑 原文作者:Ambily.raj 对于一个 ...

  7. Visual Studio 2013 Web开发

    cnbeta新闻:微软正式发布Visual Studio 2013 RTM版,微软还发布了Visual Studio 2013的最终版本..NET 4.5.1以及Team Foundation Ser ...

  8. Visual Studio 2013 Web开发、新增功能:“Browser Link”

    微软正式发布Visual Studio 2013 RTM版,微软还发布了Visual Studio 2013的最终版本..NET 4.5.1以及Team Foundation Server 2013. ...

  9. Visual Studio 2013 Web开发、新增功能:“Browser Link”

    微软正式发布Visual Studio 2013 RTM版,微软还发布了Visual Studio 2013的最终版本..NET 4.5.1以及Team Foundation Server 2013. ...

随机推荐

  1. Spring详细总结

    Spring的特性之一:IOC(控制反转Inverse Of Control),又称依赖注入,是一种重要的面向对象编程的法则来削减计算机程序的耦合问题 也是轻量级spring框架的核心: 依赖注入: ...

  2. CSS学起来并不难

    CSS CSS学起来并不难,但在大型项目中,就变得难以管理,特别是不同的人在CSS书写风格上稍有不同,团队上就更加难以沟通,为此总结了一些如何实现高效整洁的CSS代码原则: 1. 使用Reset但并非 ...

  3. sax解析案例(javabean封装xml文档数据)

    package itcast.sax; import java.io.IOException; import java.util.List; import javax.xml.parsers.Pars ...

  4. 【iCore3 双核心板_FPGA】例程四:Tcl脚本实验——配置引脚

    实验指导书及代码包下载: http://pan.baidu.com/s/1pJZDz0v iCore3 购买链接: https://item.taobao.com/item.htm?id=524229 ...

  5. OAuth2.0 微博登陆网站功能的实现(一)获取用户授权及令牌 Access Token

    在登陆一些网站的时候,可以选择登陆方式为第三方登陆,例如微博登陆,以爱奇艺为例,进入首页,点击 ”登陆“,会弹出登录框: 除了本站登陆外,还可以选择其他第三方登陆,比如微博登陆.QQ 登陆.微信登陆等 ...

  6. Intelligencia.UrlRewriter在IIS 7.0下的完全配置攻略

    在项目中,之前公司是使用IIS 7.0官方的URL重写模块,官方的使用说明请参见官方URLRewrite  ,添加伪静态支持,后来经理问我有没有涉及伪静态,我说之前项目中我一直是用Intelligen ...

  7. lua2c

    lua2c lua2c is a Lua module and utility to convert Lua 5.1 source code to C API code. http://lua-use ...

  8. RDIFramework.NET ━ 9.8 用户权限管理 ━ Web部分

    RDIFramework.NET ━ .NET快速信息化系统开发框架 9.8 用户权限管理 -Web部分 在实际应用中我们会发现,权限控制会经常变动,如:需要调整角色的分配,需要收回与授予某些角色.用 ...

  9. django使用gmail

    POSTED ON 02 JUL 2007 IN DEVELOPMENT DJANGO PYTHON WEBDid a bit of running around today to get Djang ...

  10. 记一次奇怪IE动态加载js的乱码

    1. 问题背景 某个老产品需要支持IE8,前端部分组件采用scrat开发体系进行开发的,当页面中内嵌的iframe的页面再加载组件js的时候,某些情况下会出现组件的js乱码,导致组件的js不能运行.而 ...