【发布时间】:2020-07-22 08:07:24
【问题描述】:
我有 cypress 请求调用返回 Json 数组
{ids:[one, two, three]}
如何从响应正文中解析出其中一个数组值并将其传递给下一个测试?
【问题讨论】:
我有 cypress 请求调用返回 Json 数组
{ids:[one, two, three]}
如何从响应正文中解析出其中一个数组值并将其传递给下一个测试?
【问题讨论】:
考虑到给定的信息,我猜你想要的是Asserting Network Calls from Cypress Tests。
cypress-example-recipes中有一个很好的例子
我猜你可以做类似的事情
describe('Test', () => {
let ids;
it('gets expected response', () => {
cy.server()
cy.route('GET', '/url').as('response')
cy.visit("url")
// log the request object
cy.get('@response').then(console.log)
// confirm the request status
cy.get('@response').should('have.property', 'status', 200)
// confirm the request's response
cy.get('@response').its('response').then((res) => {
expect(res.body).to.deep.equal({
"ids": ["one", "two", "three"]
})
// store the ids in a variable
ids = JSON.parse(res.body)?.ids
})
})
})
【讨论】:
{ids:[one, two, three]} - 那不是有效的JSON。请参考jsonlint.com 或进一步详细说明您真正想做的事情。