简体   繁体   English

如何将所有 Testcafe 请求记录到同一个 URL?

[英]How can I log all Testcafe requests to the same URL?

I have a URL that I hit multiple times to get different information (myEndpoint.dwr)我有一个 URL,我多次点击它来获取不同的信息 (myEndpoint.dwr)

I am specifically interested in testing the data from the 2nd and 3rd call that i make to myEndpoint .我特别感兴趣的是测试我对myEndpoint进行的第二次和第三次调用的数据。 How do I get the information from those 2 calls?我如何从这两个电话中获取信息?

I define my logger and connect it我定义我的记录器并连接它

const myEndpointUrl = `${base}/dwr/path/myEndpoint.dwr`
const logger = new RequestLogger(
   [myEndpointUrl],
  {
    logResponseHeaders: true,
    logResponseBody: true,
    stringifyResponseBody: true,
    logRequestBody: true,
    stringifyRequestBody: true,
    logRequestHeaders: true,
  }
);

fixture`Page I am testing`.page`${ladingPage}`
  .requestHooks(logger)
  .beforeEach(async (t) => {
    await t.useRole(_authenticatedUser).navigateTo(landingPage);
  });

test('the thing i am testing'), async (t) => {
  const x = logger.count(num => num)
  console.log('x: ', x)
  console.log('x.length: ', x.length)

  await t
    .useRole(_authenticatedUser)
    .expect(...
    etc etc
})

both of my console logs come up with a length of 1.我的两个控制台日志的长度都是 1。

Make sure that requests are executed to the exact same URL. I recommend you use RegExp instead of a simple string .确保请求执行到完全相同的 URL。我建议您使用RegExp而不是简单的string If you are still getting only one request, get all requests and analyze them manually.如果您仍然只收到一个请求,请获取所有请求并手动分析它们。 Also, I see that there are a few mistakes in the example.另外,我看到示例中有一些错误。 count returns Promise<number> and this is why you should use await before it. count返回Promise<number> ,这就是为什么你应该在它之前使用await The result has the number type, so it doesn't have the length property.结果具有number类型,因此它没有length属性。 In my example, I get more than one request:在我的示例中,我收到了不止一个请求:

import { RequestLogger } from "testcafe";

const myEndpointUrl = /devexpress/;
const logger = new RequestLogger(
  [myEndpointUrl],
  {
    logResponseHeaders: true,
    logResponseBody: true,
    stringifyResponseBody: true,
    logRequestBody: true,
    stringifyRequestBody: true,
    logRequestHeaders: true,
  }
);

fixture('My fixture')
  .requestHooks(logger)
  .page('https://devexpress.github.io/testcafe/example/');
test('First test', async t => {
  const x = await logger.count(num => num)
  console.log('x: ', x)
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM