describe 声明一个测试用例集
beforeEach 测试用例前置操作,相当于setup
it声明了一个测试用例
cy.visit 打开网页
cy.get 定位元素,用css selector定位选择器
type 输入文本
focus 聚焦元素
submit 提交表单
click 单击
dblclick 双击
rightclick 右击
check 是否被选中
uncheck checkbox or radio 未被选中
scrollIntoView 内嵌滚动条滑动
cy.scrollTo 浏览器自带的滚动条
trigger 触发事件
clear 清空文本
screenshot 屏幕截图,保存路径screenshots/my-image.png
wrap 包装对象 {foo: bar}
cy.get('.misc-form').find('#name').click()
cy.focused().should('have.id', 'name') 点击元素后判断是否聚焦
cy.go('back'/-1) 'forward'/1使用浏览器的缓存功能,访问上一页,下一页
cy.location('pathname').should('not.include', 'navigation')
cy.reload()不使用检测,重新加载页面
// reload the page without using the cache
cy.reload(true)
直接通过 url 地址访问页面
cy.visit('https://example.cypress.io/commands/navigation', {
timeout: 50000, // increase total time for the visit to resolve
onBeforeLoad: function(contentWindow){
// contentWindow is the remote page's window object
},
onLoad: function(contentWindow){
// contentWindow is the remote page's window object
}
})
end 结束命令链
exec 退出系统命令
.as()使用别名定位元素
cy.get('.as-table').find('td').first().find('button').as('firstBtn')
.as() 重新命名路由
cy.server()
cy.route('GET', 'comments/*').as('getComment')
cy.wait('@getComment').its('status').should('eq', 200)
each 遍历
its 判断选项里面元素个数
invoke('show') 隐藏元素变显示
then 回调函数
使用cy.contains()根据元素的内容找到元素
cy.get('.query-list')
.contains(/^b\w+/).should('have.class', 'third')
root上一级目录,否则为根目录document
cy.root().should('have.class', 'query-ul')
viewport()
设置不同分辨率查看显示效果
cy.viewport(320, 480)
cy.viewport('iphone-6+')
cy.viewport('ipad-2', 'portrait')
cy.viewport('iphone-4', 'landscape')
获取全局窗口对象
cy.window().should('have.property', 'top')
获取document对象
cy.document().should('have.property', 'charset').and('eq', 'UTF-8')
获取标题
cy.title().should('include', 'Kitchen Sink')
cy.url()
should 隐式断言:
- hava.value 是元素的value属性值,判断是否为‘’
- have.attr ('have.attr', 'placeholder', 'Email')
- have.text
- have.class
- have.html
- have.length
- have.property--'have.property', 'value', 'bar'
- exist
- include--('include', 'bar')
- contain
- be.checked
- be.null
- not.be.checked
- be.visible
- not.be.visible
- be.hidden
- eq
- match
- be.gt
- be.empty
针对同一元素多个断言,可以使用 and 语法
expect assert显示断言
expect(cookies[0]).to.have.property('value', '123ABC')
setCookie
getCookies
clearCookies
参数化
describe('参数化案例,输入不同的值', function() {
// 定义测试数据
var testdatas = ["sss", "丫丫", "123456"]
// 前置-打开浏览器
before(() => {
cy.visit('https://www.baidu.com')
})
// 参数化
testdatas.forEach((event) => {
it("百度输入框功能", function () {
cy.get('#kw').type(event)
.should('have.value', event)
.clear()
.should('have.value', '')
})
})
})
fixture 目录写个 login.json
存放测试数据
describe('登陆web网站案例', function() {
beforeEach(() => {
//cy.visit('http://xxxx:8080/zentao/user-login.html');
cy.fixture('login.json').as('login')
})
it("登陆案例", function ()
{
cy.log("读取login.json文件账号:"+this.login.username)
cy.log("读取login.json文件密码:"+this.login.password)
// let 定义变量
let username = this.login.username
let password = this.login.password
})
})
Hooks 和测试执行的顺序如下:
before()钩子运行(一次)
beforeEach() 每个测试用例前都会运行
it 运行测试用例
afterEach() 每个测试用例之后都会运行
after() 钩子运行(一次)
网友评论