从 Cypress 中的 JSON 响应中获取 属性
Get property from JSON response in Cypress
有人可以帮助解决断言以下响应:
{"rowId":"899103a2-a9b1-42t3-bc3w-6we638a43fc3","polygons":[{"polygon1":"40eea45f-ffc8-46vb-9ae6-26f5ba5edf4b","polygon2":{"type":"Polygon","geoId":[[[-45.428529,48.321791],[-45.428529,48.321791],[-45.428529,48.321791],[-45.428148,48.321653],[-45.428164,48.32163],[-45.428529,48.321791]]]}}]}
尝试使用以下代码:
Cypress.Commands.add('updateResponse', (request, elementCss) => {
cy.intercept(request, '**/api/rows*').as('update')
cy.xpath(elementCss)
.click()
.wait('@update', { timeout: 20000 })
.then((xhr) => {
cy.log(JSON.stringify(xhr.response.body))
.its('response.statusCode')
.should('eq', 200)
.its('rowId')
.should('not.be.empty')
})
})
其中 request 变量是 POST request.
仅断言响应代码为 200。对于第二个断言,rowID 不为空,出现错误:
"*Timed out retrying after 10000ms: cy.its() errored because the property: rowId does not exist on your subject.*"
我还想断言 polygon2 数组不为空并且 geoId.
中有数据
我做错了什么?提前谢谢你
你可以这样做:
Cypress.Commands.add("updateResponse", (request, elementCss) => {
cy.intercept(request, "**/api/rows*").as("update")
cy.xpath(elementCss)
.click()
.wait("@update", { timeout: 20000 })
.then((xhr) => {
expect(xhr.status).to.eq(200)
expect(xhr.body).to.have.property(
"rowId",
"899103a2-a9b1-42t3-bc3w-6we638a43fc3"
)
expect(xhr.body.rowId).not.to.be.empty
})
})
或者,在您的情况下,您必须提供 response.body.rowId
而不是 rowId
Cypress.Commands.add("updateResponse", (request, elementCss) => {
cy.intercept(request, "**/api/rows*").as("update")
cy.xpath(elementCss)
.click()
.wait("@update", { timeout: 20000 })
.then((response) => {
cy.log(JSON.stringify(response.body))
.its("response.statusCode")
.should("eq", 200)
.its("response.body.rowId")
.should("not.be.empty")
})
})
有人可以帮助解决断言以下响应:
{"rowId":"899103a2-a9b1-42t3-bc3w-6we638a43fc3","polygons":[{"polygon1":"40eea45f-ffc8-46vb-9ae6-26f5ba5edf4b","polygon2":{"type":"Polygon","geoId":[[[-45.428529,48.321791],[-45.428529,48.321791],[-45.428529,48.321791],[-45.428148,48.321653],[-45.428164,48.32163],[-45.428529,48.321791]]]}}]}
尝试使用以下代码:
Cypress.Commands.add('updateResponse', (request, elementCss) => {
cy.intercept(request, '**/api/rows*').as('update')
cy.xpath(elementCss)
.click()
.wait('@update', { timeout: 20000 })
.then((xhr) => {
cy.log(JSON.stringify(xhr.response.body))
.its('response.statusCode')
.should('eq', 200)
.its('rowId')
.should('not.be.empty')
})
})
其中 request 变量是 POST request.
仅断言响应代码为 200。对于第二个断言,rowID 不为空,出现错误:
"*Timed out retrying after 10000ms: cy.its() errored because the property: rowId does not exist on your subject.*"
我还想断言 polygon2 数组不为空并且 geoId.
中有数据我做错了什么?提前谢谢你
你可以这样做:
Cypress.Commands.add("updateResponse", (request, elementCss) => {
cy.intercept(request, "**/api/rows*").as("update")
cy.xpath(elementCss)
.click()
.wait("@update", { timeout: 20000 })
.then((xhr) => {
expect(xhr.status).to.eq(200)
expect(xhr.body).to.have.property(
"rowId",
"899103a2-a9b1-42t3-bc3w-6we638a43fc3"
)
expect(xhr.body.rowId).not.to.be.empty
})
})
或者,在您的情况下,您必须提供 response.body.rowId
rowId
Cypress.Commands.add("updateResponse", (request, elementCss) => {
cy.intercept(request, "**/api/rows*").as("update")
cy.xpath(elementCss)
.click()
.wait("@update", { timeout: 20000 })
.then((response) => {
cy.log(JSON.stringify(response.body))
.its("response.statusCode")
.should("eq", 200)
.its("response.body.rowId")
.should("not.be.empty")
})
})