首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Cypress -无法获取响应头- API自动化

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

Stack Overflow用户
提问于 2021-04-28 18:16:49
回答 1查看 87关注 0票数 0

我有一个使用Cypress的API自动化测试套件,我在其中一个测试中面临的问题之一是验证响应头。

由于某些原因,我无法使用Cypress读取响应头。

代码如下

代码语言:javascript
复制
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
})

上面的选项都没有给出标题信息。事实上,我也对执行的顺序感到困惑。

这是我的输出。

以获取以下代码。

代码语言:javascript
复制
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非常可怕,因为它会给出一个错误

代码语言:javascript
复制
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吗?因为打印的顺序不是我在代码中写的。非常感谢您在这方面抽出时间。

EN

回答 1

Stack Overflow用户

发布于 2021-05-26 16:33:50

请这样使用:JSON.parse(JSON.stringify(response.headers))"X-Frame-Options";

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67298284

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档