官方文档:https://docs.cypress.io 目前只支持英文,好处是官方入门视频不少,对于英文水平很差的也很容易入手;前端
优势:chrome
缺点:npm
为何要用cypress请看:https://segmentfault.com/a/11... 看1,2,3点就能够;json
废话很少说看了以上几点决定要不要入坑应该内心有数了;segmentfault
网上绝大部分说采用npm i cypress -d 痛点在于内置了谷歌内核太大,每一个项目都要安装太麻烦了,不便于管理,此处咱们采用全局安装npm i -g cypress,既节约了磁盘空间又节约时间浏览器
既然已经全局安装cypress的环境变量会配置到npm的环境变量中,npm的环境变量天然就在系统变量里面,cmd中输入cypress open
全局状况下打开就是慢点dom
直接拖入项目,会在项目中生成cypress文件夹和cypress.jsonide
在cypress.json中咱们能够配置测试环境:工具
{ "viewportWidth": 1440, "viewportHeight": 900, "chromeWebSecurity": false, "fixturesFolder": "cypress/fixtures", "integrationFolder": "cypress/integration", "pluginsFile": "cypress/plugins", "screenshotsFolder": "cypress/screenshots", "videosFolder": "cypress/videos", "supportFile": "cypress/support", "requestTimeout": 10000, "defaultCommandTimeout": 10000, "baseUrl": "http://192.168.1.6:9000" }
在cypress文件中:有四个文件夹,fixtures(存放测试文件), intergration(测试用例), plugins(插件), support(扩展);测试
常规测试用例能够参照intergration文件下的examples文件
describe('测试', () => { it('test', () => { cy.visit('https://news.ycombinator.com/login?goto=news') cy.get('input[name="acct"]').eq(0).type('test') cy.get('input[name="pw"]').eq(0).type('123456') cy.get('input[value="login"]').click() cy.contains('Bad login') }) })
改进按照模块进行测试用例
import {login} from './login' import {issue} from './issue' describe('test', function () { it('loginIn', login) it('issue', issue) })
在login.js中
const login = () => { cy.visit('https://news.ycombinator.com/login?goto=news') cy.get('input[name="acct"]').eq(0).type('test') cy.get('input[name="pw"]').eq(0).type('123456') cy.get('input[value="login"]').click() cy.contains('Bad login') } export {login}
cy.get(':nth-child(1) > .td > .t-input__text.edit-pointer').click({force: true}); cy.get('.el-input__inner').eq(4).type('测试内容', {force: true});
在fixtures中放入须要准备上传的文件,例如:2345.jpg
在support文件夹下的commands.js中写入扩展
Cypress.Commands.add('upload_file', (fileName, fileType = ' ', selector) => { return cy.get(selector).then(subject => { cy.fixture(fileName, 'base64') .then(Cypress.Blob.base64StringToBlob) .then(blob => { const el = subject[0]; const testFile = new File([blob], fileName, { type: fileType }); const dataTransfer = new DataTransfer(); dataTransfer.items.add(testFile); el.files = dataTransfer.files; }); }); });
而后在用例中调用:
const fileName = '2345.jpg'; //上传文件名 const fileType = 'video/jpg'; //mime类型 const fileInput = 'input[type=file]'; //上传file的input框 cy.upload_file(fileName, fileType, fileInput); cy.wait(2000);
这样就能够愉快的提交了!
后续有遇到痛点还会继续和你们分享的!
欢迎你们访问个人我的网站: http://www.slorl.com