Cypress 发送api请求

2020-06-10  本文已影响0人  BestFei

一、post 请求

/// <reference types="Cypress" />

describe('My First Test Suite', function() {

    it('Test post api', function() {
        cy.request({
            method: 'POST',
            url: "http://www.bestfei.com/login",
            body: {
                "email": "best@fei.com",
                "password": "Zaq1Xsw2"
            }
        }).then((response)=>{
            cy.log("response.status")
            cy.log(response.status)
            cy.log("response.duration")
            cy.log(response.duration)
            cy.log("response.body")
            cy.log(response.body)
            cy.setCookie('token', response.body.refrushToken);
        })

    })

})

二、get 请求

    it('Test get api2', function() {
        cy.request({
            method: 'Get',
            url: "https://jsonplaceholder.cypress.io/users?_limit=1"
        }).then((response)=>{
            cy.log("response.status")
            cy.log(response.status)
            cy.log("response.duration")
            cy.log(response.duration)
            cy.log("response.body")
            cy.log(JSON.stringify(response.body))
        })
    })

接口返回

[
  {
    "id": 1,
    "name": "Leanne Graham",
    "username": "Bret",
    "email": "Sincere@april.biz",
    "address": {
      "street": "Kulas Light",
      "suite": "Apt. 556",
      "city": "Gwenborough",
      "zipcode": "92998-3874",
      "geo": {
        "lat": "-37.3159",
        "lng": "81.1496"
      }
    },
    "phone": "1-770-736-8031 x56442",
    "website": "hildegard.org",
    "company": {
      "name": "Romaguera-Crona",
      "catchPhrase": "Multi-layered client-server neural-net",
      "bs": "harness real-time e-markets"
    }
  }
]

三、使用.as() 别名保存响应数据,以便稍后在共享测试上下文中使用
注意:使用时,要加 this ,例如:this.post

    it("save the response data shared test context", function()
    {
        cy.request('https://jsonplaceholder.cypress.io/users?_limit=1')
            .its('body.0') // yields the first element of the returned list
            .as('user') // saves the object in the test context
            .then(function () {
                cy.log("api1 response body:")
                cy.log(JSON.stringify(this.user))
                cy.log("user.id:"+this.user.id)
                cy.request('POST', 'https://jsonplaceholder.cypress.io/posts', {
                    userId: this.user.id,
                    title: 'Cypress Test Runner',
                    body: 'Fast, easy and reliable testing for anything that runs in a browser.',
                })
                    .its('body').as('post') // save the new post from the response
            })
            .then(function () {
                cy.log("api2 response body:")
                cy.log(JSON.stringify(this.post))
                expect(this.post, 'post has the right user id')
                    .property('userId').to.equal(this.user.id)
            })

    })

扩充文档
https://www.cnblogs.com/yoyoketang/p/13051549.html
`

上一篇下一篇

猜你喜欢

热点阅读