小编eba*_*ter的帖子

Cypress:在多个 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)
        })
})
Run Code Online (Sandbox Code Playgroud)

我的测试

context('Check token details', () => {
  it('Check token', () => {
      cy.postToken()
      const bToken = window.localStorage.getItem('identity')
      cy.log(bToken) …
Run Code Online (Sandbox Code Playgroud)

javascript api rest automation cypress

7
推荐指数
1
解决办法
9736
查看次数

有没有办法强制 cypress 在同一选项卡而不是另一个选项卡中打开

我知道赛普拉斯的权衡之一是在多个选项卡上进行测试。但是,我们的网站默认打开另一个选项卡。我可以强制 Cypress 在同一选项卡上打开以继续我的测试吗?我有下面的代码,但仍然打开一个新选项卡:

cy.get(element).invoke('attr', 'target', ' _self').click()
Run Code Online (Sandbox Code Playgroud)

我记得在某个地方找到了它可以完成,但我凌晨 1 点的大脑无法通过谷歌搜索找到它。我还在 Cypress 文档中找到了这一点,但它可能与我的情况无关,因为我需要在通过 SSO 登录的新页面上执行多个断言: https: //docs.cypress.io/api/commands/ invoke.html#带参数的函数

javascript automated-tests cypress

5
推荐指数
2
解决办法
9174
查看次数

标签 统计

cypress ×2

javascript ×2

api ×1

automated-tests ×1

automation ×1

rest ×1