我们有一个 ASP.Net 站点,可将您重定向到显示会话 ID 的 url。像这样:
这个 id 对于每个请求都是唯一的。
是否可以使用标准的 Visual Studio 2008/2010 webtest 测试该站点?我怎样才能提供测试这些数据?
我必须使用相同的 ID 调用几个不同的页面。
我们有一个 ASP.Net 站点,可将您重定向到显示会话 ID 的 url。像这样:
这个 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 字符串后,您可以在任何后续 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;
}
}
}
}
}