-1

I have an existing Angular v5 app and have environment.json files for my environments (like DEV, Test, Production, etc.). The environments files are stored in the directory like so: src/Environments/DEV/environment.json.

Here is an example of a dev environment.json file:

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

There is a root environment.json file in src folder that my app reads from. When I want to use a specific environment I just copy that environment content into the root and run the app.

Now with Cucumber and Protractor is there a way I can pass some command line argument to specify which environment.json file to use based on my setup? I have urls in these environment.json files so I need a way to tell Cucumber and Protractor which environment to use. If I have to copy all of the environment.json files into the e2e folder that is fine with me. Just in case the solution I need to use depends on the tools I am using here is my tsconfig.e2e.json file. Please let me know if it is incorrect:

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

Here is the protractor.conf.js file. Let me know if it is incorrect as well please:

// 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'
    });
  }
};

I'm also using npm if that matters. I'm running these tests with ng e2e command provided by angular.

Bmoe
  • 888
  • 1
  • 15
  • 37

1 Answers1

0

sure, 2 ways:

  1. pass a parameter to protractor protractor conf.js --params.env="dev" and then refer to it as browser.params.env in specs. Downside is, it will only be available when the config is parsed and the browser is started, so you can really use that in the config itself

  2. Run the process with an env variable MY_VAR=Dev protractor config.js and it will be available anywhere by running process.env.MY_VAR

For reference

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

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


P.S.

how you implement it is up to you, but this approach is the most flexible

conf.js

let environment = require('src/Environments/' + process.env.TEST_ENV + '/environment.json');

module.exports = {
  baseUrl: environment.API_ORIGIN
}

and start your protractor like so

TEST_ENV=DEV protractor config.js
Sergey Pleshakov
  • 7,964
  • 2
  • 17
  • 40
  • Hi. I see you gave me a protractor command. Will this work for combination of Cucumber and Protractor? – Bmoe May 18 '21 at 14:10
  • Also I'm new with this setup. So I realized my config files aren't setup correctly for cucumber and protractor and updated them. – Bmoe May 18 '21 at 14:12
  • doesn't matter. 1st way is pure protractor feature regardless of framework. 2nd is actually node specific logic and it not even related to protractor let alone cucumber – Sergey Pleshakov May 18 '21 at 14:17
  • Ok. Now i have one `environment.json` file per environment in my app. How could I use that env variable to read the correct `environment.json` file? The path for the files is as so: `src/Environments/DEV/environment.json`, `src/Environments/TEST/environment.json`, etc. They all have the same keys. Only difference is values. I have example of the file content in the question. – Bmoe May 18 '21 at 14:25
  • depends where you use it. Config or spes? – Sergey Pleshakov May 18 '21 at 14:42
  • I haven't figured it out in the e2e test side yet but in my app we're just making an "api request" to the root `environment.json` file when the app is loaded. On deployment we are updating that file with the right environment. We mapped the json file to a data model and are just storing the contents in a shared service used throughout the application. If there is a better way to do this pertaining to the e2e tests I'm all ears. – Bmoe May 18 '21 at 15:00
  • I don't have a `conf.js` file. I need to put it in `protractor.conf.js` instead? – Bmoe May 18 '21 at 15:11
  • of course, whatever you call it, as long as it is a part of your executable configuration – Sergey Pleshakov May 18 '21 at 15:15
  • Sorry but how do I run protractor from command line? Using Windows 10. Running `TEST_ENV=DEV node_modules/protractor/bin/protractor protractor.conf.js`. I am getting `'TEST_ENV' is not recognized as an internal or external command, operable program or batch file. ` I can run the protractor using `ng e2e` command but the `require` function is failing because the json file cannot be found. – Bmoe May 20 '21 at 02:18
  • yeah, it's different for windows... check out this answer https://stackoverflow.com/questions/9249830/how-can-i-set-node-env-production-on-windows – Sergey Pleshakov May 20 '21 at 12:39
  • Hi. Sorry took so long to get back to you. But getting an error when trying to require the json file: ` E/configParser - Error: Cannot find module `'src/Environments/TEST/environment.json'`. The file is definitely there. – Bmoe Jun 11 '21 at 02:32
  • this is another issue. I solved what was asked – Sergey Pleshakov Jun 11 '21 at 14:15
  • The code you posted isn't working though. I get the error I posted in my comment. – Bmoe Jun 11 '21 at 17:49