Testcafe:如何测试请求的POST参数

Testcafe: How to test POST parameters of request

我正在测试的页面在单击按钮时发出 POST ajax 请求。我想测试此请求中发送的参数是否正确。我该怎么做?

这是我试过的:

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)
  ;
});

但 logger.request 未定义。另外 logger.requests.length 是 0.

如果有人能告诉我如何检查请求正文,我将不胜感激?

RequestLogger object has the requests property,这是一组记录的请求,但不是单个请求。 我试图用空 requests 属性 重现该问题,但它按预期工作。请检查以下测试代码:

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();
});

更新。 我发现了以下两者之间的区别:

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

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

第二种情况不工作,因为我们需要等到请求完全处理,所以我们需要在 assertion

之前添加一个 await