我如何使用多个(而不是单个)客户端证书登录cypress?

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

我们可以使用多个客户端证书进行登录吗?例如:我需要以 [电子邮件受保护] 身份登录应用程序,然后我需要再次以 [电子邮件受保护] 身份登录。那么是否可以使用证书以多用户身份登录?

我已成功配置一个客户端证书并成功登录。但无法找到使用不同用户 ID 进行多次登录的解决方案?期待解决方案

我将证书存储在 cert 文件夹中并将其链接到 cypress.config 文件中,它适用于单个证书。

javascript automation cypress client-certificates
1个回答
0
投票

Cypress 配置确实支持多个证书,但它是基于每个 URL,而不是基于每个用户。

参见客户端证书

module.exports = defineConfig({
  clientCertificates: [
    {
      url: 'https://a.host.com',
      ca: ['certs/ca.pem'],
      certs: [
        {
          ...
        },
      ],
    },
    {
      url: 'https://b.host.com/a_base_route/**',
      ca: [],
      certs: [
        {
          ...
        },
      ],
    },
    {
      url: 'https://a.host.*.com/',
      ca: [],
      certs: [
        {
          ...
        },
      ],
    },
  ],
})

这可能足以解决您的问题,只需将所有证书添加到数组中,但我怀疑并非如此。

相反,您可能需要在测试中动态更改证书。

  • 将证书添加到
    certificates.json
    文件
  • 为每个证书添加
    user
    属性
  • 在测试的某个时刻更改证书

证书归档 -

certificates.json

{
  clientCertificates: [
    {
      user: 'Sandy', 
      url: 'https://a.host.com',
      ca: ['certs/ca.pem'],
      certs: [
        {
          ...
        },
      ],
    },
    {
      user: 'Mandy', 
      url: 'https://b.host.com/a_base_route/**',
      ca: [],
      certs: [
        {
          ...
        },
      ],
    },
    {
      user: 'Randy', 
      url: 'https://a.host.*.com/',
      ca: [],
      certs: [
        {
          ...
        },
      ],
    },
  ],
}

动态更改证书

const certificates = require('certificates.json')

it('Tests with certificate for Randy', () => {

  const userCert = Cypress._.find(certificates, { 'user': 'Randy' })
  const configCert = Cypress._.omit(userCert , ['user'])   
  Cypress.config('clientCertificates', [ configCert ])

  ...

这是代码的要点,您可以通过多种方式进行修改,例如在

cypress/support/e2e.js
中创建自定义命令,使每个测试中的一行或在套件的顶部(在
describe()
内)。

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