赛普拉斯如何 return 来自本地存储的 JWT 令牌并在另一个 api 调用中使用它
Cypress how to return JWT token from localstorage and use it in another api call
嗨,我想做的是将 localStorage 保存到变量中的某个地方,这样我就可以在不同的测试场景中引用它,但我不确定这是否可行,因为我的 token1 变量始终为空。
这是我的 support/command.js 文件
Cypress.Commands.add('postTokenLogin', () => {
cy.request({
method: 'POST',
url: '***/people/sign_in',
body: {
"login": "test@test.com",
"password":"***",
},
headers:{
'content-type': 'application/json'
}
}).then((response) => {
expect(response.body).have.property('firstName')
expect(response.body.token).have.property('authorization')
cy.setLocalStorage('token',response.body.token.authorization )
})
})
现在在我的测试用例中,我希望能够在我的 header
中使用该令牌
import "cypress-localstorage-commands";
let token1 = '';
describe('HTTP Example', ()=>{
before(() => {
cy.postTokenLogin();
cy.saveLocalStorage();
});
beforeEach(() => {
cy.restoreLocalStorage();
});
it("the value of JWT Token should exist in localStorage", () => {
cy.getLocalStorage('token').then(token => {
cy.log("the token", token); // I get JWT Token in here
});
});
it('GET List ', ()=>{
cy.getLocalStorage('token').then((token) => {
token1 = token;
})
cy.log('Let Tokennn is ===>' , token1) // Always Empty
cy.request({
method: 'GET',
url: '***/peopleList',
headers:{
'content-type': 'application/json',
'Authorization': token1 // ===> this is also empty
}
}).then((response) => {
expect(response.body).have.property('firstName')
expect(response.body).have.property('lastname')
})
})
})
我可以再要一个吗('GET colours ', ()=>{}) 然后只传递 token1?
您正在使用异步代码,因此如果您需要使用令牌而不是验证,您应该像下面这样嵌套代码
import "cypress-localstorage-commands";
let token1 = '';
describe('HTTP Example', () => {
before(() => {
cy.postTokenLogin();
cy.saveLocalStorage();
});
beforeEach(() => {
cy.restoreLocalStorage();
});
it("the value of JWT Token should exist in localStorage", () => {
cy.getLocalStorage('token').then(token => {
cy.log("the token", token); // I get JWT Token in here
});
});
it('GET List ', () => {
cy.getLocalStorage('token').then((token) => {
token1 = token;
cy.log('Let Tokennn is ===>', token1) // Always Empty
cy.request({
method: 'GET',
url: '***/peopleList',
headers: {
'content-type': 'application/json',
'Authorization': token1 // ===> this is also empty
}
}).then((response) => {
expect(response.body).have.property('firstName')
expect(response.body).have.property('lastname')
})
})
})
嗨,我想做的是将 localStorage 保存到变量中的某个地方,这样我就可以在不同的测试场景中引用它,但我不确定这是否可行,因为我的 token1 变量始终为空。
这是我的 support/command.js 文件
Cypress.Commands.add('postTokenLogin', () => {
cy.request({
method: 'POST',
url: '***/people/sign_in',
body: {
"login": "test@test.com",
"password":"***",
},
headers:{
'content-type': 'application/json'
}
}).then((response) => {
expect(response.body).have.property('firstName')
expect(response.body.token).have.property('authorization')
cy.setLocalStorage('token',response.body.token.authorization )
})
})
现在在我的测试用例中,我希望能够在我的 header
中使用该令牌import "cypress-localstorage-commands";
let token1 = '';
describe('HTTP Example', ()=>{
before(() => {
cy.postTokenLogin();
cy.saveLocalStorage();
});
beforeEach(() => {
cy.restoreLocalStorage();
});
it("the value of JWT Token should exist in localStorage", () => {
cy.getLocalStorage('token').then(token => {
cy.log("the token", token); // I get JWT Token in here
});
});
it('GET List ', ()=>{
cy.getLocalStorage('token').then((token) => {
token1 = token;
})
cy.log('Let Tokennn is ===>' , token1) // Always Empty
cy.request({
method: 'GET',
url: '***/peopleList',
headers:{
'content-type': 'application/json',
'Authorization': token1 // ===> this is also empty
}
}).then((response) => {
expect(response.body).have.property('firstName')
expect(response.body).have.property('lastname')
})
})
})
我可以再要一个吗('GET colours ', ()=>{}) 然后只传递 token1?
您正在使用异步代码,因此如果您需要使用令牌而不是验证,您应该像下面这样嵌套代码
import "cypress-localstorage-commands";
let token1 = '';
describe('HTTP Example', () => {
before(() => {
cy.postTokenLogin();
cy.saveLocalStorage();
});
beforeEach(() => {
cy.restoreLocalStorage();
});
it("the value of JWT Token should exist in localStorage", () => {
cy.getLocalStorage('token').then(token => {
cy.log("the token", token); // I get JWT Token in here
});
});
it('GET List ', () => {
cy.getLocalStorage('token').then((token) => {
token1 = token;
cy.log('Let Tokennn is ===>', token1) // Always Empty
cy.request({
method: 'GET',
url: '***/peopleList',
headers: {
'content-type': 'application/json',
'Authorization': token1 // ===> this is also empty
}
}).then((response) => {
expect(response.body).have.property('firstName')
expect(response.body).have.property('lastname')
})
})
})