2

I have been exploring the projects on GitHub which demonstrates the capabilities of integrating TestCafe with CucumberJS, however, I am not able to find any reference to test the RestAPI through this integration and I need it as per my project requirements.

Can I request someone to post a working example, currently I am exploring the following repo: https://github.com/rquellh/testcafe-cucumber

I have already tried to explore the projects available on GitHub:

https://github.com/search?p=3&q=testcafe+cucumber&type=Repositories

I am expecting the Cucumber BDD driven workflow based on TestCafe:

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

Alex Skorkin
  • 4,264
  • 3
  • 25
  • 47
curiousTester
  • 121
  • 1
  • 12

1 Answers1

3

There are two options here.
 
If you want to test RestAPI itself by sending requests from a test scenario and checking a response, you don't need TestCafe for this. According to this issue TestCafe still cannot make HTTP requests. Furthermore, Request Handlers cannot handle requests you make from your test code using external tools. They work only for requests that a testing page makes.
 
If a script on your testing page makes RestAPI requests and you want to know what is in response, you can just add a request logger like this:

let requestLogger = null;
 
Given('I use example.com REST API', async function () {
  requestLogger = RequestLogger(/https:\/\/api\.example\.com/, {
    logResponseHeaders: true,
    logResponseBody: true
  });

  await testController.addRequestHooks(requestLogger);
});

And then you can obtain data from requestLogger as described in the Test API documentation.

Alexey Lindberg
  • 756
  • 3
  • 12
  • Thanks @Alexey Lindberg, this question should rather be handled by devexpress to introduce api test capabilities to the tool. Accepting the work around that you have proposed. I am using axios and fetch as of now to test the api. – curiousTester Aug 02 '19 at 01:17
  • can you please look into the code and help: '`const apiBaseUrl = 'https://api.test.samplewebsite.com.au/'; const apiLogger = RequestLogger(apiBaseUrl, { logResponseHeaders: true, logResponseBody: true }); await testController.addRequestHooks(apiLogger); await testController.navigateTo('https://baseurl.sitecore-scaled-test-cm.azurewebsites.net/').expect(apiLogger.contains(r => r.response.statusCode === 200)).ok();`' I am not able to figure out why I am getting the error: 'errMsg: 'AssertionError: expected false to be truthy' }' – curiousTester Aug 15 '19 at 04:34
  • According to the [Select Requests to be Handled by the Hook](https://devexpress.github.io/testcafe/documentation/test-api/intercepting-http-requests/select-requests-to-be-handled-by-the-hook.html#filter-by-url) topic, you are filtering requests by an exact url: 'https://api.test.samplewebsite.com.au/'. If it is only a part of the request url, try to use the Regular Expression instead. For example: `const urlRegExp = new RegExp('https://api.test.samplewebsite.com.au'); const apiLogger = RequestLogger(urlRegExp, { logResponseHeaders: true, logResponseBody: true });`. – Dmitry Ostashev Aug 15 '19 at 15:27