Visual Studio WebTestRequest

本文关键字:WebTestRequest Studio Visual | 更新日期: 2023-09-27 18:24:22

我需要一些关于visual studio 上WebTestRequest的帮助

我有两个请求,第二个请求需要来自第一个请求响应的一些数据

如何使用从第一次响应到第二次请求所需的特定数据?(在我的情况下是Sessionid

测试请求:

{
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.VisualStudio.TestTools.WebTesting;

public class WebTest1Coded : WebTest
{
    public WebTest1Coded()
    {
        this.PreAuthenticate = true;
        this.Proxy = "default";
    }
    public override IEnumerator<WebTestRequest> GetRequestEnumerator()
    {
        WebTestRequest request1 = new WebTestRequest("https://xxx.xxx.com/api/xxx");
        request1.Method = "POST";
        request1.QueryStringParameters.Add("format", "json", false, false);
        StringHttpBody request1Body = new StringHttpBody();
        request1Body.ContentType = "application/json";
        request1Body.InsertByteOrderMark = false;
        request1Body.BodyString = "{'"UserName'":'"xxx'",'"Password'":'"xxx'"}";
        request1.Body = request1Body;
        yield return request1;
        request1 = null;
        WebTestRequest request2 = new WebTestRequest("https://xxx.xxx.com/api/xxx");
        request2.Method = "POST";
        request2.QueryStringParameters.Add("format", "json", false, false);
        StringHttpBody request2Body = new StringHttpBody();
        request2Body.ContentType = "application/json";
        request2Body.InsertByteOrderMark = false;
        request2Body.BodyString = @"
          RequestHeader"": 
  ""SessionId"""": ""xxx"",
  ""ApiKey"""": ""xxx""
        request2.Body = request2Body;
        yield return request2;
        request2 = null;
    }   
}

Visual Studio WebTestRequest

在原始Web测试文件中比在编码Web测试中更容易做到这一点。只需在第一个请求中添加提取规则。它将值保存到上下文参数中,然后可以在第二个请求中使用该参数。

如果你真的想使用编码Web测试,那么你可以创建一个具有适当提取规则和上下文参数用法的沙箱Web测试。然后将其转换为Coded,并将感兴趣的部分复制到测试中。

提取abcdef之间的文本并将结果存储在acbdef上下文参数中的ExtractText规则生成了以下代码。

WebTestRequest request3 = new WebTestRequest("http://localhost/");
request3.ThinkTime = 1;
request3.ParseDependentRequests = false;
ExtractText extractionRule1 = new ExtractText();
extractionRule1.StartsWith = "abc";
extractionRule1.EndsWith = "def";
extractionRule1.IgnoreCase = false;
extractionRule1.UseRegularExpression = false;
extractionRule1.Required = true;
extractionRule1.ExtractRandomMatch = false;
extractionRule1.Index = 0;
extractionRule1.HtmlDecode = true;
extractionRule1.SearchInHeaders = false;
extractionRule1.ContextParameterName = "abcdef";
request3.ExtractValues += new EventHandler<ExtractionEventArgs>(extractionRule1.Extract);
yield return request3;
request3 = null;

其他提取规则具有不同的属性,这些属性需要给定值。我建议把你想要的主要细节放在网络测试中,然后生成代码,找出哪些C#代码是必要的。

提取的值用于下一个请求的三个位置:

WebTestRequest request4 = new WebTestRequest(
    ("http://localhost/" + (this.Context["abcdef"].ToString() + "/more")));
request4.ThinkTime = 1;
request4.ParseDependentRequests = false;
request4.Headers.Add(new WebTestRequestHeader("Use-abcdef-again",
   ("pqr" + (this.Context["abcdef"].ToString() + "stu"))));
request4.Headers.Add(new WebTestRequestHeader("Use-abcdef",
    this.Context["abcdef"].ToString()));
yield return request4;
request4 = null;