简体   繁体   English

如何在代码测试中获得响应主体?

[英]how to get the response body in code webtest?

I wrote a webtest that calls a web-service. 我写了一个称为网络服务的网络测试。

I want to get the response body and do some validation on it. 我想获取响应主体并对其进行一些验证。

    public override IEnumerator<WebTestRequest> GetRequestEnumerator()
    {


        WebTestRequest request2 = new WebTestRequest("webservice");

        request2.Headers.Add("Content-Type", "application/json");
        request2.Method = "POST";
        request2.Encoding = System.Text.Encoding.GetEncoding("utf-8");
        StringHttpBody request2Body = new StringHttpBody();

        request2Body.ContentType = "application/json";
        request2Body.InsertByteOrderMark = false;
        request2Body.BodyString = @"{                                       <body>}";
        request2.Body = request2Body;


        WebTestResponse res = new WebTestResponse();
        console.WriteLine(res.BodyBytes);

       yield return request2;

       request2 = null;
    }

When i ran the above code i didn't get any response on my console. 当我运行上面的代码时,我的控制台上没有任何响应。

How can i get the response body using coded webtest? 如何使用编码的Webtest获得响应正文?

There are at least three problems with the code in the question 问题中的代码至少存在三个问题

  1. The code in the question does not perform the request before doing the WriteLine . 问题中的代码在执行WriteLine之前不会执行请求。 The two statements WebTestResponse res = new WebTestResponse(); 这两个语句WebTestResponse res = new WebTestResponse(); and console.WriteLine(res.BodyBytes); console.WriteLine(res.BodyBytes); just create a new WebTestResponse object (with all default values) and then try to print part of its contents. 只需创建一个新的WebTestResponse对象(具有所有默认值),然后尝试打印其部分内容即可。 The request is issued by the code that calls your GetRequestEnumerator method. 该请求是由调用GetRequestEnumerator方法的代码发出的。

  2. The console object is not defined. 未定义console对象。 The normal console has a first letter uppercase, ie Console . 普通控制台的首字母大写,即Console

  3. When a web test executes I am not sure where its "console" output will go. 当执行网络测试时,我不确定其“控制台”输出将到达何处。 The standard output of a web test is not, as far as I know, a well defined thing. 据我所知,Web测试的标准输出不是定义明确的东西。

An easy way to get at the response body is to use the PostRequest method of a WebTestRequestPlugin . 获得响应正文的一种简单方法是使用WebTestRequestPluginPostRequest方法。 For a start 作为一个开始

public class BodyContentsDemo : WebTestRequestPlugin
{
    public override void PostRequest(object sender, PostRequestEventArgs e)
    {
        byte[] bb = e.Response.BodyBytes;
        string ss = e.Response.BodyString;

        e.WebTest.AddCommentToResult(
            "BodyBytes is " +
            bb == null ? " null"
            : bb.Length.ToString() + " bytes");

        e.WebTest.AddCommentToResult(
            "BodyString is " +
            ss == null ? "null"
            : ss.Length.ToString() + " chars");

        // Use bb or ss.
    }
}

Note the use of AddCommentToResult to provide logging information to the web test results log. 请注意,使用AddCommentToResult将日志记录信息提供给Web测试结果日志。

Finally I am able to find a solution for last couple of days I was struggling to capture the response text from Web Performance test. 最终,我能够在过去几天努力寻找从Web Performance测试中获取响应文本的解决方案。 Hope this helps 希望这可以帮助

public override IEnumerator GetRequestEnumerator() { 公共重写IEnumerator GetRequestEnumerator(){

    WebTestRequest request2 = new WebTestRequest("webservice");

    request2.Headers.Add("Content-Type", "application/json");
    request2.Method = "POST";
    request2.Encoding = System.Text.Encoding.GetEncoding("utf-8");
    StringHttpBody request2Body = new StringHttpBody();

    request2Body.ContentType = "application/json";
    request2Body.InsertByteOrderMark = false;
    request2Body.BodyString = @"{<body>}";
    request2.Body = request2Body;


    WebTestResponse res = new WebTestResponse();
    console.WriteLine(res.BodyBytes);

   yield return request2;
   /*This will generate a new string which can be part of your filename when      you run performance tests*/
   String randomNo = DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss").Replace("-", "").Replace(" ", "").Replace(":", "");
   /*This will generate a new file each time your WebRequest runs so you know what the server is returning when you perform webtests*/
   /*You can use some Json parser if your response is Json and capture and validate the response*/
   System.IO.File.WriteAllText(@"C:\Users\XXXX\PerformanceTestRequests\LastResponse" + randomNo+ ".txt", this.LastResponse.BodyString);
   request2 = null;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM