javascript-testcafe RequestLogger不拦截api调用

前端之家收集整理的这篇文章主要介绍了javascript-testcafe RequestLogger不拦截api调用 前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

参考文献:

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

Cannot intercept outgoing AJAX request from page using Testcafe

How to log Google Analytics calls in Testcafe?

我在本地运行,并且遇到也在本地运行的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;  
    }
  }
}
原文链接:https://www.f2er.com/js/531152.html

猜你在找的JavaScript相关文章