【问题标题】:Cypress how to return JWT token from localstorage and use it in another api call赛普拉斯如何从本地存储返回 JWT 令牌并在另一个 api 调用中使用它
【发布时间】:2021-06-14 07:57:33
【问题描述】:

嗨,我要做的是将 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 )
    })
})

现在在我的测试用例中,我希望能够在我的标头中使用该令牌

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 colors', ()=>{}) 并传递 token1 吗?

【问题讨论】:

    标签: jwt token cypress


    【解决方案1】:

    您正在使用异步代码,因此如果您需要使用令牌而不是验证,您应该嵌套如下代码

    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')
              })
            })
    
          })
    

    【讨论】:

    • 感谢您的回复。我怎样才能将该令牌存储在某个地方,以便我可以在其他任何地方使用它?
    • 将其转储到文件中并再次读取 docs.cypress.io/api/commands/writefile/#Text ` cy.writeFile('path/to/token.txt', 'tokenvalue') cy.readFile('path/to/token.txt' ).then((token) => { .... }) `
    • @RahulL 谢谢你的回答是我想要的
    猜你喜欢
    • 2023-02-14
    • 2020-08-17
    • 1970-01-01
    • 2019-04-18
    • 1970-01-01
    • 2020-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多