若是想从头学起Cypress,能够看下面的系列文章哦html
https://www.cnblogs.com/poloyy/category/1768839.htmlgit
使用该命令在网络层管理 HTTP 请求的行为github
cy.route() 命令详解:http://www.javashuo.com/article/p-bcbrptkn-nx.html正则表达式
cy.intercept(url, routeHandler?) cy.intercept(method, url, routeHandler?) cy.intercept(routeMatcher, routeHandler?)
要匹配的请求 URL ,能够是字符串也能够是正则表达式npm
cy.intercept('http://example.com/widgets')
cy.intercept('http://example.com/widgets', { fixture: 'widgets.json' })
没有指定请求方法的话,能够匹配任意类型的请求方法json
请求方法数组
cy.intercept('POST', 'http://example.com/widgets', { statusCode: 200, body: 'it worked!' })
{ /** * 与 HTTP Basic身份验证中使用的用户名和密码匹配 */ auth?: { username: string | RegExp, password: string | RegExp } /** * 与请求上的 HTTP Headers 匹配 */ headers?: { [name: string]: string | RegExp } /** * 与请求上的 hostname 匹配 */ hostname?: string | RegExp /** * If 'true', 只有 https 的请求会被匹配 * If 'false', 只有 http 的请求会被匹配 */ https?: boolean /** * 与请求上的 method 请求方法匹配 * 默认 '*', 匹配所有类型的 method */ method?: string | RegExp /** * 主机名后的路径, 包括了 ? 后面的查询参数 * www.baidu.com/s?wd=2 */ path?: string | RegExp /** * 和 path 同样, 不过无论 ? 后面的查询参数 * www.baidu.com/s */ pathname?: string | RegExp /** * 与指定的端口匹配, 或者传递多个端口组成的数组, 其中一个匹配上就好了 */ port?: number | number[] /** * 与请求路径 ? 后面跟的查询参数匹配上 * wd=2 */ query?: { [key: string]: string | RegExp } /** * 完整的请求 url * http://www.baidu.com/s?wd=2 */ url?: string | RegExp }
{ /** * 将 fixture 文件做为响应主体, 以 cypress/fixtures 为根目录 */ fixture?: string /** * 将字符串或 JSON 对象做为响应主体 */ body?: string | object | object[] /** * 响应 headers * @default {} */ headers?: { [key: string]: string } /** * 响应状态码 * @default 200 */ statusCode?: number /** * 若是 true, Cypress 将破坏网络链接, 而且不发送任何响应 * 主要用于模拟没法访问的服务器 * 请勿与其余选项结合使用 */ forceNetworkError?: boolean /** * 发送响应前要延迟的毫秒数 */ delayMs?: number /** * 以多少 kbps 发送响应体 */ throttleKbps?: number }
Cypress 官方项目的下载地址:https://github.com/cypress-io/cypress-example-kitchensinkpromise
npm start
注: route() 将来将会被弃用浏览器
登陆请求匹配上了路由服务器
最重要的固然是 request 和 response 两个属性
断言请求体和响应状态码
// 断言匹配此路由的请求接收到包含【username】的请求 body cy.wait('@login3').its('request.body').should('have.property', 'username') // 断言匹配此路由的请求接收到 HTTP 状态码为 500 cy.wait('@login3').its('response.statusCode').should('eq', 200) // 断言匹配此路由的请求接收到包含【redirect】的请求 body cy.wait('@login3').its('response.body').should('have.property', 'redirect')
不过这样的话只能每次写一条不能同时三条都写,因此仍是建议像代码图同样,先 .then() 再进行断言
会从cypress安装目录/fixtures 下读取对应的数据文件,它会变成响应 body 的数据
自定义了响应body、statusCode,还有返回响应的延时时间
延时生效了
body 和 statusCode 变成自定义的数据了
beforeEach(() => { cy.visit('http://localhost:7079/login') })
能够看到回调函数只有一个参数,就是 request 参数
回调函数内不能包含 cy.**() 的命令,若是包含会报错
简单来讲就是
cy.type() 命令执行完后会返回一个 promise 对象,同时又会调用回调函数,而回调函数内又调用了 cy.get() 返回了一个 promise 对象,Cypress 会将这种状况当作测试失败处理
意思就是一个请求能够同时匹配上多个路由
一个登陆请求匹配成功了两个路由,且回调函数会按匹配的顺序执行
回调函数的参数就是一个请求对象,它其实能够调用如下方法
{ /** * 销毁该请求并返回网络错误的响应 */ destroy(): void /** * 控制请求的响应 * 若是传入的是一个函数, 则它是回调函数, 当响应时会调用 * 若是传入的是一个 StaticResponse 对象, 将不会发出请求, 而是直接将这个对象当作响应返回 */ reply(interceptor?: StaticResponse | HttpResponseInterceptor): void /** * 使用 response body(必填) 和 response header(可选) 响应请求 */ reply(body: string | object, headers?: { [key: string]: string }): void /** * 使用 HTTP 状态码(必填)、 response body(可选)、response header(可选) 响应请求 */ reply(status: number, body?: string | object, headers?: { [key: string]: string }): void /** * 重定向到新的 location 来响应请求, * @param statusCode 用来重定向的 HTTP 状态代码, Default: 302 */ redirect(location: string, statusCode?: number): void }
可使用 req.reply() 函数来动态控制对请求的响应
cy.intercept('/login', (req) => { // functions on 'req' can be used to dynamically respond to a request here // 将请求发送到目标服务器 req.reply() // 将这个 JSON 对象响应请求 req.reply({plan: 'starter'}) // 将请求发送到目标服务器, 而且拦截服务器返回的实际响应, 而后进行后续操做(相似抓包工具对响应打断点) req.reply((res) => { // res 就是实际的响应对象 }) })
一个是 request 对象,一个是 response 对象
cy.intercept('/notification', (req) => { req.reply((resp) => { // Success 将做为 response body 返回到浏览器 resp.send('Success') // 将 success.json 里面的数据做为 response body 返回到浏览器 resp.send({fixture: 'success.json'}) // 将响应延迟 1000ms resp.delay(1000) // 将响应限制为 64kbps resp.throttle(64) }) })
{ /** * 能够自定义 response statusCode、response body、response header * 也能够直接传 StaticResponse 对象 */ send(status: number, body?: string | number | object, headers?: { [key: string]: string }): void send(body: string | object, headers?: { [key: string]: string }): void send(staticResponse: StaticResponse): void /** * 继续返回响应 */ send(): void /** * 等待 delayMs 毫秒,而后再将响应发送给客户端 */ delay: (delayMs: number) => IncomingHttpResponse /** * 以多少 kbps 的速度发送响应 */ throttle: (throttleKbps: number) => IncomingHttpResponse }