单击后赛普拉斯获取对象 return
Cypress get object return after click
我正在使用赛普拉斯对 nuxt 进行一些测试,我想在注册后检查我的 return 对象,打印数据工作正常但我永远无法取出数据,赛普拉斯是否可能检查 console.log return 对象并将其用作测试的证明 ?
我的意图是使用 return 响应状态 201(创建)并按预期使用。
describe('Testing Register to backend', () => {
const dummyUsername = 'Mxaabyxxcxxtxisxxsxx1suxsaxxx'
const dummyEmail = 'Micxaebuxcxuxxxitxxssasx1sxx@gmail.com'
const dummyPassword = 'aabjxlyucxxixxtassss21xx34x'
beforeEach(function() {
cy.visit('my-account') // we run our custom command
})
it('Should able get through register and return 200', () => {
cy.get('.ps-tab-li #toggle__Register').click({
force: true
})
cy.get('.form-group #register__tab--username').type(dummyUsername, {
force: true
});
cy.get('.form-group #register__tab--email').type(dummyEmail, {
force: true
});
cy.get('.form-group #register__tab--password').type(dummyPassword, {
force: true
});
cy.get('button').contains('Register').click().wait(3000).then((response) => {
// console.log()
cy.log(response)
// isit possible to get object data on log to use in expect ?
// HTTP 201 Created success status response code
// expect(xhr.status).to.eq(201)
})
})
})
您需要在发布数据的 URL 上设置拦截。
大致上,
cy.intercept('POST', url).as('post') // set this up at top
// fill in form...
cy.get('button').contains('Register').click() // trigger POST to url
cy.wait('@post') // wait for intercept instead of cy.wait(3000)
.then(interception => {
// read the response
expect(interception.response.status).to.eq(201)
})
One-off 通配符
如果您在点击后无法捕获 URL,请添加一个捕获任何内容的拦截,并将其设置为仅捕获一个响应(下一个)。
// set this up at top of test
cy.intercept('POST', '*', { times: 1 }) // 'POST, '*' means any URL
.as('post') // times:1 means catch once only
// then turn off
// fill in form...
cy.get('button').contains('Register').click() // trigger POST
cy.wait('@post') // wait for intercept
.then(interception => {
// read the response
expect(interception.response.status).to.eq(201)
})
说明
cy.intercept()
命令的 URL 参数是对来自应用程序的请求的过滤器。当您添加通配符时,它会捕获更多请求,例如 **/api/*
是捕获任何 API 请求的标准模式。
如果您无法捕获特定的 URL,您可以使用 *
,它可以捕获所有内容。
但是 cy.intercept()
对所有测试仍然有效,所以如果我们添加 {times: 1}
那么它只会应用于下一个请求,然后停止捕获请求。因此它会捕获来自按钮单击的请求,但不会干扰您稍后添加的任何其他也需要拦截的测试。
我建议这应该是一个临时步骤,以帮助找出 URL 需要捕获的内容。查看拦截请求的属性,找出更好的 URL 来过滤
.then(interception => {
console.log(interception.request) // take a look at request URL property
// in devtools
...
我正在使用赛普拉斯对 nuxt 进行一些测试,我想在注册后检查我的 return 对象,打印数据工作正常但我永远无法取出数据,赛普拉斯是否可能检查 console.log return 对象并将其用作测试的证明 ?
我的意图是使用 return 响应状态 201(创建)并按预期使用。
describe('Testing Register to backend', () => {
const dummyUsername = 'Mxaabyxxcxxtxisxxsxx1suxsaxxx'
const dummyEmail = 'Micxaebuxcxuxxxitxxssasx1sxx@gmail.com'
const dummyPassword = 'aabjxlyucxxixxtassss21xx34x'
beforeEach(function() {
cy.visit('my-account') // we run our custom command
})
it('Should able get through register and return 200', () => {
cy.get('.ps-tab-li #toggle__Register').click({
force: true
})
cy.get('.form-group #register__tab--username').type(dummyUsername, {
force: true
});
cy.get('.form-group #register__tab--email').type(dummyEmail, {
force: true
});
cy.get('.form-group #register__tab--password').type(dummyPassword, {
force: true
});
cy.get('button').contains('Register').click().wait(3000).then((response) => {
// console.log()
cy.log(response)
// isit possible to get object data on log to use in expect ?
// HTTP 201 Created success status response code
// expect(xhr.status).to.eq(201)
})
})
})
您需要在发布数据的 URL 上设置拦截。
大致上,
cy.intercept('POST', url).as('post') // set this up at top
// fill in form...
cy.get('button').contains('Register').click() // trigger POST to url
cy.wait('@post') // wait for intercept instead of cy.wait(3000)
.then(interception => {
// read the response
expect(interception.response.status).to.eq(201)
})
One-off 通配符
如果您在点击后无法捕获 URL,请添加一个捕获任何内容的拦截,并将其设置为仅捕获一个响应(下一个)。
// set this up at top of test
cy.intercept('POST', '*', { times: 1 }) // 'POST, '*' means any URL
.as('post') // times:1 means catch once only
// then turn off
// fill in form...
cy.get('button').contains('Register').click() // trigger POST
cy.wait('@post') // wait for intercept
.then(interception => {
// read the response
expect(interception.response.status).to.eq(201)
})
说明
cy.intercept()
命令的 URL 参数是对来自应用程序的请求的过滤器。当您添加通配符时,它会捕获更多请求,例如 **/api/*
是捕获任何 API 请求的标准模式。
如果您无法捕获特定的 URL,您可以使用 *
,它可以捕获所有内容。
但是 cy.intercept()
对所有测试仍然有效,所以如果我们添加 {times: 1}
那么它只会应用于下一个请求,然后停止捕获请求。因此它会捕获来自按钮单击的请求,但不会干扰您稍后添加的任何其他也需要拦截的测试。
我建议这应该是一个临时步骤,以帮助找出 URL 需要捕获的内容。查看拦截请求的属性,找出更好的 URL 来过滤
.then(interception => {
console.log(interception.request) // take a look at request URL property
// in devtools
...