作用

发起一个 HTTP 请求

语法格式

  1. cy.request(url)
  2. cy.request(url, body)
  3. cy.request(method, url)
  4. cy.request(method, url, body)
  5. cy.request(options)

参数说明

url

请求 URL

cy.request() 在 cy.visit() 后面

  1. // 先访问某个 url
  2. cy.visit('http://localhost:8080/app')
  3. // 请求 url 是 http://localhost:8080/users/1.json
  4. cy.request('users/1.json')

设置了 baseUrl,且 cy.request() 在 cy.visit() 前面

cypress.json

  1. // cypress.json
  2. {
  3. "baseUrl": "http://localhost:1234"
  4. }


测试代码

  1. // url 是 http://localhost:1234/seed/admin
  2. cy.request('seed/admin')

备注

如果 cypress 无法确定 host,它将抛出错误

body

  • 请求正文,不同接口内容,body 会有不同的形式
  • Cypress 设置了 Accepts 请求头,并通过 encoding 选项序列化响应体

method

请求方法,没啥好说的,默认是 GET

options

Cypress系列(68)- request() 命令详解 - 图1

GET 请求的栗子

  1. context('get请求', function () {
  2. it('默认访问方式', function () {
  3. cy.request('http://www.helloqa.com')
  4. });
  5. it('使用 options', function () {
  6. cy.request({
  7. method: 'get',
  8. url: 'http://www.helloqa.com'
  9. })
  10. });
  11. // .request() 常常和别名 .as() 一起使用,用来进行接口返回值的断言
  12. it('真实测试', function () {
  13. cy.request({
  14. method: 'get',
  15. url: 'https://www.helloqa.com'
  16. }).as('comments')
  17. cy.get('@comments')
  18. .then((response) => {
  19. expect(response.status).to.be.eq(200)
  20. })
  21. });
  22. })

测试结果

Cypress系列(68)- request() 命令详解 - 图2

.request() 返回值

Cypress系列(68)- request() 命令详解 - 图3
包含以下属性

  • status
  • body
  • headers
  • duration

.request() 别名后通过 .get() 的返回值

Cypress系列(68)- request() 命令详解 - 图4
包含以下属性

  • status
  • body
  • headers
  • duration
  • statusText
  • allRequestResponses
  • requestHeaders
  • redirects
  • isOkStatusCode

使用 .request() 代替 .visit() 的栗子

官方有那么一句话

有时候,cy.request() 测试页面的内容要比 cy.visit() 更快,然后等待整个页面加载所有资源

通过 .visit() 测试需要登录才能访问的页面

  1. const username = 'jane.lane'
  2. const password = 'password123'
  3. it('使用 visit', function () {
  4. // 相当于 UI 界面操作
  5. cy.visit('')
  6.   // 登录操作
  7. cy.get("input[name=username]").type(username)
  8. cy.get("input[name=password]").type(password)
  9. cy.get("form").submit()
  10. // 会跳转至需要登录才能访问的页面
  11. cy.get("h1").should("contain", "jane.lane") });

测试结果

Cypress系列(68)- request() 命令详解 - 图5

通过 .request() 测试需要登录才能访问的页面

  1. it('request代替visit', function () {
  2. // 通过接口层面去访问页面
  3. // 请求页面
  4. cy.request('/login')
  5. .its('body')
  6. .should('include', '<p>In this recipe we:</p>')
  7. // 登录请求
  8. cy.request({
  9. method: 'post',
  10. url: '/login',
  11. // 表单格式的请求
  12. form: true,
  13. body: {
  14. username: 'jane.lane',
  15. password: 'password123'
  16. }
  17. })
  18. // 访问需要登录之后才能访问的页面
  19. cy.request('/dashboard')
  20. .its('body')
  21. .should('include', 'jane.lane')
  22. });

测试结果

Cypress系列(68)- request() 命令详解 - 图6

官方重点

通常,一旦对登录进行了适当的e2e测试,就没有理由继续使用 cy.visit() 登录并等待整个页面加载所有关联的资源,然后再运行其他命令,这样做可能会减慢我们整个测试套件的速度

轮询发出请求的栗子

背景

  • 当轮询服务器以获取可能需要一段时间才能完成的响应时,此功能很有用
  • 如何做:创建一个递归函数

测试代码

  1. function req() {
  2. cy
  3. .request('/')
  4. .then((resp) => {
  5. if (resp.status === 200)
  6. // 请求成功则退出轮询
  7. return
  8. // 递归
  9. req()
  10. })
  11. }
  12. context('轮询request', function () {
  13. it('默认访问方式', function () {
  14. cy.visit('http://localhost:7077/')
  15. // 轮询前的操作
  16. cy.get("form").click()
  17. // 轮询请求
  18. .then(() => {
  19. req()
  20. })
  21. });
  22. })

关于 .request() 的注意事项

Debugging

  • 通过 .request() 发出的请求不会出现在开发者工具(F12)网络一栏中
  • Cypress 实际上并未从浏览器发出XHR请求
  • 实际上是从 Cypress Test Runner(在Node中)发出HTTP请求
  • 因此,不会在开发人员工具中看到该请求

Cookie

  • 通过 .request() 发出的请求,Cypress 会自动发送和接收 Cookie
  • 在发送 HTTP 请求之前,如果请求来自浏览器,Cypress 会自动附加本应附加的 Cookie
  • 此外,如果响应具有 Set-Cookie 标头,则这些标头将自动在浏览器 Cookie 上重新设置
  • 换句话说,cy.request() 透明地执行所有基础功能,就好像它来自浏览器一样

https://www.cnblogs.com/poloyy/p/13841964.html