【问题标题】:testcafe RequestLogger not intercepting api callstestcafe RequestLogger 不拦截 api 调用
【发布时间】:2019-07-07 16:52:02
【问题描述】:

由于某种原因,我无法让 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();
}

【问题讨论】:

    标签: javascript typescript automated-tests e2e-testing testcafe


    【解决方案1】:

    我怀疑 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;  
        }
      }
    }
    

    【讨论】:

    • 我也尝试过等待方法,但不完全是那种方式。我在调用之后和记录器之前直接放置了一个调试器,以便尝试手动拦截它。我会试一试,谢谢你的回答。
    • 原来这不一定是解决方案,但引导我朝着正确的方向前进。我正在运行上面建议的检查,但为了让 RequestLogger 工作,我必须修改我的记录器对象以使用 url 的正则表达式
    猜你喜欢
    • 2023-04-02
    • 1970-01-01
    • 2015-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多