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

测试码

  1. // have tried several urls,from exact to generic to ports.
  2. const logger = RequestLogger("/api/",{
  3. logRequestHeaders: true,logRequestBody: true
  4. });
  5. const url = 'localhost:3000/reports';
  6. fixture `Report`
  7. .page(url)
  8. .requestHooks(logger)
  9. .beforeEach(async (t: TestController) => {
  10. await loginAndNavToReports({ t });
  11. });
  12. test("Reports",async (t: TestController) => {
  13. // this fires an api call through the /api/ path
  14. await t.click(".test-reportLaborSummary");
  15. // have tried several comparisons here,all fail or expect falsey to be truthy errors
  16. await t.expect(logger.count(() => true)).ok();
  17. }
最佳答案
我怀疑TestCafe的运行速度比调用api的代码快.

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

要检查记录器是否已接到电话,我建议采用以下方式:

  1. await wait_for_first_request();
  2. const receivedCalls = logger.requests.length;
  3. if (receivedCalls === 0) {
  4. throw new Error('api has not been called')
  5. }
  6. async function wait_for_first_request() {
  7. for (let i = 0; i < 50; i++) {
  8. await t.wait(100);
  9. if (logger.requests.length > 0 ) {
  10. return;
  11. }
  12. }
  13. }

猜你在找的JavaScript相关文章