我正在尝试将 TestCafe 用于客户端 javascript 库,但无法通过计数机制捕获测试页面加载的任何传出 AJAX 请求。
我的尝试包括使用RequestLogger https://devexpress.github.io/testcafe/documentation/test-api/intercepting-http-requests/logging-http-requests.html该库中的对象涉及使用正则表达式设置第一个参数/\.org/
,捕获任何使用 .org 后缀的传出磁贴请求。我相信这是有道理的,因为所有传出请求都会发送到 openstreetmap.org,目的是获取 png 地图图块。
我的测试的核心如下所示:
import { RequestLogger } from "testcafe";
fixture`Hello World - Leaflet`.page`http://localhost:8080`;
const logger = RequestLogger(/org/, {
logRequestHeaders: true,
logResponseHeaders: true
});
test("Test if there's an outgoing network request...", async t => {
// Do something...
await t
.wait(5000)
.expect(logger.count(() => true))
.gt(0, "Must detect more than zero outgoing requests to openstreetmap");
});
为了捕获 AJAX 请求的正确传出计数,我是否缺少某些内容?
如果有帮助,我创建了一个包含此问题的存储库,以人们可以尝试在不进行配置的情况下解决的方式进行设置:
感谢您的详细描述。
你需要attach https://devexpress.github.io/testcafe/documentation/test-api/intercepting-http-requests/attaching-hooks-to-tests-and-fixtures.html the logger
给你的test
/fixture
。您还可以在测试运行期间使用以下命令附加和分离钩子t.addRequestHooks
and t.removeRequestHooks
方法。
在测试代码中,我附上了logger
钩到test
:
import { RequestLogger } from "testcafe";
fixture`Hello World - Leaflet`.page`http://localhost:8080`;
const logger = RequestLogger(/org/);
test
.requestHooks(logger)
("Test if there's an outgoing network request...", async t => {
await t
.wait(5000)
.expect(logger.count(() => true))
.gt(0, "Must detect more than zero outgoing requests to openstreetmap");
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)