url 中带有 session-id 的 Webtest

发布于 2024-08-13 08:38:22 字数 475 浏览 2 评论 0原文

我们有一个 ASP.Net 站点,它将您重定向到显示会话 ID 的 URL。像这样:

http://localhost/(S(f3rjcw45q4cqarboeme53lbx))/main.aspx

这个每个请求的 id 都是唯一的。

是否可以使用标准的 Visual Studio 2008/2010 Webtest 来测试该网站?我怎样才能提供测试这些数据?

我必须使用相同的 id 调用几个不同的页面。

We have an ASP.Net site that redirects you to a url that shows a session-id. like this:

http://localhost/(S(f3rjcw45q4cqarboeme53lbx))/main.aspx

This id is unique with every request.

Is it possible to test this site using a standard visual studio 2008/2010 webtest? How can I provide the test this data?

I have to call a couple of different pages using that same id.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

安静 2024-08-20 08:38:22

是的,做到这一点相对容易。然而,您将需要创建一个编码的网络测试。

在我的示例中,我们有一个登录帖子,它将返回包含会话字符串的 url。
就在我们向枚举器发出登录发布请求(request3)之后,我调用以下内容。

WebTestRequest request3 = new WebTestRequest((this.Context["WebServer1"].ToString() + "/ICS/Login/English/Login.aspx"));
//more request setup code removed for clarity
yield return request3;
string responseUrl = Context.LastResponse.ResponseUri.AbsoluteUri;
string cookieUrl = GetUrlCookie(responseUrl, this.Context["WebServer1"].ToString(),"/main.aspx"); 
request3 = null;

其中 GetUrlCookie 是这样的:

public static string GetUrlCookie(string fullUrl, string webServerUrl, string afterUrlPArt)
    {
        string result = fullUrl.Substring(webServerUrl.Length);
        result = result.Substring(0, result.Length - afterUrlPArt.Length);
        return result;
    }

一旦您获得了会话 cookie 字符串,您就可以在请求/发布的任何后续 url 中轻松替换它
例如,

WebTestRequest request4 = new WebTestRequest((this.Context["WebServer1"].ToString() + cookieUrl + "/mySecureForm.aspx"));

我为我的代码如此粗糙表示歉意,但它在我的项目中已被弃用,并且是从代码库的第一个版本中提取的 - 并且说它很简单:)

对于任何负载测试,根据您的应用程序,您可能必须提出一个存储过程来调用,以在每次运行测试时提供不同的登录信息。

请注意,由于无法提前确定响应 url,因此对于登录帖子,您必须暂时关闭 urlValidationEventHandler。为此,我将validationruleeventhandler存储在本地变量中:

        ValidateResponseUrl validationRule1 = new ValidateResponseUrl();
        urlValidationRuleEventHandler = new EventHandler<ValidationEventArgs>(validationRule1.Validate);

这样就可以根据需要打开和关闭它:

this.ValidateResponse -= urlValidationRuleEventHandler ;
this.ValidateResponse += urlValidationRuleEventHandler ;

另一种方法是编写您自己的代码,例如这样(从Visual Studio代码反映并更改为不区分大小写。

class QueryLessCaseInsensitiveValidateResponseUrl : ValidateResponseUrl
{
    public override void Validate(object sender, ValidationEventArgs e)
    {
        Uri uri;
        string uriString = string.IsNullOrEmpty(e.Request.ExpectedResponseUrl) ? e.Request.Url : e.Request.ExpectedResponseUrl;
        if (!Uri.TryCreate(e.Request.Url, UriKind.Absolute, out uri))
        {
            e.Message = "The request URL could not be parsed";
            e.IsValid = false;
        }
        else
        {
            Uri uri2;
            string leftPart = uri.GetLeftPart(UriPartial.Path);
            if (!Uri.TryCreate(uriString, UriKind.Absolute, out uri2))
            {
                e.Message = "The request URL could not be parsed";
                e.IsValid = false;
            }
            else
            {
                uriString = uri2.GetLeftPart(UriPartial.Path);
                ////this removes the query string
                //uriString.Substring(0, uriString.Length - uri2.Query.Length);
                Uri uritemp = new Uri(uriString);
                if (uritemp.Query.Length > 0)
                {
                    string fred = "There is a problem";
                }
                //changed to ignore case
                if (string.Equals(leftPart, uriString, StringComparison.OrdinalIgnoreCase))
                {
                    e.IsValid = true;
                }
                else
                {
                    e.Message = string.Format("The value of the ExpectedResponseUrl property '{0}' does not equal the actual response URL '{1}'. QueryString parameters were ignored.", new object[] { uriString, leftPart });
                    e.IsValid = false;
                }
            }
        }
    }
}

Yes, it is relatively easy to do this. You will need to create a coded webtest however.

In my example we have a login post that will return the url including the session string.
Just after the we yield the login post request (request3) to the enumerator I call the following.

WebTestRequest request3 = new WebTestRequest((this.Context["WebServer1"].ToString() + "/ICS/Login/English/Login.aspx"));
//more request setup code removed for clarity
yield return request3;
string responseUrl = Context.LastResponse.ResponseUri.AbsoluteUri;
string cookieUrl = GetUrlCookie(responseUrl, this.Context["WebServer1"].ToString(),"/main.aspx"); 
request3 = null;

Where GetUrlCookie is something like this:

public static string GetUrlCookie(string fullUrl, string webServerUrl, string afterUrlPArt)
    {
        string result = fullUrl.Substring(webServerUrl.Length);
        result = result.Substring(0, result.Length - afterUrlPArt.Length);
        return result;
    }

Once you have the session cookie string, you can substitute it really easy in any subsequent urls for request/post
e.g.

WebTestRequest request4 = new WebTestRequest((this.Context["WebServer1"].ToString() + cookieUrl + "/mySecureForm.aspx"));

I apologise for my code being so rough, but it was deprecated in my project and is pulled from the first version of the codebase - and for saying it was easy :)

For any load testing, depending on your application, you may have to come up with a stored procedure to call to provide distinct login information each time the test is run.

Note, because the response url cannot be determined ahead of time, for the login post you will have to temporarily turn off the urlValidationEventHandler. To do this I store the validationruleeventhandler in a local variable:

        ValidateResponseUrl validationRule1 = new ValidateResponseUrl();
        urlValidationRuleEventHandler = new EventHandler<ValidationEventArgs>(validationRule1.Validate);

So can then turn it on and off as I require:

this.ValidateResponse -= urlValidationRuleEventHandler ;
this.ValidateResponse += urlValidationRuleEventHandler ;

The alternative is to code your own such as this (reflectored from the Visual Studio code and changed to be case insensitive.

class QueryLessCaseInsensitiveValidateResponseUrl : ValidateResponseUrl
{
    public override void Validate(object sender, ValidationEventArgs e)
    {
        Uri uri;
        string uriString = string.IsNullOrEmpty(e.Request.ExpectedResponseUrl) ? e.Request.Url : e.Request.ExpectedResponseUrl;
        if (!Uri.TryCreate(e.Request.Url, UriKind.Absolute, out uri))
        {
            e.Message = "The request URL could not be parsed";
            e.IsValid = false;
        }
        else
        {
            Uri uri2;
            string leftPart = uri.GetLeftPart(UriPartial.Path);
            if (!Uri.TryCreate(uriString, UriKind.Absolute, out uri2))
            {
                e.Message = "The request URL could not be parsed";
                e.IsValid = false;
            }
            else
            {
                uriString = uri2.GetLeftPart(UriPartial.Path);
                ////this removes the query string
                //uriString.Substring(0, uriString.Length - uri2.Query.Length);
                Uri uritemp = new Uri(uriString);
                if (uritemp.Query.Length > 0)
                {
                    string fred = "There is a problem";
                }
                //changed to ignore case
                if (string.Equals(leftPart, uriString, StringComparison.OrdinalIgnoreCase))
                {
                    e.IsValid = true;
                }
                else
                {
                    e.Message = string.Format("The value of the ExpectedResponseUrl property '{0}' does not equal the actual response URL '{1}'. QueryString parameters were ignored.", new object[] { uriString, leftPart });
                    e.IsValid = false;
                }
            }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文