为 Cypress 中的所有请求添加基本身份验证

问题描述 投票:0回答:5

我是 Cypress 新手,需要为所有

cy.visit()
调用添加基本身份验证。 授权凭据取决于部署(即它们特定于我们在环境配置文件中设置的“baseUrl”)。

目前,我有;

cy.visit("/", {
  auth: {
    username: '...',
    password: '...'
  }
});

我想要的是将“auth”对象移动到 evg 配置文件,所以我只需要

cy.visit("/")
在规格中。

非常感谢

authentication testing cypress e2e-testing
5个回答
2
投票

如果您打算重用身份验证,那么最好创建一个单独的身份验证方法,例如:

1。在 `cypress/support/commands.js 中创建一个新的自定义命令, 因为它是在通过 supportFile(默认为 cypress/support/index.js)中的 import 语句评估任何测试文件之前加载的。

Cypress.Commands.add('login', () => {
    // (you can use the authentification via API request)
    return cy
        .request({
            method: 'POST',
            url: your_url,
            form: true,
            body: {
                username: Cypress.env('username'),
                password: Cypress.env('password'),
                grant_type: 'password',
                client_id: your_clientId,
                client_secret: your_clientSecret,
                scope: 'openid',
            },
        })
})

2。然后在你的测试中使用它:

describe('My Test Name', () => {

    before(() => {
        cy.login();
    });

    it('should visit my base URL', () => {
        cy.visit('/');
    });
});

注 1: 在这里查看如何设置环境变量:Cypress.io: Environments Variables

注2:在这里查看如何使用自定义命令:Custom Commands - Correct Usage


1
投票

编辑:因为你的语法是正确的 - 我将分享我在任务中使用的方法。

如果您的身份验证工作正常,您可以制作自定义命令 - visitAndAuthorise 例如:

Cypress.Commands.add("shopAdmin_visitAndLogin", (url) => {
    cy.log('shopAdmin_visitAndLogin')
    cy.visit(url)
    cy.get('[data-qa="emailInput"]')
        .type(Cypress.env('credentials').email)
    cy.get('[data-qa="passwordInput"]')
        .type(Cypress.env('credentials').password)
    cy.get('[data-qa="loginButton"]')
        .click()
    cy.get('[data-qa="logOutButton"]')
        .should('be.visible')
})

你的 cypress.env.json 文件需要包含一个像这样的凭证对象:

{
   "credentials": {
      "email": "[email protected]",
      "password": "myPassword"
   }
}

或者按照你的语法:

Cypress.Commands.add("shopAdmin_visitAndLogin", (url) => {
    cy.log('shopAdmin_visitAndLogin')
    cy.visit(url, {
auth: {
    username: Cypress.env('credentials').username,
    password: Cypress.env('credentials').password
  }})
})

1
投票

如果您对所有页面都有 HTTP 基本身份验证,请将此代码添加到您的

cypress/support/commands.js

Cypress.Commands.overwrite('visit',  (originalFn, url, options) => {

options = options || {}

options.auth = {
  username: 'replace_this_with_the_username',
  password: 'replace_this_with_the_password'
}

return originalFn(url, options);
});

0
投票

这就是我使用 cy.request 使用 Cypress 处理 Basic Auth 的方式:

cy.request({
        method:'POST',
        url:'myURL',
        body: {
            Name: name,
            userId: userId,
            languageId: languageId
          },
          headers: {
            authorization: 'Basic lasdkfjlsdyZHRoYXRpa25vdzp'
          },
    }).then(response => {
       expect(response.status).to.equal(201)
        })
    })

基本上,cy.request 中的“headers”对象发挥了作用。


0
投票

这个话题比乍看起来要复杂得多。例如

  • 如何配置测试以在不同环境(DEV、UAT、PROD)中运行
  • 如何避免在测试运行时泄露密码(在 Cypress 日志中)

我在“在 E2E 测试中保持密码保密”一文中找到了非常有用的提示,内容涉及如何在 Cypress 中编写代码,使用户登录但不将密码写入日志。它还提供了如何配置以及安全存储密码的提示。

本文的主要主题是:

  • 可见密码
  • 不要硬编码密码
  • 验证密码
  • 避免 UI 登录
  • 持续集成
  • 插件
  • 结论

关于在测试中正确定义和使用环境变量的更多提示:

© www.soinside.com 2019 - 2024. All rights reserved.