MAILOSAUR_API_KEY 添加到 cypress.json 或 <filename>config.json 文件时未被读取

MAILOSAUR_API_KEY not being read when added to cypress.json or <filename>config.json file

我遇到一个问题,在 cypress returns 中出现错误:You must set the CYPRESS_MAILOSAUR_API_KEY environment variable to use the Mailosaur plugin.

我尝试将 MAILOSAUR_API_KEY 放入 cypress.json 文件和 cypress.env.json 中。我认为 CYPRESS_MAILOSAUR_API_KEY 应该用于系统环境变量,在这种情况下是否需要?或者我在 cypress.json 和 env.json 中的变量都没有被读取? (我的 cypress.env.json 文件与 cypress.json 文件位于根目录文件夹中)

cypress.json 文件:

{
    "baseUrl": "<base-url-here>",
    "chromeWebSecurity": false,
    "MAILOSAUR_API_KEY": "<api-key-here>"
}

cypress.env.json 文件:

{
  "extends": "./cypress.json",
  "chromeWebSecurity": false,
  "MAILOSAUR_API_KEY": "<api-key-here>",
}

commands.js

Cypress.Commands.add("testMailosaur", (serverId, emailAddress) => {
  cy.mailosaurGetMessage(serverId, {
    sentTo: emailAddress,
  }).then((email) => {
    expect(email.subject).to.equal("Test email address");
    return email.text.body
  });
});

错误详情:

You must set the CYPRESS_MAILOSAUR_API_KEY environment variable to use the Mailosaur plugin.

  18 |     if (!this.apiKey) {
  19 |       // CYPRESS_ prefix necessary per https://docs.cypress.io/guides/guides/environment-variables.html#Option-3-CYPRESS
> 20 |       throw new Error('You must set the CYPRESS_MAILOSAUR_API_KEY environment variable to use the Mailosaur plugin.');
     | ^
  21 |     }
  22 | 
  23 |     return {

附加信息: 我通过 CommonJS 语法在 support/index.json 中导入了 cypress-mailosaur

import "./commands";
require("cypress-mailosaur");

发现我使用的是 config.json 作为我的环境变量,而不是 cypress.env.json。这就是为什么我当前的代码不起作用,因为它是一个 config.json 文件并且仅扩展 cypress.json 语法应该与它在 cypress.json 文件中的实现方式相同,其中 MAILOSAUR_API_KEY 应该在 env 对象内。

文档:https://docs.cypress.io/guides/guides/environment-variables#Option-1-configuration-file

{
  "extends": "./cypress.json",
  "env": {
    "MAILOSAUR_API_KEY": "<api-key-here>"
  },
  **other cypress env configs here**
}

要在 Cypress 中拥有单独的环境变量,主要有两个选项可以在文件中配置它们:

1.将它们放在正常的 cypress.json 配置文件中

env 键定义它们,例如:

{
  "baseUrl": "http://localhost:4200",
  "env": {
    "MAILOSAUR_API_KEY": "<api-key-here>"
  }
}

文档:https://docs.cypress.io/guides/guides/environment-variables#Option-1-configuration-file

2。将它们放在专用的 cypress.env.json 文件中

{
  "MAILOSAUR_API_KEY": "<api-key-here>"
}

文档:https://docs.cypress.io/guides/guides/environment-variables#Option-2-cypress-env-json

用法:

两种情况下您的测试中的用法相同:

Cypress.env('MAILOSAUR_API_KEY')

Cypress 环境变量文档: https://docs.cypress.io/guides/guides/environment-variables