Home > Software design >  How can I log all Testcafe requests to the same URL?
How can I log all Testcafe requests to the same URL?

Time:01-12

I have a URL that I hit multiple times to get different information (myEndpoint.dwr)

I am specifically interested in testing the data from the 2nd and 3rd call that i make to 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.

CodePudding user response:

Make sure that requests are executed to the exact same URL. I recommend you use RegExp instead of a simple 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. The result has the number type, so it doesn't have the length property. 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)
});
  • Related