繁体   English   中英

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 对象具有requests 属性,它是一个已记录请求的数组,但不是单个请求。 我试图用空的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();

第二种情况不起作用,因为我们需要等到请求被完全处理,所以我们需要在断言之前添加一个await

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM