无法使用Testcafe拦截来自页面的传出AJAX请求 [英] Cannot intercept outgoing AJAX request from page using Testcafe
问题描述
我正在尝试将TestCafe用于客户端javascript库,并且我无法通过计数机制捕获测试页面加载时的任何传出AJAX请求。
I am attempting to use TestCafe for a client-side javascript library, and I am unable to capture any outgoing AJAX requests for on the load of a testing page through the counting mechanism.
我的尝试涉及使用 RequestLogger
对象涉及使用正则表达式 /\.org /
,捕获使用.org后缀的任何传出磁贴请求。我相信这是有道理的,因为所有传出的请求都转到openstreetmap.org,目的是抓住png地图图块。
My attempts involve using the RequestLogger
object from this library involve setting up the first parameter using the regex /\.org/
, to capture any outgoing tile request which uses the .org suffix. I believe this makes sense, as all the outgoing requests go to openstreetmap.org with the aim of grabbing png map tiles.
我的测试核心如下所示:
The core of my test looks like the following:
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请求传出计数,我是否缺少某些内容?
Is there something I am missing in order to capture the proper outgoing count of AJAX requests?
如果有帮助,我已经制作了一个包含这个问题的回购,设置方式让人们可以尝试解决而无需配置:
If it helps, I've made a repo which contains this problem, set up in a way that people can attempt to solve without configuration:
推荐答案
感谢您的详细说明。
您需要附加 记录器
到您的测试
/ 夹具
。您还可以使用 t.addRequestHooks
和 t.removeRequestHooks
方法在测试运行期间附加和分离挂钩。
You need to attach the logger
to your test
/fixture
. You can also attach and detach hooks during test run using the t.addRequestHooks
and t.removeRequestHooks
methods.
在测试代码中,我将 logger
挂钩附加到测试
:
In the test code, I attached the logger
hook to the 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");
});
这篇关于无法使用Testcafe拦截来自页面的传出AJAX请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!