我们有一个ASP.Net网站,它将您重定向到显示会话标识的网址。像这样:在网址中使用会话标识的Webtest
此ID与每个请求是唯一的。
是否有可能使用标准的Visual Studio 2008/2010 webtest测试此站点?我如何提供测试这些数据?
我必须使用相同的ID调用几个不同的页面。
我们有一个ASP.Net网站,它将您重定向到显示会话标识的网址。像这样:在网址中使用会话标识的Webtest
此ID与每个请求是唯一的。
是否有可能使用标准的Visual Studio 2008/2010 webtest测试此站点?我如何提供测试这些数据?
我必须使用相同的ID调用几个不同的页面。
是的,这样做相对容易。但是,您需要创建一个编码的webtest。
在我的例子中,我们有一个登录帖子,它将返回包含会话字符串的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字符串,可以替代它很容易在任何后续网址的请求/ POST 例如
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 ;
另一种方法是编写自己的像这样的(reflectored从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;
}
}
}
}
}