从控制台应用程序调用生成的Web测试代码

4

我有一个关于从Visual Studio Web测试中提取代码的一般性问题。我使用了Web测试的“生成代码”选项,并得到了以下内容:

using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.WebTesting;
using Microsoft.VisualStudio.TestTools.WebTesting.Rules;

namespace SignsOfLife
{
    public class SignsOfLifeCoded : WebTest {

        public SignsOfLifeCoded() {
            this.PreAuthenticate = true;
        }

        public override IEnumerator<WebTestRequest> GetRequestEnumerator() {
            // Initialize validation rules that apply to all requests in the WebTest
            if ((this.Context.ValidationLevel >= Microsoft.VisualStudio.TestTools.WebTesting.ValidationLevel.Low)) {
                ValidateResponseUrl validationRule1 = new ValidateResponseUrl();
                this.ValidateResponse += new EventHandler<ValidationEventArgs>(validationRule1.Validate);
            }

            WebTestRequest request1 = new WebTestRequest("http://localhost/site/client/default.aspx");
            yield return request1;
            request1 = null;

            WebTestRequest request2 = new WebTestRequest("http://localhost/site/login.aspx");
            request2.ThinkTime = 5;
            request2.QueryStringParameters.Add("c", "clientcode", false, false);
            ExtractHiddenFields extractionRule1 = new ExtractHiddenFields();
            extractionRule1.Required = true;
            extractionRule1.HtmlDecode = true;
            extractionRule1.ContextParameterName = "1";
            request2.ExtractValues += new EventHandler<ExtractionEventArgs>(extractionRule1.Extract);
            yield return request2;
            request2 = null;

            WebTestRequest request3 = new WebTestRequest("http://localhost/site/login.aspx");
            request3.Method = "POST";
            request3.ExpectedResponseUrl = "http://localhost/site/user/home.aspx";
            request3.QueryStringParameters.Add("c", "clientcode", false, false);
            FormPostHttpBody request3Body = new FormPostHttpBody();
            request3Body.FormPostParameters.Add("__LASTFOCUS", this.Context["$HIDDEN1.__LASTFOCUS"].ToString());
            request3Body.FormPostParameters.Add("__EVENTTARGET", this.Context["$HIDDEN1.__EVENTTARGET"].ToString());
            request3Body.FormPostParameters.Add("__EVENTARGUMENT", this.Context["$HIDDEN1.__EVENTARGUMENT"].ToString());
            request3Body.FormPostParameters.Add("__VIEWSTATE", this.Context["$HIDDEN1.__VIEWSTATE"].ToString());
            request3Body.FormPostParameters.Add("__EVENTVALIDATION", this.Context["$HIDDEN1.__EVENTVALIDATION"].ToString());
            request3Body.FormPostParameters.Add("Login1$UserName", "username");
            request3Body.FormPostParameters.Add("Login1$Password", "password");
            request3Body.FormPostParameters.Add("Login1$LoginButton", "Log In");
            request3.Body = request3Body;
            yield return request3;
            request3 = null;
        }
    }
}

我想要做的是将这个测试放到一个单独的服务中,以便进行全天健康检查。我想确保用户能够在一整天内登录。我应该使用什么流程将测试转换成类似控制台应用程序的形式?我在调试webtest代码时遇到了问题。例如,我应该如何正确调用GetRequestEnumerator方法并从代码中对其进行响应?

1个回答

0

第一建议:
假设:您想每天运行它,并使其可重用/可扩展以进行新的测试

将测试代码放入 Web 服务中,并适当链接。 然后创建一个 Windows 服务,根据任何时间间隔每天消耗该服务,并以某种方式存储结果。

如果我的假设不正确,则这是一项不必要的工作,因为以后不会被重用。

第二个建议:
假设:您的测试仅在一天内执行

只需编写一个控制台应用程序以在给定间隔执行测试 编写控制台应用程序以运行计时器类并注册回调事件,以便在经过时间间隔后触发。 在您定义的回调方法中执行所需的测试并记录结果。

有一个线程问题可以用多种方式处理... 您希望您的应用程序整天运行,因此您只需使用类似于此的东西即可

public class tester
{
   public tester()
   {
       //setup timer logic here
       //..
       //..

       var ts = new ThreadStart(run);
       var thread = new Thread(ts);
       thread.start();
   } 

   public void run()
   {
       while (ShouldContinue);
       {
           //do nothing execute keep app going
       }
   }
}

当您满足退出条件时,请切换ShouldContinue ... 我会假设在这种情况下,条件是应用程序运行了24小时以上

第二个解决方案设计和制造起来确实不需要太长时间。但第一个解决方案将需要更多时间。

希望这回答了您的问题或激发了另一个想法 =D


这并没有回答问题。例如:“我应该如何正确调用GetRequestEnumerator方法并从代码中对其进行响应?”完全没有得到回答。 - Jari Turkia

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接