我想到了。通过在返回 WebTestRequest 后直接访问 Context 对象。我使用了 Context 的 TryGetValue 方法。下面添加的代码。我的提取规则名为“firstprj”。如果没有找到 URL,我还可以使用此方法返回的 bool 跳过所有剩余的 WebTestRequests。
WebTestRequest request2 = new WebTestRequest("http://tramdev/MyProjects.aspx");
request2.Method = "POST";
request2.Headers.Add(new WebTestRequestHeader("x-microsoftajax", "Delta=true"));
FormPostHttpBody request2Body = new FormPostHttpBody();
request2Body.FormPostParameters.Add("ctl00$cphMain$SM", "ctl00$cphMain$pnlLoadCurtain|ctl00$cphMain$btnSearch");
request2Body.FormPostParameters.Add("__EVENTTARGET", this.Context["$HIDDEN1.__EVENTTARGET"].ToString());
request2Body.FormPostParameters.Add("__EVENTARGUMENT", this.Context["$HIDDEN1.__EVENTARGUMENT"].ToString());
request2Body.FormPostParameters.Add("__LASTFOCUS", this.Context["$HIDDEN1.__LASTFOCUS"].ToString());
request2Body.FormPostParameters.Add("__VIEWSTATE", this.Context["$HIDDEN1.__VIEWSTATE"].ToString());
request2Body.FormPostParameters.Add("__EVENTVALIDATION", this.Context["$HIDDEN1.__EVENTVALIDATION"].ToString());
request2Body.FormPostParameters.Add("ctl00$cphMain$txtProjectName", "");
request2Body.FormPostParameters.Add("ctl00$cphMain$txtOpenStartDate", "");
request2Body.FormPostParameters.Add("ctl00$cphMain$txtOpenEndDate", "");
request2Body.FormPostParameters.Add("ctl00$cphMain$ddlAOR", "--All--");
request2Body.FormPostParameters.Add("ctl00$cphMain$ddlSubAOR", "--All--");
request2Body.FormPostParameters.Add("ctl00$cphMain$ddlContact", "0");
request2Body.FormPostParameters.Add("ctl00$cphMain$lstProjectStatus", "Applied for Charter");
request2Body.FormPostParameters.Add("ctl00$cphMain$gvPrjSearch$ctl02$txtstatus", "");
request2Body.FormPostParameters.Add("ctl00$cphMain$gvPrjSearch$ctl03$txtstatus", "");
request2Body.FormPostParameters.Add("ctl00$cphMain$gvPrjSearch$ctl06$txtstatus", "");
request2Body.FormPostParameters.Add("__ASYNCPOST", "true");
request2Body.FormPostParameters.Add("ctl00$cphMain$btnSearch", "Search");
request2.Body = request2Body;
ExtractAttributeValue extractionRule2 = new ExtractAttributeValue();
extractionRule2.TagName = "a";
extractionRule2.AttributeName = "href";
extractionRule2.MatchAttributeName = "";
extractionRule2.MatchAttributeValue = "";
extractionRule2.HtmlDecode = true;
extractionRule2.Required = true;
extractionRule2.Index = 5;
extractionRule2.ContextParameterName = "";
extractionRule2.ContextParameterName = "firstprj";
request2.ExtractValues += new EventHandler<ExtractionEventArgs>(extractionRule2.Extract);
yield return request2;
request2 = null;
// get prjId of first record in project list (href in extracted attribute value)
bool urlFound;
object firstProjectUrlObj;
string prjId;
urlFound = Context.TryGetValue("firstprj", out firstProjectUrlObj);