赛普拉斯:在多个API测试中重复使用身份验证令牌

埃班斯特

我有一个生成令牌的Rest API。此会话令牌在多个REST API中用作授权承载令牌我以此为参考:https : //github.com/cypress-io/cypress-example-recipes/blob/master/examples/logging-in__jwt/cypress/integration/spec.js

但是,在该示例中,生成令牌的功能嵌入在测试中。我试图创建一个应在本地存储的自定义命令,但测试未将其拾取。请注意,自定义命令中不包含返回值。

我的以下代码在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)
  })
})

当我运行测试时,日志显示null“身份”的值。但是,它显示了cy.log(identity.access_token)我使用cy.writeFile尝试放置的自定义命令中的当前值,但我认为这不是一个干净的方法。必须在函数和不同类之间传递数据。

样本JSON格式:

{
  "token": "<this is the value I would like to use for other API's authorisation bearer token>",
  "expires_in": 1200,
  "token_type": "Bearer"
}
哈维尔·布雷亚(Javier Brea)

您可以使用cypress-localstorage-commands包在测试之间持久化localStorage。

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在赛普拉斯测试规范文件中导出可重复使用的Javascript函数

在受Windows身份验证保护的Intranet应用上编写赛普拉斯测试

赛普拉斯测试验证

在赛普拉斯中,在测试之前在localStorage中设置一个令牌

如何使用赛普拉斯测试在文本字段中输入生成的验证码文本?

如何避免赛普拉斯中的重复代码

在赛普拉斯中,如何测试一个属性等于多个值之一?

如何验证赛普拉斯中的错误消息?

在赛普拉斯中运行测试的所有断言

赛普拉斯-在iframe中运行测试

赛普拉斯:登录身份验证重定向到另一个域:解决方法?

如何使用赛普拉斯测试文件输入?

赛普拉斯-使用Google Recaptcha测试联系表

赛普拉斯的条件测试

赛普拉斯劈弦测试

赛普拉斯-排除之前的测试

如何使用jquery在赛普拉斯测试中获取div'text'值

赛普拉斯测试失败,因为Chrome Renderer在CI中崩溃(使用无人机)

如何使用赛普拉斯测试第二个表中的值

赛普拉斯夹具在 TypeScript 中的使用

赛普拉斯中的路径变量

在赛普拉斯中使用别名

使用赛普拉斯执行拖放

赛普拉斯-是否可以将重复的测试部分存储在一个地方?

如何使用赛普拉斯选择文本[赛普拉斯]

将功能导入规格并在赛普拉斯的多个测试之间共享价值的推荐方法?

钩子之前的赛普拉斯在每个 it 语句之后重复

赛普拉斯与SystemJS

在 Azure DevOps 中的“bash 退出,代码为 1”中赛普拉斯测试退出失败