在發送正文“電子郵件”和“密碼”后,https: //identitytoolkit.googleapis.com/ 在回應中回傳一些物件。其中之一是“idToken”:具有令牌值。
我需要什么?
我需要獲取此令牌,將其存盤在變數中并在進一步的測驗中重復使用。
到目前為止,我準備了這樣的東西:
it("Get a fresh admin firebase token", () => {
cy.request({
method: "POST",
url: "https://identitytoolkit.googleapis.com/...",
body: {
"email": "myUsername",
"password": "myPassword",
"returnSecureToken": true
},
headers: {
accept: "application/json"
}
}).then((responseToLog) => {
cy.log(JSON.stringify(responseToLog.body))
}).then(($response) => {
expect($response.status).to.eq(200);
})
})
})```
Above code works, but cy.log() returns the whole body response. How can I separate only idToken and reuse it in my next API scenarios?
uj5u.com熱心網友回復:
考慮到idToken在回應體中,then()您可以直接包裝值并使用別名保存它,然后再使用它。
it('Get a fresh admin firebase token', () => {
cy.request({
method: 'POST',
url: 'https://identitytoolkit.googleapis.com/...',
body: {
email: 'myUsername',
password: 'myPassword',
returnSecureToken: true,
},
headers: {
accept: 'application/json',
},
}).then((response) => {
cy.wrap(response.body.idToken).as('token')
})
})
cy.get('@token').then((token) => {
cy.log(token) //logs token or Do anything with token here
})
如果您想在不同的it塊中使用令牌,您可以:
describe('Test Suite', () => {
var token
it('Get a fresh admin firebase token', () => {
cy.request({
method: 'POST',
url: 'https://identitytoolkit.googleapis.com/...',
body: {
email: 'myUsername',
password: 'myPassword',
returnSecureToken: true,
},
headers: {
accept: 'application/json',
},
}).then((response) => {
token = response.body.idToken
})
})
it('Use the token here', () => {
cy.log(token) //prints token
//use token here
})
})
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/325927.html
