四、编辑WebTest
1、QueryString Parameters & Form Post Parameters
2、添加验证规则和自定义验证规则
1、QueryString Parameters & Form Post Parameters查询字符串参数和窗体发布参数
查询字符串参数一般是URL中如XXX.asp?问号后的内容,窗体发布参数是请求页面中Form中的内容,同时也可以
提取出隐藏的窗体参数,如AllGiftPwds={{$HIDDEN1.AllGiftPwds}},这些参数可以使用数据绑定来进行参数化,在后面会介绍到。
2、添加验证规则和自定义验证规则
添加验证规则:
预定义的规则包括以下几个:
查找文本
窗体区域
最大请求时间
请求属性值
所需的标记
验证级别
可以使用请求的“验证级别”来控制在特定负载测试中使用哪些验证规则。可以将每个规则的验证级别设置为“低”、“中”或“高”。通常,所设置的验证级别越高,测试的运行速度就越慢。
见下图:
验证规则的执行对性能有一定影响。
将负载测试设置设为“低”可执行最少的请求设置级别,适合重载测试和压力运行。将负载测试设置设为“高”可执行最多的规则,适合轻量负载测试期间使用。
自定义验证规则:
通过从 ValidationRule 类派生,可以创建自己的验证规则。
在VSTS For Testers中提供了Test API,因此可以通过建立类库,生成dll文件引用。
1、创建一个自定义验证规则的类库项目
2、在该类库项目中,添加一个对 Microsoft.VisualStudio.TestTools.WebTesting.dll 的引用。
3、创建一个从 ValidationRule 类派生的类。实现
using System;
using System.Diagnostics;
using System.Globalization;
using Microsoft.VisualStudio.TestTools.WebTesting;
namespace SampleWebTestRules
{
//*******************************************************************
//The Required Tag Validation Rule is used to check that an HTML tag
// with a particular name is found one or more times in the HTML
// response.
//********************************************************************
public class CustomValidateTag : ValidationRule
{
//*********************************************************************
// Name of the HTML tag that must exist in the HTML document in order
// for the validation rule to succeed.
//*********************************************************************
public string RequiredTagName
{
get { return requiredTagName; }
set { requiredTagName = value; }
}
//*********************************************************************
// The minimum number of times the HTML tag that must exist in the HTML
// document for the validation rule to succeed.
// ********************************************************************
public int MinOccurrences
{
get { return minOccurrences; }
set { minOccurrences = value; }
}
//***************************************************************
/// Specify a user readable name for use in the user interface.
//*****************************************************************
public override string RuleName
{
get { return "Custom Validate Tag"; }
}
public override string RuleDescription
{
get { return "Validates that the specified tag exists on the page."; }
}
//*******************************************************************
// Validate is called with the test case Context and the
// request Context.
// These allow the rule to examine both the request and the response
// and make sure that the data matchs the required values.
//********************************************************************
public override void Validate(object sender, ValidationEventArgs e)
{
bool validated = false;
int numTagsFound = 0;
foreach (HtmlTag tag in e.Response.HtmlDocument.GetFilteredHtmlTags(requiredTagName))
{
Debug.Assert(string.Equals(tag.Name, requiredTagName, StringComparison.InvariantCultureIgnoreCase));
if ( ++numTagsFound >= minOccurrences )
{
validated = true;
break;
}
}
e.IsValid = validated;
// On failure, set the errorText
if ( !validated )
{
if (numTagsFound > 0)
{
e.Message = String.Format("Only found {0} occurences of the tag", numTagsFound);
}
else
{
e.Message = String.Format("Did not find any occurances of tag '{0}'", requiredTagName);
}
}
}
//*******************************************************************
// Private Members
//*********************************************************************
// Name of the tag required
private string requiredTagName;
// Minimum number of times the tag must appear
private int minOccurrences;
}
}
4、Build类库,生成dll文件
5、在测试项目中,添加对包含自定义验证规则的类库项目的引用。
6、在“添加验证规则”对话框中就可以显示自定义验证规则
7、CustomeValidateTag Demo下载。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix