【问题标题】:Environment Flag for Cucumber Protractor E2E Tests?黄瓜量角器 E2E 测试的环境标志?
【发布时间】:2021-05-18 23:08:57
【问题描述】:

我有一个现有的 Angular v5 应用程序,并且有用于我的环境(如 DEV、测试、生产等)的 environment.json 文件。环境文件存储在如下目录中:src/Environments/DEV/environment.json

这是一个开发environment.json 文件的示例:

{
  "Comment": "Environment=DEV",
  "API_ORIGIN": "https://myapp-dev",
  "ORIGIN": "https://myapp-dev/index.html",
}

src 文件夹中有一个根 environment.json 文件,我的应用程序将从该文件夹中读取。当我想使用特定环境时,我只需将该环境内容复制到根目录并运行应用程序即可。

现在有了 Cucumber 和 Protractor,有没有一种方法可以传递一些命令行参数来根据我的设置指定要使用的 environment.json 文件?我在这些environment.json 文件中有网址,所以我需要一种方法来告诉 Cucumber 和 Protractor 使用哪个环境。如果我必须将所有environment.json 文件复制到适合我的 e2e 文件夹中。以防我需要使用的解决方案取决于我在这里使用的工具是我的tsconfig.e2e.json 文件。如有不妥请告知:

{
  "extends": "../tsconfig.json",
  "compilerOptions": {
    "outDir": "../out-tsc/e2e",
    "baseUrl": "./",
    "module": "commonjs",
    "target": "es5",
    "types": [
      "chai",
      "cucumber",
      "node"
    ]
  }
}

这是protractor.conf.js 文件。如果不正确,请告诉我:

// Protractor configuration file, see link for more information
// https://github.com/angular/protractor/blob/master/lib/config.ts

exports.config = {
  allScriptsTimeout: 11000,
  specs: [
    './e2e/features/**/*.feature'
  ],
  capabilities: {
    'browserName': 'chrome'
  },
  directConnect: true,
  baseUrl: 'http://localhost:4200/',
  framework: 'custom',
  frameworkPath: require.resolve('protractor-cucumber-framework'),
  cucumberOpts: {
    // require step definition files before executing features
    require: ['./e2e/steps/**/*.ts'],
    // <string[]> (expression) only execute the features or scenarios with tags matching the expression
    tags: [],
    // <string[]> ("extension:module") require files with the given EXTENSION after requiring MODULE (repeatable)
    compiler: []
  },

  // Enable TypeScript for the tests
  onPrepare() {
    require('ts-node').register({
      project: 'e2e/tsconfig.e2e.json'
    });
  }
};

如果这很重要,我也会使用npm。我正在使用 angular 提供的 ng e2e 命令运行这些测试。

【问题讨论】:

    标签: json protractor cucumber


    【解决方案1】:

    当然,两种方式:

    1. 将参数传递给量角器protractor conf.js --params.env="dev",然后在规范中将其称为browser.params.env。缺点是,它仅在解析配置并启动浏览器时可用,因此您可以在配置本身中真正使用它

    2. 使用环境变量 MY_VAR=Dev protractor config.js 运行该进程,运行 process.env.MY_VAR 即可在任何地方使用它

    供参考

    https://stackoverflow.com/a/58547994/9150146

    https://stackoverflow.com/a/66111592/9150146


    附言

    如何实现它取决于您,但这种方法是最灵活的

    conf.js

    let environment = require('src/Environments/' + process.env.TEST_ENV + '/environment.json');
    
    module.exports = {
      baseUrl: environment.API_ORIGIN
    }
    

    然后像这样启动你的量角器

    TEST_ENV=DEV protractor config.js
    

    【讨论】:

    • 嗨。我看到你给了我一个量角器命令。这适用于黄瓜和量角器的组合吗?
    • 我也是这个设置的新手。所以我意识到我的配置文件没有为黄瓜和量角器正确设置并更新它们。
    • 没关系。第一种方式是纯粹的量角器功能,无论框架如何。第二个实际上是节点特定的逻辑,它甚至与量角器无关,更不用说黄瓜了
    • 好的。现在我的应用程序中每个环境都有一个environment.json 文件。我如何使用该环境变量来读取正确的environment.json 文件?文件的路径如下:src/Environments/DEV/environment.jsonsrc/Environments/TEST/environment.json 等。它们都有相同的键。唯一不同的是价值观。我有问题中文件内容的示例。
    • 取决于你在哪里使用它。配置还是 spes?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多