我cy.request用来创建一个新用户。我需要获取userID并使用它来组装一个 url。
例如:
function createUser () {
cy.request({
method: 'GET',
url: `/human/sign_in`
}).then(response => {
const $ = cheerio.load(response.body)
const token = $('css to get the token')
cy.request({
method: 'POST',
url: `/signups/brands`,
form: true,
body: {
'authenticity_token': token,
'name': 'some name',
'email': 'some email'
}
})
}).then(response => {
const $ = cheerio.load(response.body)
const userID = $('css to get userID') // here's the userID
})
}
Run Code Online (Sandbox Code Playgroud)
如何返回 this userID,以及如何在以下代码中引用它?
describe('some feature', () => {
it('should do something', () => {
createUser()
cy.visit(`/account/${userID}`) // how to refer to it?
})
})
Run Code Online (Sandbox Code Playgroud)
我查了官方文档。它似乎as()可以做一些伎俩。但是我找不到as()在cy.request().
谢谢!
我们在测试中使用自定义命令执行相同的操作并从那里返回值。带返回值的自定义命令将自动等待返回值,因此您不必担心异步问题或别名的麻烦。
Cypress.Commands.add("createUser", () {
return cy.request({
method: 'GET',
url: `/human/sign_in`
}).then(response => {
const $ = cheerio.load(response.body)
const token = $('css to get the token')
cy.request({
method: 'POST',
url: `/signups/brands`,
form: true,
body: {
'authenticity_token': token,
'name': 'some name',
'email': 'some email'
}
})
}).then(response => {
const $ = cheerio.load(response.body)
return $('css to get userID') // here's the userID
})
})
Run Code Online (Sandbox Code Playgroud)
那么你的测试将如下所示:
describe('some feature', () => {
it('should do something', () => {
cy.createUser().then(userId => {
cy.visit(`/account/${userID}`)
})
})
})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6311 次 |
| 最近记录: |