访问Link
cy.visit("http:www.baidu.com")
cy.visit("http://xxxxx/login")
//设置baseUrl后,修改为 cy.visit("/login")
获取当前页面URL和Title
cy.url()
cy.title()
//url中应该包含字符XXX,包含文本
cy.url().should("contain","www.baidu.com")
cy.url().should("contain.text","xxx")
//have.text等同于 eq,如果只是多个文本中的一个则使用contain.text
cy.url().should("have.text","xxx")
//页面title应该等于XXX
cy.title().should("eq","xxx")
页面刷新
cy.reaload()
等待
cy.wait(1000) //等待1s
页面前进和后退
//后退
cy.go('back)
cy.go(-1)
//前进
cy.go('forward)
cy.go(1)
输入文本
输入框输入文本,需要先清空在输入
cy.get("input").clear().type(“需要输入的文本”)
单选/多选按钮选中或取消
选中单选框(radio button)
或者复选框(check box)
取消选中的单选框(radio button)
或者复选框(check box)
//选中单选框(radio button)或者复选框(check box)
//选中name这个选项
cy.get("radio").check("name");
cy.get('[type="check"]').check();
cy.get('[type="check"]').check();
//取消选中的单选框(radio button)或者复选框(check box)
//取消选中
cy.get("radio").uncheck("name");
cy.get('[type="check"]').uncheck();
下拉菜单选择
//获取下拉列表
cy.get("select").select("下拉选项的值");
cy.get("li").eq(0).click();
断言
.should('be.checked')
.should("be.visible")
.should('exist')
.should('not exist')
.should('contain', 'xxx')
.should('not.contain', 'xxx')
.should('hava.value', 'xxx')
.should('hava.text', 'xxx')
.should('hava.attr', 'href', '/users')
.should('hava.class', 'active')
.should('not.hava.clacc', 'disabled')
.should('hava.length',3)
.should('hava.css', 'text-decoration', 'line-through')
常用代码汇总
操作 | 对应例子代码 |
---|---|
排除测试套件describe( ) | describe.skip() |
排除测试用例it( ) | it.skip() |
指定要运行的测试套件 | describe.only |
指定要运行的测试用例 | it.only |
操作被覆盖带元素使用{force:true} | cy.get('#btn').click({force:true}) |
元素可见 | should("be.visible") |
元素是否存在 | //判断元素存在 cy.get('.check-box).should('exist') //判断元素不存在 cy.get('.check-box).should('no exist') |
将上一条命令的返回结果注入下一个命令中 | cy.then() |
创建一个断言,断言将自动重试,直到他们通过或超时 | cy.and() |
将数组作为单独的参数传会到回调函数 | cy.spread() |
对上一条命令的结果执行调用方法操作 | cy.invoke() |
获取属性值 | cy.its() |
取别名 | cy.as() |
遍历当前元素 | cy.each() |
设置窗口 | { //在cypress.json中添加'viewportWidth':'1000', 'viewportHeight':'600' } //运行中设置 cy.viewpoint(1024,768) |
获取元素属性值 | //获取元素btn的文本 cy.get('#btn').then(function(){ const btnTxt = $btn.text() cy.log(btnTxt) }) |
should断言 | .should('be.checked') .should('contain', 'xxx') .should('hava.value', 'xxx') |
模拟键盘操作 | //Enter键盘 cy.get('input').type('{enter}') {backspace}:删除光标左侧的字符; {del}:删除光标右侧的字符; {downarrow}:向下移动光标; {end}:将光标移到行尾; {enter}:按Enter键; {esc}:按ESC键; {home}:将光标移到行首; {insert}:在光标右边插入字符; {leftarrow}:向左移动光标; {pagedown}:向下滚动; {pageup}:向上滚动; {rightarrow}:向右移动光标; {selectall}:通过创建选择范围来选择所有文本; {uparrow}:向上移动光标。 |