我的 cypress.config.js 文件中有以下内容:
clientCertificates: [
{
url: 'https://1.1.1.1',
ca: [],
certs: [
{
cert: 'certs/cert.pem',
key: 'certs/cert.key'
},
],
} ],
当我针对 1.1.1.1 进行测试时(作为示例),这非常有效。但我不想签入 gitlab 的证书文件。如果我不针对 1.1.1.1 进行测试,而只测试不需要证书的 localhost,我不希望 Cypress 关心这些证书文件是否存在。因此,我希望其他开发人员检查我的测试存储库并针对本地主机进行测试,而不会因为这些证书文件不存在而失败。但到目前为止,如果我删除这些证书文件,然后针对本地主机进行测试,Cypress 将失败:
无法加载 clientCertificates[0] 的客户端证书:ENOENT: 没有这样的文件或目录,请打开“certs/cert.pem”。更多调试 详细信息使用 DEBUG=cypress:server:client-certificates* 运行 Cypress
如果我针对本地主机进行测试,为什么 Cypress 会关心这些文件是否存在?
那么,如果我说得有道理,我该如何解决这个问题呢?我可能可以制作名为 cert.pem 和 cert.key 的虚拟文件,并要求如果有人针对 1.1.1.1 进行测试,则将这些文件替换为有效的证书。但这似乎有点老套。我什至尝试过“chromeWebSecurity: false”,但它仍然希望这些文件存在。有没有办法有条件地查看 clientCertificates?
感谢您的任何建议。 乔恩
您可以将证书拆分成一个单独的文件(您可以
.gitignore
以防止发送到存储库)。
如果项目中存在
cypress.config.js
文件,client-certs.json
中的一点JavaScript可以有条件地加载。
const { defineConfig } = require("cypress");
// check for certs to load
let certs
const fs = require('fs')
const certsPath = './client-certs.json'
if (fs.existsSync(certsPath)) {
const json = require(certsPath)
certs = json.clientCertificates
}
module.exports = defineConfig({
e2e: {
setupNodeEvents(on, config) {
if (certs) {
config.clientCertificates = certs
}
return config
},
},
})