Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testcafe: How to test POST parameters of request

The page I am testing makes a POST ajax request when clicking a button. I would like to test, if the parameters that are being sent in this request are correct. How would I go about that?

This is, what I tried:

import {RequestLogger, Selector} from '../../../node_modules/testcafe';

const requestUrl = 'http://localhost:8080/mypage/posttarget';
const logger = RequestLogger({url: requestUrl, method: 'post'}, {logRequestBody: true, logRequestHeaders: true});


fixture `Notifications`.page('http://localhost:8080/mypage')
  .requestHooks(logger);

test('notification request contains id', async t => {
  await t
    .click('#submit-notification')
    .expect(logger.request.body.id)
    .eql(1)
  ;
});

But logger.request is undefined. Also logger.requests.length is 0.

I would appreciate it, if someone could show me how I can check the request body?

like image 435
mwiegboldt Avatar asked Oct 18 '18 12:10

mwiegboldt


People also ask

Can TestCafe do API testing?

TestCafe includes a comprehensive set of server-side API testing tools. You can add dedicated API tests to your test suite, or include API testing methods in existing functional tests.

How do I mock data in TestCafe?

Use the RequestMock constructor to create a mocker. Call the onRequestTo and respond methods in a chain to specify mock responses for every handled request. To use the mocker during tests, attach it to a test or fixture. No WebDriver required.

How do I send API request in TestCafe?

To make an HTTP-request from your test code, you can use the standard https nodejs module. Here's an example: import https from 'https'; const executeRequest = () => { return new Promise(resolve => { const options = { hostname: ' https://api.com/move/sample', port: 443, path: '/', method: 'POST' }; const req = https.

How do I run TestCafe?

The easiest way to run TestCafe tests is to use the command line interface. Specify the target browser and test file path. TestCafe opens the selected browser and starts test execution.


1 Answers

The RequestLogger object has the requests property, which is an array of logged requests, but not a single request.   I've tried to reproduce the issue with an empty requests property but it works as expected. Please check the following test code:

import { RequestLogger } from 'testcafe';

const requestUrl = 'https://demos.devexpress.com/aspxgridviewdemos/gridediting/EditForm.aspx';
const logger = RequestLogger({ url: requestUrl, method: 'post' }, { logRequestBody: true, logRequestHeaders: true });


fixture `Notifications`.page('https://demos.devexpress.com/aspxgridviewdemos/gridediting/EditForm.aspx')
    .requestHooks(logger);

test('notification request contains id', async t => {
    await t.click('#ContentHolder_grid_DXEFL_DXCBtn9');
    await t.expect(logger.requests[0].request.body).ok();
});

UPD. I've found a difference between:

await t.click('#ContentHolder_grid_DXEFL_DXCBtn9');
await t.expect(logger.requests[0].request.body).ok();

and

await t
    .click('#ContentHolder_grid_DXEFL_DXCBtn9')
    .expect(logger.requests[0].request.body).ok();

  The second case is not working because we need to wait until the request is fully processed, and so we need to add an await before assertion

like image 112
Alex Kamaev Avatar answered Oct 02 '22 13:10

Alex Kamaev