以下来自于今天我们的内部讨论。一个用户想要运行一个提取规则并运行一些基于此规则是否成功与否的不同请求。问题是一个失败的提取规则通常导致Web 测试也失败。幸运的是,有一个简单方法来检查该规则的成功状态,之后使用该值,并防止由于此规则导致Web测试失败。
您有如下的编码的Web测试:
public override IEnumerator<WebTestRequest> GetRequestEnumerator()
{
WebTestRequest request1 = new WebTestRequest("http://vsnc/");
request1.RecordedResponseUrl = "http://vsnc/";
ExtractText extractionRule1 = new ExtractText();
extractionRule1.StartsWith = "Logged in as ";
extractionRule1.EndsWith = ".";
extractionRule1.IgnoreCase = false;
extractionRule1.UseRegularExpression = false;
extractionRule1.Required = true;
extractionRule1.Index = 0;
extractionRule1.HtmlDecode = true;
extractionRule1.ContextParameterName = "Name";
request1.ExtractValues += new EventHandler<ExtractionEventArgs>(extractionRule1.Extract);
yield return request1;
}
你可以通过使用一个自定义的事件处理来防止Web测试失败以此来代替将提取规则直接挂接到ExtractValues事件程序.您的事件处理程序可以检查和甚至修改ExtractionEventArgs上的值,如下所示:
public override IEnumerator<WebTestRequest> GetRequestEnumerator()
{
WebTestRequest request1 = new WebTestRequest("http://vsnc/");
request1.RecordedResponseUrl = "http://vsnc/";
request1.ExtractValues += new EventHandler<ExtractionEventArgs>(request1_ExtractValues);
yield return request1;
if ((bool)this.Context["LogInNameFound"] == true)
{
//do something, issue different requests, etc.
}
}
void request1_ExtractValues(object sender, ExtractionEventArgs e)
{
ExtractText extractionRule1 = new ExtractText();
extractionRule1.StartsWith = "Logged in as ";
extractionRule1.EndsWith = ".";
extractionRule1.IgnoreCase = false;
extractionRule1.UseRegularExpression = false;
extractionRule1.Required = true;
extractionRule1.Index = 0;
extractionRule1.HtmlDecode = true;
extractionRule1.ContextParameterName = "Name";
//call the extraction rule directly
extractionRule1.Extract(sender, e);
//here's where I want to check or modify the success status of the rule
if (e.Success)
{
//set a context parameter for use later in the web test
this.Context["LogInNameFound"] = true;
}
else
{
//set a context parameter to indicate this rule failed
this.Context["LogInNameFound"] = false;
//force the rule to pass
e.Success = true;
}
}
正如您看到,插入自己的事件处理程序可以使您更多的控制提取和验证规则执行。
JoshCh发布于星期四,2006年11月16日下午11点01分