testcafe RequestLogger 没有拦截 api 调用

testcafe RequestLogger not intercepting api calls

出于某种原因,我无法让 testcafe 的 RequestLogger 记录我正在进行的任何 API 调用。我已经阅读了几乎所有关于 RequestLogger 的文章和问题,所有内容都指向与下面代码相同的内容。不确定我做错了什么,任何帮助都会很棒。

参考文献:

https://devexpress.github.io/testcafe/documentation/test-api/intercepting-http-requests/logging-http-requests.html

我在本地 运行 并在本地点击 API 即 运行 ,前端端口 3000 后端端口 8080,API 是在:8080/api/admin。我可以看到记录器被注入到测试中,但没有更新它,它只是一个带有初始道具的乏味对象,在 t.expect 语句后会出错。

我想知道 beforeEach 是否破坏了某些东西,但我需要它来触发任何 API 调用,因为用户需要进行身份验证。我可以看到 API 请求在调试时被调用,我正在尝试拦截,但没有运气

testcafe 版本:1.0.0 || 0.23.3

测试代码

// have tried several urls, from exact to generic to ports.
const logger = RequestLogger("/api/", {
  logRequestHeaders: true,
  logRequestBody: true
});

const url = 'localhost:3000/reports';

fixture `Report`
  .page(url)
  .requestHooks(logger)
  .beforeEach(async (t: TestController) => {
    await loginAndNavToReports({ t });
  });

test("Reports", async (t: TestController) => {
  // this fires an api call through the /api/ path
  await t.click(".test-reportLaborSummary");
  // have tried several comparisons here, all fail or expect falsey to be truthy errors
  await t.expect(logger.count(() => true)).ok();
}

我怀疑 TestCafe 比调用 api 的代码运行得更快。

在使用记录器对象之前,您应该等待它至少收到一个调用。

检查记录器是否接到电话,我建议这样做:

await wait_for_first_request();
const receivedCalls = logger.requests.length;
if (receivedCalls === 0) {
  throw new Error('api has not been called')
}


async function wait_for_first_request() {
  for (let i = 0; i < 50; i++) {
    await t.wait(100);
    if (logger.requests.length > 0 ) {
      return;  
    }
  }
}