若是想从头学起Cypress,能够看下面的系列文章哦html
https://www.cnblogs.com/poloyy/category/1768839.html前端
管理控制整个网络请求web
可在开发者工具(network 一栏)看到请求的 type 是 xhr,或者直接点击 xhr 进行筛选npm
一样是 login 请求,有些是 xhr,有些倒是 document,对于 type=document 的请求, .route() 默认是不会拦截到的浏览器
使用 Fetch API 的请求以及其余类型的网络请求(例如页面加载和 <script> 标记)将不会在命令日志中被拦截或看到服务器
实验性 route2() 命令,该命令支持使用 Fetch API 的请求以及其余类型的网络请求,例如页面加载;该命令将在后面wenz展开讲解网络
cy.route(url)
cy.route(url, response)
cy.route(method, url)
cy.route(method, url, response)
cy.route(callbackFn)
cy.route(options)
须要监听的 URL,遵循 minimatch 模式函数
为匹配上的 URL 提供自定义响应体工具
待匹配监听 URL 的请求方法post
回调函数
能够经过 *、** 来匹配动态的路由,我们直接看栗子就行了
cy.server() cy.route('**/users/*/comments') // https://localhost:7777/users/123/comments <-- 匹配 // https://localhost:7777/users/123/comments/465 <-- 不匹配
cy.server() cy.route('**/posts/**') // https://localhost:7777/posts/1 <-- 匹配 // https://localhost:7777/posts/foo/bar/baz <-- 匹配 // https://localhost:7777/posts/quuz?a=b&1=2 <-- 匹配 // https://localhost:7777/posts <-- 不匹配
cy.route('**/users/*') // 下面的都匹配 /users/1 http://localhost:2020/users/2 https://google.com/users/3 // 下面的都不匹配 /users/4/foo http://localhost:2020/users/5/foo
注:演示项目是 cypress 提供的,如何下载可看 Cypress 系列文章的一开始几篇都有写
cd C:\Users\user\Desktop\py\cypress-example-recipes\examples\logging-in__xhr-web-forms
npm start
http://localhost:7079/
const username = 'jane.lane' const password = 'password123' before(function () { cy.visit('http://localhost:7079/') }) it('正常登陆,修改登陆请求的status、response', function () { cy.server() cy.route({ url: '**/login', method: 'POST', status: 503, delay: 1000, response: { success: false, data: 'Not success' }, }).as("login") cy.get("input[name=username]").type(username) cy.get("input[name=password]").type(`${password}{enter}`) cy.wait('@login').then((res) => { cy.log(res) expect(res.status).to.eq(503) expect(res.responseBody.data).to.eq('Not success') }) });
能够看到成功匹配一个请求
若是要对响应体作断言,能够从这对象里面拿到对应的值
Cypress 经过 cy.route().as() 和 cy.wait() ,能够自动等到接口返回之后再执行后续操做,加强了测试用例的健壮性
// 简单的代码结构(仅演示) // 启动 Mock 服务器 cy.server({ // 添加 options... }) // 添加多个 route 路由 cy.route({ // 添加 options... }).as("route1") cy.route({ // 添加 options... }).as("route2") .... // UI 界面的操做... // 某些操做发出请求 // 等待请求的完成 cy.wait('route1').then((res)=>{ // 对接口的响应作后续操做或断言 expect(res.status).to.eq(200) })
指定了 status 参数以后,也必须指定 response 参数
不匹配路由的请求,强制返回 404 状态和空 response
cy.server({ force404: true }) cy.route({ url: '**/logins', method: 'POST', status: 503, delay: 1000, response: { success: false, data: 'Not success' }, }).as("login") // 伪代码 // 发出 /login 请求的操做
当 /login 没有匹配到任意路由的时候,会返回 404
能够看到没有请求匹配成功此路由
it('cy.route() - route responses to matching requests', () => { // https://on.cypress.io/route // 访问 cy.visit('https://example.cypress.io/commands/network-requests') // 预置变量 let message = 'whoa, this comment does not exist' // 启动 Mock 服务器 cy.server() // 路由1:监听 url 是 comments/* 且 请求方法是 GET 的请求 cy.route('GET', 'comments/*').as('getComment') // 点击按钮触发请求 cy.get('.network-btn').click() // 等待请求响应成功后获取 status 进行断言 cy.wait('@getComment').its('status').should('eq', 200) // 路由2:监听 url 是 /commets 且 请求方法是 POST 的请求 cy.route('POST', '/comments').as('postComment') // 点击按钮触发请求 cy.get('.network-post').click() // 等待请求响应成功后进行断言 cy.wait('@postComment').should((xhr) => { expect(xhr.requestBody).to.include('email') expect(xhr.requestHeaders).to.have.property('Content-Type') expect(xhr.responseBody).to.have.property('name', 'Using POST in cy.route()') }) /* 路由3:监听 url 是 comments/* 且 请求方法是 POST 的请求 自定义 status、response、delay 并返回给监听到的请求 */ cy.route({ method: 'PUT', url: 'comments/*', status: 503, response: {error: message}, delay: 500, }).as('putComment') // // 等待请求响应成功后进行断言 cy.get('.network-put').click() cy.wait('@putComment') // 出现 404 以后断言文案 cy.get('.network-put-comment').should('contain', message) })
Cypress 会在命令日志中显示 XHR 是发送给服务器仍是 stub
在命令日志中显示(XHR STUB)的XHR就是发送到 stub的,而且它们的 response,status,headers,delay 已由匹配的 cy.route() 控制