Cypress02:基础命令使用

访问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("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')

模拟键盘操作

//Enter键盘

cy.get('input').type('{enter}')

{backspace}:删除光标左侧的字符;

{del}:删除光标右侧的字符;

{downarrow}:向下移动光标;

{end}:将光标移到行尾;

{enter}:按Enter键;

{esc}:按ESC键;

{home}:将光标移到行首;

{insert}:在光标右边插入字符;

{leftarrow}:向左移动光标;

{pagedown}:向下滚动;

{pageup}:向上滚动;

{rightarrow}:向右移动光标;

{selectall}:通过创建选择范围来选择所有文本;

{uparrow}:向上移动光标。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值