8 接口测试

    在服务和服务、系统和系统之间进行通信时,常常会使用到接口。通过接口测试,可以在项目早期更快发现问题。接口有很多类型,而现阶段使用的接口是基于HTTP协议的接口。

8.1 Cypress支持的HTTP请求方式

    在Cypress中发起HTTP请求时,需要使用到的命令为cy.request(),其基本语法格式如下所示:

cy.request(url)
cy.request(url, body)
cy.request(method, url)
cy.request(method, url, body)
cy.request(options)

    主要参数详细信息如下所示:

  • url

    url(String),发起请求的接口地址。需要注意的事项如下所示:

    1、如果cy.request()在cy.visit()后发起请求时,则Cypress将默认使用cy.visit()中的域名做为发起接口请求的域名地址,示例如下所示:

cy.visit('https://www.surpassme.com/app')
cy.request('users/add') //  实际访问的URL: https://www.surpassme.com/users/add

    2、如果事先在cypress.json设置了baseUrl时,则在发送接口请求时,可以不填写域名,Cypress在实际发起请求时,会自动将baseUrl添加到接口地址前面。示例如下所示:

// cypress.json
{
  "baseUrl": "https://www.surpassme.com/
}
cy.request('user/add') // 实际访问的URL: https://www.surpassme.com/users/add

    3、如果Cypress没有检测到域名,则抛错误异常

  • body

    body (String, Object)是发起请求的请求体。根据接口类型,body会有不同的形式。

  • method

    method (String) 是发起请求的方法。默认请求方法为GET,其支持的方法比较多,最常见的有GETPOSTPUTDELETE

  • **options **

    options (Object)是可选项,可以定义一些其他的参数来改变cy.request的一些行为,主要哪下所示:

  • 输出内容

    在通过cy.request()发送请求后,输出的响应内容主要有statusbodyheadersduration

8.2 示例

8.2.1 发起GET请求

    GET是平常使用最多的请求,我们来看看示例,如下所示:

/// <reference types="cypress" />

describe('发送GET请求示例', () => {
    let url="http://httpbin.org/get"

    it('发送请求的GET示例用例-1', () => {
        cy.request(url).as("response");
        cy.get("@response").should((response)=>{
            expect(response.body).to.have.property("headers")
            expect(response.body.url).to.eq(url)
            expect(response.body.headers.Host).to.eq("httpbin.org")
        });
    });

    it('发送请求的GET示例用例-2', () => {
        cy.request("GET",url).as("response");
        cy.get("@response").should((response)=>{
            expect(response.body).to.have.property("headers")
            expect(response.body.url).to.eq(url)
            expect(response.body.headers.Host).to.eq("httpbin.org")
        });
    });

    it('发送请求的GET示例用例-3', () => {
        cy.request("GET",url,{"name":"Surpass","age":28}).as("response");
        cy.get("@response").should((response)=>{
            expect(response.body).to.have.property("headers")
            expect(response.body.url).to.contain(url)
            expect(response.body.headers.Host).to.eq("httpbin.org")
        });
    });


    it('发送请求的GET示例用例-4', () => {
        cy.request({
            method:"GET",
            url:url,
            qs:{"name":"Surpass","age":28}
        }).then((response)=>{
           expect(response.body.args.name).to.eq("Surpass")
           expect(response.body.args.age).to.eq("28")
           expect(response.status).to.eq(200)
           expect(response.body.headers.Host).to.eq("httpbin.org")
           expect(response.body).to.have.property("headers")
        });
    });

    it('获取图片示例', () => {
        cy.request({
            method:"GET",
            url:"https://www.cnblogs.com/images/logo.svg",
            encoding:"base64"
        }).then((response) => {
            let base64Content=response.body;
            let mime=response.headers["content-type"];
            let imageDataUrl=`data:${mime};base64,${base64Content}`
        })
    });

    it('下载文件', () => {
      cy.request({
          method:"GET",
          url:"https://www.cnblogs.com/images/logo.svg",
          encoding:"binary"
      }).then((response)=>{
          cy.writeFile("./cnblog.logo.svg",response.body,"binary");
      })
    });
});

    运行结果如下所示:

WEB自动化测试(8)—— Cypress 接口测试-LMLPHP

8.2.1 发起POST请求

    示例如下所示:

/// <reference types="cypress" />

describe('发送POST请求示例', () => {
    let url="http://httpbin.org/post";
    let body={"name":"Surpass","age":28};

    it('发送请求的POST示例用例-1', () => {
        cy.request("POST",url,body).as("response");
        cy.get("@response").should((response)=>{
             expect(response.body.json.name).to.eq("Surpass")
             expect(response.body.headers.Host).to.eq("httpbin.org")
        })
    });

    it('发送请求的POST示例用例-2', () => {
        cy.request({
           method:"POST",
           url:url,
           body:body,
           form:true
        }).then((response)=>{
            expect(response.body.form.name).to.eq("Surpass")
            expect(response.body.headers.Host).to.eq("httpbin.org")
        });
    });

    it('发送请求的POST示例用例-3', () => {
        cy.request({
           method:"POST",
           url:url,
           body:body,
           form:false,
           headers:{"Content-Type":"application/json","Customer-Header":"Surpass"}
        }).then((response)=>{
            expect(response.body.json.name).to.contain("Surpass")
            expect(response.body.headers.Host).to.eq("httpbin.org")
            expect(response.headers["content-type"]).to.eq("application/json")
            expect(response.body.headers["Customer-Header"]).to.eq("Surpass")
        });
    });
});

    运行结果如下所示:

WEB自动化测试(8)—— Cypress 接口测试-LMLPHP


最后感谢每一个认真阅读我文章的人,看着粉丝一路的上涨和关注,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走

WEB自动化测试(8)—— Cypress 接口测试-LMLPHP

这些资料,对于做【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴我走过了最艰难的路程,希望也能帮助到你!凡事要趁早,特别是技术行业,一定要提升技术功底。希望对大家有所帮助…….

如果你不想再体验一次自学时找不到资料,没人解答问题,坚持几天便放弃的感受的话,可以加入下方我的qq群大家一起讨论交流,里面也有各种软件测试资料和技术交流。

WEB自动化测试(8)—— Cypress 接口测试-LMLPHP

09-29 16:06