微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Cypress 拦截 API JSON 响应并提取 URL

如何解决Cypress 拦截 API JSON 响应并提取 URL

我的网络应用程序发送 API POST 请求以创建应用程序并返回 JSON 响应。我想从该响应中访问一个特定的 JSON 对象。 我的 JSON 是这样开始的

[
    {
        "status_code": 201,"body": {
            "created": "2021-01-28T00:00:00Z","modified": "2021-01-28T00:00:00Z","id": "a2d86d17-9b3c-4c4d-ac49-5b9d8f6d6f8f","applicant": {
                "id": "07f1e1d3-0521-401b-813e-3f777f2673c6","status": "Pending","first_name": "","last_name": "","URL": "some onboarding url"

我想在 JSON 响应中获取该 URL,稍后在我的 cypress 自动化脚本中访问它。 请注意,JSON 响应以方括号而不是大括号开头,这意味着,我假设整个响应是一个对象?

我的柏树脚本看起来像这样

        cy.contains('button','CONTINUE').click() 
        cy.EmailGen('candidate').then(email => {
            cy.get('#emails\\[0\\]').type(`${email}`)
            cy.wrap(email).as('candidateEmail')
        })
        //writing intercept here,before the Send application button,which triggers the POST req.
        cy.intercept('/hr/v1/applications/invite').as('getURL')
        cy.get('button').contains('SEND APPLICATION').click({ force: true })
        //waiting on the alias of intercept and using interception to print objects from response
        cy.wait('@getURL').then((interception)=> {
            cy.log(interception.response.body.applicant.URL)
        }) 
        
        cy.logout()

脚本执行没有错误。只是在 cy.log 语句中没有记录任何内容。下面是屏幕。

execution screen shot

我也尝试使用下面给出的另一种方法

cy.intercept('/hr/v1/applications/invite',(req) => {
            req.reply((res=> {
                expect(res.status_code).to.equal('201')
            expect(res.body.applicant.status).to.equal('Pending')    
            }))
            
        })

在这种情况下,我收到一个嵌入在请求和响应中的断言错误以及一些我无法理解的其他内容。 完整的错误是这样的...... "预计以下错误源自您的测试代码,而不是来自 Cypress。\n\n > 传递给 req.reply() 的响应回调在拦截响应时引发错误:\n\n预期未定义为等于 '201'\n \nRoute: {\n "matchUrlAgainstPath": true,\n "url": "/hr/v1/applications/invite"\n}\n\n拦截的请求:{} 拦截的响应:{} 当 Cypress 检测到未捕获的错误来源时从您的测试代码中,它将自动使当前测试失败。包含 window.zE 不是一个函数"

读起来有点奇怪..

enter image description here

我的应用程序有时会抛出这个异常,我使用以下代码处理过。

cy.on('uncaught:exception',(err,runnable) => {
            expect(err.message).to.include('window.zE is not a function')
            done()
            return false
        })

我真的希望我已经在这里解释了一切。请帮助一个菜鸟。 问候

解决方法

正如 Richard Matsen 在评论中所建议的那样, 我使用了 console.log(interception.response) 并检查了 Cypress 控制的浏览器中的控制台输出。 我注意到响应 json 结构与我在使用 Web 应用程序时在开发人员工具的网络选项卡中得到的不同。 响应如下...

{headers: {…},url: "https://example.com/hr/v1/applications/invite/batch/",method: null,httpVersion: "1.1",statusCode: 200,…}
   body: Array(1)
     0:
       body:
          applicant: {id: "c6b2d686-d4f3-483e-abc8-e4641c365845",status: "Pending",first_name: "",last_name: "",email: "qa2+candidate879@example.com",…}
applicant_status: "NONE"
applicant_status_label: "None"
created: "2021-01-29T00:00:00Z"
get_applicant_status_display: "None"
id: "ad2939f5-c8ab-490a-a9e1-b0474de69e2c"
URL: "some url"

这让我将json遍历修改为 interception.response.body[0].body.applicant.URL

如果其他人有一个巧妙的方法来处理这个问题,请告诉我!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。