从Cypress中的JSON响应中获取属性

编程入门 行业动态 更新时间:2024-10-06 08:27:29
本文介绍了从Cypress中的JSON响应中获取属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

是否有人可以帮助解决以下问题:

{"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') }) })

其中请求变量为POST请求。

仅断言响应代码为200。对于第二个断言,rowID不为空,我收到错误:

"*Timed out retrying after 10000ms: cy.its() errored because the property: rowId does not exist on your subject.*" 我还希望断言,多边形2数组不为空,且在大地水准面内具有数据。

我做错了什么?预先感谢您

推荐答案

您可以这样做:

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

更多推荐

从Cypress中的JSON响应中获取属性

本文发布于:2023-11-26 23:11:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1635589.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:属性   Cypress   JSON

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!