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,其支持的方法比較多,最常見的有GET、POST、PUT、DELETE
- **options **
? ? options (Object)是可選項,可以定義一些其他的引數來改變cy.request的一些行為,主要哪下所示:
| 選項 | 默認值 | 功能描述 |
|---|---|---|
| log | true | 是否在Command log中顯示命令 |
| url | null | 發起請求的URL地址 |
| method | GET | 請求方法 |
| auth | null | 添加鑒權頭資訊 |
| body | null | 請求體 |
| failOnStatusCode | true | 若回傳的狀態碼不是2xx和3xx系列,則認為請求失敗 |
| followRedirect | true | 是否自動重定向 |
| form | false | 是否以表單形式發送請求體,如果是的話,則設定urlencode為x-www-form-urlencoded |
| encoding | utf8 | 請求回應的編碼方式,支持ascii, base64, binary, hex, latin1, utf8, utf-8, ucs2, ucs-2, utf16le, utf-16le等 |
| gzip | true | 是否接受gzip編碼 |
| headers | null | 添加額外的請求頭 |
| qs | null | 查詢引數,如果填寫后,則自動追加到URL地址后面 |
| retryOnStatusCodeFailure | false | 在通過狀態碼判定為失敗后的重試次數,如果設定為true,則重試4次 |
| retryOnNetworkFailure | true | 在通過網路問題判定后為失敗后的重試次數,如果設定true,則重試4次 |
| timeout | responseTimeout | 決議域名地址的超時時間 |
- 輸出內容
? ? 在通過cy.request()發送請求后,輸出的回應內容主要有status、body、headers、duration,
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");
})
});
});
? ? 運行結果如下所示:

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")
});
});
});
? ? 運行結果如下所示:

原文地址:https://www.jianshu.com/p/007976277dc8
本文同步在微信訂閱號上發布,如各位小伙伴們喜歡我的文章,也可以關注我的微信訂閱號:woaitest,或掃描下面的二維碼添加關注:

作者: Surpassme
來源: http://www.jianshu.com/u/28161b7c9995/
http://www.cnblogs.com/surpassme/
宣告:本文著作權歸作者所有,歡迎轉載,但未經作者同意必須保留此段宣告,且在文章頁面明顯位置給出 原文鏈接 ,否則保留追究法律責任的權利,如有問題,可發送郵件 聯系,讓我們尊重原創者著作權,共同營造良好的IT朋友圈,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/509188.html
標籤:其他
上一篇:使用Dolby Encoding Engine及一些三方工具實作在非macOS上制作Dolby Vision 8.4視頻
