访问Link
cy.visit("http:www.baidu.com")
获取当前页面URL和Title
cy.url()
cy.title()//url中应该包含字符XXX,包含文本
cy.url().should("contain","www.baidu.com")
cy.url().should("contain.text","xxx")cy.url().should("have.text","xxx")
//页面title应该等于XXX
cy.title().should("eq","xxx")
页面刷新
cy.reaload()
页面前进和后退
//后退
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) //选中US这个选项 cy.get("radio").check("us"); cy.get('[type="check"]').check(); cy.get('[type="check"]').check(); //取消选中的单选框(radio button)或者复选框(check box) //取消选中 cy.get("radio").uncheck("us"); cy.get('[type="check"]').uncheck();
下拉菜单选择
//获取下拉列表 cy.get("select").select("下拉选项的值"); cy.get("li").eq(0).click();
元素操作
操作 | 对应例子代码 |
操作被覆盖带元素使用{force:true} | cy.get('#btn').click({force:true}) |
元素可见 | should("be.visible") |
元素是否存在 | //判断元素存在 cy.get('.check-box).should('exist') //判断元素不存在 cy.get('.check-box).should('no exist') |
should断言 | .should('be.checked') .should(be.visible) .should('exist') .should('not exist') .should('hava.value', 'xxx') .should('hava.text', 'xxx') .should('hava.attr', 'href', '/users') .should('contain', 'xxx') .should('not.contain', 'xxx') .should('hava.class', 'active') .should('not.hava.clacc', 'disabled') .should('hava.length',3) .should('hava.css', 'text-decoration', 'line-through') |
模拟键盘操作 | //Enter键盘 cy.get('input').type('{enter}') {backspace}:删除光标左侧的字符; {del}:删除光标右侧的字符; {downarrow}:向下移动光标; {end}:将光标移到行尾; {enter}:按Enter键; {esc}:按ESC键; {home}:将光标移到行首; {insert}:在光标右边插入字符; {leftarrow}:向左移动光标; {pagedown}:向下滚动; {pageup}:向上滚动; {rightarrow}:向右移动光标; {selectall}:通过创建选择范围来选择所有文本; {uparrow}:向上移动光标。 |