【问题标题】:Cannot find a dependent request in webtest在 webtest 中找不到依赖请求
【发布时间】:2018-02-23 16:09:04
【问题描述】:
我在 VS2015 中使用我的 webtest 记录器记录了一个测试。当我重新运行测试时,它在某个 .css 文件的依赖 GET 请求中失败。 webtest 结果中的 url 显示类似 https://mycompany/blah/Style%20Guides/Global_CSS.css 错误是一个简单的 404 错误。
现在我转到主请求并搜索这个特定的依赖请求,以便我可以将其 Parse_Dependent_Request 选项设置为 False 或将 Extected_Http_Status_Code 设置为 404,这对我来说很好。但我无法在主请求或任何其他请求下找到这个特定的依赖请求。
我试图将所有依赖请求的所有 Parse_Dependent_Request 选项更改为 false 并了解哪个实际发送了 Get 请求,但它们都没有工作。我从 webtest 生成代码并确实进行了页面搜索但是徒劳无功。请问如何获取请求?
【问题讨论】:
标签:
c#
visual-studio-2015
httprequest
webtest
【解决方案1】:
许多相关请求 (DR) 在 Web 测试中并不明确。当主请求的parse dependent requests 是true 时,Visual Studio 会处理该主请求的 HTML 响应以查找 DR,并将它们添加到 DR 列表中。也可以解析任何 HTML 格式的 DR 响应,并将其 DR 添加到列表中。
处理缺失或有问题的 DR 的一种技术是运行一个插件来修改 DR 列表。下面的代码基于Codeplex 提供的“Visual Studio 性能测试快速参考指南”(3.6 版)第 189 页上的WebTestDependentFilter。 Codeplex 文档包含许多其他关于 Web 和负载测试的有用信息。
public class WebTestDependentFilter : WebTestPlugin
{
public string FilterDependentRequestsThatStartWith { get; set; }
public string FilterDependentRequestsThatEndWith { get; set; }
public override void PostRequest(object sender, PostRequestEventArgs e)
{
WebTestRequestCollection depsToRemove = new WebTestRequestCollection();
// Note, you can't modify the collection inside a foreach, hence the second collection
// requests to remove.
foreach (WebTestRequest r in e.Request.DependentRequests)
{
if (!string.IsNullOrEmpty(FilterDependentRequestsThatStartWith))
{
if (r.Url.StartsWith(FilterDependentRequestsThatStartWith))
{
depsToRemove.Add(r);
}
}
else if (!string.IsNullOrEmpty(FilterDependentRequestsThatEndWith))
{
if (r.Url.EndsWith(FilterDependentRequestsThatEndWith))
{
depsToRemove.Add(r);
}
}
}
foreach (WebTestRequest r in depsToRemove)
{
e.WebTest.AddCommentToResult(string.Format("Removing dependent: {0}", r.Url));
e.Request.DependentRequests.Remove(r);
}
}
}
上面代码中的搜索条件可以很容易地修改为(例如)检查 URL 的中间部分。
另一种变体是将某些 DR 的预期响应代码设置为其他值。这可能会比删除失败的 DR 进行更准确的性能测试,因为仍然需要服务器来处理请求并返回响应。