web页面上的导航功能,上一页,下一页,重新加载页面,直接访问页面。
.go() 上一页,下一页
语法:
cy.go(direction)
cy.go(direction, options)
使用:
上一页
cy.go(-1)
cy.go('back')
下一页
cy.go(1)
cy.go('forward')
.reload() 刷新页面
语法:
cy.reload()
cy.reload(forceReload)
cy.reload(options)
cy.reload(forceReload, options)
例:
cy.visit('http://localhost:3000/admin')
cy.get('#undo-btn').click().should('not.be.visible')
cy.reload()
cy.get('#undo-btn').click().should('not.be.visible')
.visit() 访问页面
语法:
cy.visit(url)
cy.visit(url, options)
cy.visit(options)
例
cy.visit('http://localhost:3000') // Yields the window of the remote page
cy.visit('./pages/hello.html')
options:
- url – null 要访问的URL
- method – GET 请求方法(GET,POST)
- bpdy – null 随POST请求一起发送的可选主体。如果它是一个字符串,它将不加修改地传递。如果它 是一个对象,它将是URL编码的字符串,并发送一个Content-Type:application/x-www-urlencoded头部
- headers – {} 请求头部
- qs – null 附加到请求url的查询参数
- log – true 日志信息
- auth – null 添加基本授权头信息
- failOnStatusCode – 2xx和3xx以外的响应码是否失败
- onBeforeLoad – function 在页面加载其所有资源之前调用
- onLoad – function 在页面触发其加载事件后调用。
- retryOnStatusCodeFailure – false Cypress是否应该在后台自动重试状态代码错误。如果设置为true,
Cypress将重试请求最多4次 - retryOnNetworkFailure – true Cypress是否应该自动重试暂态网络错误的引擎盖。如果设置为true,
Cypress将重试请求最多4次。 - timeout – pageLoadTimeout 页面超时时间
超时时间设置
cy.visit('/index.html', { timeout: 30000 })
授权 auth
cy.visit('https://www.acme.com/', {
auth: {
username: 'wile',
password: 'coyote'
}
})
将用户名密码放在URL中
cy.visit('https://wile:coyote@www.acme.com')
onBeforeLoad
cy.visit('http://localhost:3000/#dashboard', {
onBeforeLoad: (contentWindow) => {
// contentWindow is the remote page's window object
}
})
onLoad
cy.visit('http://localhost:3000/#/users', {
onLoad: (contentWindow) => {
// contentWindow is the remote page's window object
if (contentWindow.angular) {
// do something
}
}
})
qs
// visits http://localhost:3500/users?page=1&role=admin
cy.visit('http://localhost:3500/users', {
qs: {
page: '1',
role: 'admin'
}
})
参数直接通过url传递
// visits http://example.com/users?page=1&admin=true
cy.visit('http://example.com/users?page=1', {
qs: { admin: true }
})
提交表单
cy.visit({
url: 'http://localhost:3000/cgi-bin/newsletterSignup',
method: 'POST',
body: {
name: 'George P. Burdell',
email: 'burdell@microsoft.com'
}
})