赛普拉斯:如何访问 POST-request 的 body?
Cypress: How to access the body of a POST-request?
赛普拉斯有没有办法检查 POST-request 的 body?
例如:我在表格中输入了一些数据,然后按下 "Submit"。
form-data 通过 POST-request 发送,与 header-data.
之间用空行分隔
我想查看 form-data。如果我输入的所有数据都包含在内并且它们是否正确。
赛普拉斯可以吗?
cy.get('@login').then(function (xhr) {
const body = xhr.requestBody;
console.log(xhr);
expect(xhr.method).to.eq('POST');
});
xhr-object 没有包含传输的数据。
我在谷歌上搜索了同样的问题,并在到达文档之前以某种方式降落在这里。
无论如何,你有没有尝试过类似的东西:
cy.wait('@login').should((xhr) => {
const body = xhr.request.body
expect(body).to.match(/email/)
})
我还没有使用 multipart/form-data
编码请求对其进行测试,但我怀疑您也可以通过这种方式找到请求正文。
祝你好运!
应该可以。
describe('Capturing data sent by the form via POST method', () => {
before(() => {
Cypress.config('baseUrl', 'https://www.reddit.com');
cy.server();
cy.route({
method: 'POST',
url: '/login'
}).as('redditLogin');
});
it('should capture the login and password', () => {
cy.visit('/login');
cy.get('#loginUsername').type('username');
cy.get('#loginPassword').type('password');
cy.get('button[type="submit"]').click();
cy.wait('@redditLogin').then(xhr => {
cy.log(xhr.responseBody);
cy.log(xhr.requestBody);
expect(xhr.method).to.eq('POST');
})
});
});
这是您在 Chrome 开发人员工具中检查数据的方法。
当您在 Cypress 中 运行 进行测试时,您应该会看到与 Chrome 开发人员工具中看到的相同的内容。
赛普拉斯有没有办法检查 POST-request 的 body?
例如:我在表格中输入了一些数据,然后按下 "Submit"。 form-data 通过 POST-request 发送,与 header-data.
之间用空行分隔我想查看 form-data。如果我输入的所有数据都包含在内并且它们是否正确。
赛普拉斯可以吗?
cy.get('@login').then(function (xhr) {
const body = xhr.requestBody;
console.log(xhr);
expect(xhr.method).to.eq('POST');
});
xhr-object 没有包含传输的数据。
我在谷歌上搜索了同样的问题,并在到达文档之前以某种方式降落在这里。
无论如何,你有没有尝试过类似的东西:
cy.wait('@login').should((xhr) => {
const body = xhr.request.body
expect(body).to.match(/email/)
})
我还没有使用 multipart/form-data
编码请求对其进行测试,但我怀疑您也可以通过这种方式找到请求正文。
祝你好运!
应该可以。
describe('Capturing data sent by the form via POST method', () => {
before(() => {
Cypress.config('baseUrl', 'https://www.reddit.com');
cy.server();
cy.route({
method: 'POST',
url: '/login'
}).as('redditLogin');
});
it('should capture the login and password', () => {
cy.visit('/login');
cy.get('#loginUsername').type('username');
cy.get('#loginPassword').type('password');
cy.get('button[type="submit"]').click();
cy.wait('@redditLogin').then(xhr => {
cy.log(xhr.responseBody);
cy.log(xhr.requestBody);
expect(xhr.method).to.eq('POST');
})
});
});
这是您在 Chrome 开发人员工具中检查数据的方法。
当您在 Cypress 中 运行 进行测试时,您应该会看到与 Chrome 开发人员工具中看到的相同的内容。