如果想从头学起Cypress,可以看下面的系列文章哦
https://www.cnblogs.com/poloyy/category/1768839.html
cy.route()
.route()
cy.server()
cy.server(options)
cy.route()
// 启动服务器
cy.server()
cy.route()
注:演示项目是 cypress 提供的,如何下载可看 Cypress 系列文章的一开始几篇都有写
cd C:\Users\user\Desktop\py\cypress-example-recipes\examples\logging-in__xhr-web-forms
npm start
http://localhost:7079/
context('route 的栗子', function () {
const username = 'jane.lane'
const password = 'password123'
before(function () {
cy.visit('http://localhost:7079/')
})
it('栗子1', function () {
cy.server({
method: 'POST',
status: 503,
delay: 1000,
headers: {
'x-token': 'abc-123-foo-bar'
}
})
cy.route({
url: '**/login',
response: {
success: false,
data: 'Not success'
},
}).as("login")
cy.get("input[name=username]").type(username)
cy.get("input[name=password]").type(`${password}{enter}`)
cy.wait('@login').then((res) => {
cy.log(res)
expect(res.status).to.eq(503)
expect(res.responseBody.data).to.eq('Not success')
expect(res.responseHeaders).to.have.property('x-token', 'abc-123-foo-bar')
})
});
})
it('栗子2', function () {
cy.server()
cy.route({
url: '**/login',
method: 'POST',
status: 503,
response: {
data:"success"
}
}).as("login")
cy.get("input[name=username]").type(username)
//第一次发出请求
cy.get("input[name=password]").type(`${password}{enter}`)
cy.wait('@login').then((res) => {
expect(res.status).to.eq(503)
// 关闭服务器
cy.server({
enable: false
})
})
cy.visit('http://localhost:7079/')
cy.get("input[name=username]").type(username)
//第二次发出请求
cy.get("input[name=password]").type(`${password}{enter}`)
});
第二个请求虽然被路由监听到了,但是因为服务器关闭了,所以并没有获取路由的 status、response
cy.visit()
cy.visit()