无法从 cypress 中的响应正文获取对象值

问题描述 投票:0回答:1

我是 Cypress 的初学者,我正在尝试亲身体验 Cypress 的 API 测试。 我正在尝试来自以下 URL 的非常基本的

GET
响应: https://automationexercise.com/api/productsList

我试图获得

responseCode
,但每当我尝试反对时,我都会得到空白值。 我的逻辑如下:

it("Verify All Product response contains correct keys and values", () => {

    cy.request("GET","https://automationexercise.com/api/productsList").then((response) => {
        expect(response.status).to.eq(200)
        expect(response.body).length.to.be.greaterThan(1)
        cy.log(response.body.responseCode)
  });
});

我已经编辑了我的结果截图: 让我知道我在哪里犯了错误。 谢谢。

javascript cypress web-api-testing
1个回答
0
投票

请勿使用

cy.log()
进行调试,请使用
console.log()

如果您打开浏览器(不在 Cypress 中),请打开开发工具,转到网络并打开录制。

然后将您的 URL 放入导航框中 - 开发工具会向您显示网络请求,您可以在 Response 子选项卡中检查响应。

从中,您可以看到路径中没有

body
,因此不要将其添加到测试中。

© www.soinside.com 2019 - 2024. All rights reserved.