testcafe RequestLogger没有拦截api调用 [英] testcafe RequestLogger not intercepting api calls

查看:51
本文介绍了testcafe RequestLogger没有拦截api调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

For some reason, I cannot get testcafe's RequestLogger to log any of the API calls that I am making. I've read nearly all of the articles and questions on the RequestLogger and everything is pointing to what appears to be the same as the below code as working. Not sure what I'm doing wrong, any help would be great.

参考:

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

无法使用Testcafe拦截页面中的传出AJAX请求

如何在Testcafe中记录Google Analytics(分析)调用?

我在本地运行,并且遇到也在本地运行的API,前端在端口3000上,后端在端口8080上,API位于:8080/api/admin.我可以看到记录器已注入测试中,但没有任何更新,它只是一个带有初始道具的平淡对象,在t.expect语句后会出错.

I am running locally and hitting an API that is running locally as well, front-end on port 3000 and backend on port 8080, API is at: 8080/api/admin. I can see the logger as injected into the test but nothing updates it, its just a bland object with initial props and will error out after the t.expect statement.

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

I wonder if the beforeEach is breaking something but I need it in order to fire any API calls because the user needs to be authenticated. I can see the API request being called when debugging, that I am trying to intercept, but no luck

testcafe版本:1.0.0 ||0.23.3

testcafe version: 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的代码快.

I suspect that TestCafe runs faster than the code that calls the api.

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

Before using the logger object you should wait that it has received at least one call.

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

To check if the logger has received a call, I suggest to do it this way:

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;  
    }
  }
}

这篇关于testcafe RequestLogger没有拦截api调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆