1 回答

TA貢獻2003條經驗 獲得超2個贊
您可以使用cy.wrap()來包裝第一個請求的響應,然后您可以在任何地方使用它。
自定義命令:
Cypress.Commands.add('getAuthToken', () => {
? ? cy.request({
? ? ? ? method: 'POST',
? ? ? ? url: 'https://www.example.com/tokens',
? ? ? ? body: {
? ? ? ? ? ? email: 'admin_username',
? ? ? ? ? ? password: 'admin_password'
? ? ? ? }
? ? }).then((response) => {
? ? ? ? cy.wrap(response).as('getAuthTokenResponse')
? ? })
})
Cypress.Commands.add('createUser', (user) => {
? ? cy.get('@getAuthTokenResponse').then((resp) => {
? ? ? ? cy.request({
? ? ? ? ? ? method: 'POST',
? ? ? ? ? ? url: 'https://www.example.com/users',
? ? ? ? ? ? headers: ({ Authorization: 'Bearer ' + resp.token }),
? ? ? ? ? ? body: user
? ? ? ? })
? ? })
})
在您的測試文件中,您只需添加:
cy.getAuthToken()
cy.createUser(user)
添加回答
舉報