Cypress-无法获取响应标头-API自动化

Cypress-无法获取响应标头-API自动化,api,cypress,Api,Cypress,我有一个使用Cypress的API自动化测试套件,其中一个测试中我面临的问题是验证响应头 由于某些原因,我无法使用Cypress读取响应头 代码如下 cy.request({ method:'GET', url:Cypress.env("Authorisationurl")+tokenId+'&decision=allow&acr_values=1', followRedirect: false, headers:{ 'Accept

我有一个使用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块

选项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?因为打印的顺序不是我在代码中写的。非常感谢您在这方面花费的时间。

请像这样使用: JSON.parse(JSON.stringify(response.headers))[“X-Frame-Options”]