kiwigrid/gherkin-testcafe

Need help with requestHook

Closed this issue · 4 comments

I am trying to implement a requestHook for RequestLogger and I am not able to wrap my head around it.

I am trying to get what the login Request is sending back, aka the response. This is what I got and it dosn't work. I appreciate any help I can get :)

const { RequestLogger } = require('testCafe');
const logger = RequestLogger({ loggerURL, method: 'post' }, {
  logResponseHeaders: true,
  logResponseBody: true
});

Then(/I should be able to press the login button/, async t => {
  await t
    .expect(submitButton.hasAttribute('disabled'))
    .notOk()
    .click(submitButton)
    .addRequestHooks(...logger)
  await t.expect(logger.contains(record => record.response.statusCode === 200)).ok();
  const logRecord = logger.requests[0];
  console.log('---- Logger');
  console.log(logger);
  console.log('---- logRecord');
  console.log(logRecord);
  console.log(logRecord.userAgent);           
  console.log(logRecord.request.url);         
  console.log(logRecord.request.method);      
  console.log(logRecord.response.statusCode); 
});

I think you attach the hook too late.

You should attach it before the request you are trying to log is done (i.e. before clicking the submit button).

Apart from that, you initalize the logger incorrectly. The option is called url, not loggerURL. Please refer to the documentation.

(Plus you do not need to spread the logger to addRequestHooks. Just call it as .addRequestHooks(logger).)

Thank you Lukas-Kullmann

When I dont spread the logger .addRequestHooks(logger), I am getting:
1) Hook is expected to be a RequestHook subclass, but it was object.

If I run with the spread .addRequestHooks(...logger):
TypeError: Found non-callable @@iterator

Any ideas?

Hi dthisner,

I have no idea. It seems to me like you are using it in the same way the documentation says.

In any case, this is not a gherkin-testcafe issue but an issue with the usage of testcafe itself. Please open an issue at their repo if you need assistance with this.

I will close this issue for now.
If you find any issue regarding gherkin-testcafe in the future, feel free to open a new issue.

My tech lead came up with this:


Then(/I should be able to press the login button/, async t => {
  const httpLogger = new AddRequestLogger({
    requestFilter: event =>
      event.requestOptions.url ===
      '<url>',
    responseFilter: event => event.statusCode > 200,
  });

  await t
    .addRequestHooks(httpLogger)
    .expect(submitButton.hasAttribute('disabled')).eql(false)
    .hover(submitButton)
    .click(submitButton)
    .wait(1000);
});

and it works like a charm :)

Thanks @Lukas-Kullmann for providing fast answers and great feedback :)