我有一个使用Cypress的API自动化测试套件,我在其中一个测试中面临的问题之一是验证响应头。
由于某些原因,我无法使用Cypress读取响应头。
代码如下
cy.request({
method:'GET',
url:Cypress.env("Authorisationurl")+tokenId+'&decision=allow&acr_values=1',
followRedirect: false,
headers:{
'Accept': "/*"
}
}).then((response) => {
const rbody = (response.body);
cy.log(response.status)
//THIS GOT ASSERTED TO TRUE
expect(response.status).to.equal(302)
//OPTION1
cy.wrap(response.headers['X-Frame-Options']).then(() => {
return response.headers['X-Frame-Options'];
});
//OPTION2
return response.headers['X-Frame-Options']
//OPTION3
return response.headers
})上面的选项都没有给出标题信息。事实上,我也对执行的顺序感到困惑。
这是我的输出。

以获取以下代码。
const rbody = (response.body);
cy.log(response.status)
cy.log(response)
expect(response.status).to.equal(302)
cy.log(response.headers)
cy.log(response.headers['X-Frame-Options'])
return response.headers['X-Frame-Options']此外,不太确定对象{9}表示什么。有谁能解释一下这里发生了什么。我知道Cypress的执行流程,代码是在then block中作为回调函数编写的。
选项3非常可怕,因为它会给出一个错误
cy.then() failed because you are mixing up async and sync code.
In your callback function you invoked 1 or more cy commands but then returned a synchronous value.
Cypress commands are asynchronous and it doesn't make sense to queue cy commands and yet return a synchronous value.
You likely forgot to properly chain the cy commands using another cy.then().
The value you synchronously returned was: Object{9}有没有人能帮我一下,正确的方法是什么?我知道Cypress是非常快速和易于使用的,但是为了远离Selenium,我们需要让有意义的错误消息的开发人员更容易编码。对象{9}帮助不大。另外,我需要使用Cy.log吗?因为打印的顺序不是我在代码中写的。非常感谢您在这方面抽出时间。
发布于 2021-05-26 16:33:50
请这样使用:JSON.parse(JSON.stringify(response.headers))"X-Frame-Options";
https://stackoverflow.com/questions/67298284
复制相似问题