0

我一直在尝试从编码的 Web 测试(VS2008)中的提取规则中提取结果。我想从 testA 中的标签中提取 URL 并将其输入到 testB 的运行参数中。在“yield return WebTestRequest”语句之后,我一直在遍历 testA 的代码。这可能吗?

代码片段:

ExtractAttributeValue();
            extractionRule6.TagName = "a";
            extractionRule6.AttributeName = "href";
            extractionRule6.MatchAttributeName = "";
            extractionRule6.MatchAttributeValue = "";
            extractionRule6.HtmlDecode = true;
            extractionRule6.Required = true;
            extractionRule6.Index = 0;
            extractionRule6.ContextParameterName = "";
            extractionRule6.ContextParameterName = "fifth";
            request2.ExtractValues += new EventHandler<ExtractionEventArgs>(extractionRule6.Extract);
                  yield return request2;

大卫

4

1 回答 1

0

我想到了。通过在返回 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);
于 2013-06-26T15:00:30.400 回答