赛普拉斯:在多个API测试中重复使用身份验证令牌 [英] Cypress: Re-use auth token across multiple API tests
问题描述
我有一个生成令牌的Rest API.此会话令牌在多个REST API中用作授权 Bearer令牌.我以此为参考:
I have a Rest API which generates a token. This session token is used across multiple REST API's as an authorization Bearer token. I used this as reference: https://github.com/cypress-io/cypress-example-recipes/blob/master/examples/logging-in__jwt/cypress/integration/spec.js
但是,在该示例中,生成令牌的功能嵌入在测试中.我试图创建一个应在本地存储的自定义命令,但测试未将其拾取.请注意,自定义命令中不包含返回值.
However, in that example, the function to generate token is embedded in the test. I tried to create a custom command for which should store locally but it is not being picked up by the test. Note that no return value is included in the custom command.
下面 support/commands.js 下的我的代码:
let identity
Cypress.Commands.add('postToken', () => {
cy.request({
method: 'POST',
url: Cypress.env('api_identity_url'), //get from cypress.env.json
form: true, //sets to application/x-www-form-urlencoded
body: {
grant_type: 'client_credentials',
scope: 'xero_all-apis'
},
auth: {
username: Cypress.env('api_identity_username'),
password: Cypress.env('api_identity_password')
}
})
.its('body')
.then((response) => {
identity = response
window.localStorage.setItem('identity', JSON.stringify(identity))
cy.log(identity.access_token)
})
})
我的测试:
context('Check token details', () => {
it('Check token', () => {
cy.postToken()
const bToken = window.localStorage.getItem('identity')
cy.log(bToken)
})
})
运行测试时,日志显示'identity'的 null
值.但是,它在我放置 cy.log(identity.access_token)
的自定义命令中显示了当前值.我尝试使用 cy.writeFile ,但我认为这不是一个干净的方法.必须在函数和不同的类之间传递某种数据.
When I run the test, the log shows null
value for 'identity'. However, it shows the current value in the custom command where I placed cy.log(identity.access_token)
I tried using cy.writeFile but I don't think this is a clean method. There must be some way data can be passed between functions, and different classes.
示例JSON格式:
{
"token": "<this is the value I would like to use for other API's authorisation bearer token>",
"expires_in": 1200,
"token_type": "Bearer"
}
推荐答案
您可以使用cypress-localstorage-commands 软件包可在测试之间持久化localStorage.
You can use the cypress-localstorage-commands package to persist localStorage between tests.
在 support/commands.js
中:
import "cypress-localstorage-commands";
Cypress.Commands.add('postToken', () => {
cy.request({
method: 'POST',
url: Cypress.env('api_identity_url'), //get from cypress.env.json
form: true, //sets to application/x-www-form-urlencoded
body: {
grant_type: 'client_credentials',
scope: 'xero_all-apis'
},
auth: {
username: Cypress.env('api_identity_username'),
password: Cypress.env('api_identity_password')
}
})
.its('body')
.then(identity => {
cy.setLocalStorage("identity_token", identity.token);
});
});
在测试内部:
describe("postToken", ()=> {
before(() => {
cy.postToken();
cy.saveLocalStorage();
});
beforeEach(() => {
cy.restoreLocalStorage();
});
it("should exist identity in localStorage", () => {
cy.getLocalStorage("identity_token").should("exist");
cy.getLocalStorage("identity_token").then(token => {
console.log("Identity token", token);
});
});
it("should still exist identity in localStorage", () => {
cy.getLocalStorage("identity_token").should("exist");
cy.getLocalStorage("identity_token").then(token => {
console.log("Identity token", token);
});
});
});
这篇关于赛普拉斯:在多个API测试中重复使用身份验证令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!