Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular2 - Executing e2e tests in different environments

I am new to Protractor. I have developed few tests cases to accomplish the end to end testing. These test cases works fine in all 3 different environments that we have : dev, testing and production.

But I need to change Angular application URL in test cases with respect the environment in which testing needs to be done (as the URL differs from environment to environment).

I am using Angular CLI command ng e2e to run the test. Now my questions are

1) Is it possible to pass params ng e2e, so that based on params, URL can be set dynamically inside test cases ?

2) Is it necessary to install protractor globally, Since Angular CLI creates the angular application with protractor under node_modules?

In my project set up, Protractor version is 5.1.2 and Angular CLI version is 1.2.0.

Below is Protract configuration

exports.config = {
  allScriptsTimeout: 11000,
  specs: [
     './e2e/**/*.e2e-spec.ts'
  ],
  capabilities: {
    'browserName': 'chrome'
  },
  directConnect: true,
  baseUrl: 'http://localhost:4200/',
  framework: 'jasmine',
  jasmineNodeOpts: {
    showColors: true,
    defaultTimeoutInterval: 30000,
    print: function() {}
  },
  useAllAngular2AppRoots: true,
  beforeLaunch: function() {
    require('ts-node').register({
        project: 'e2e'
    });
  },
  onPrepare: function() {
    jasmine.getEnv().addReporter(new SpecReporter());
  }
};

I tried couple of things like adding params:[env:dev] and passed the parameter in command line as ng e2e params.env=test, But I always get the default values and not the values passed from command line.

I also tried @cnishina answer, but console.log of process.env does not have needed details.

like image 265
Sameer K Avatar asked Sep 01 '17 10:09

Sameer K


2 Answers

Switching between environments using environment variables

My recommendation is to try to use environment variables and switch between environments in the Protractor configuration file. You could export TEST_ENV=dev and then have conditionals for the browser.baseUrl

Protractor config file

var baseUrl = '';
if (process.env.TEST_ENV === 'dev') {
  baseUrl = 'http://devurl';
} else if (process.env.TEST_ENV === 'stage') {
  baseUrl = 'http://stageurl';
} else {
  baseUrl = 'http://produrl';
}

exports.config = {
  baseUrl: baseUrl
}

For a better concrete example of a configuration file using process.env, see the Testing Angular Applications, chapter 11 GitHub repo.

You could also change baseUrl parameter inline; however, if you are changing multiple variables based on the testing environment, I would suggest using environment variables:

ng e2e {protractor config file} --baseUrl='http://devurl'

Protractor global install?

Protractor does not need to be installed globally. The recommendation is to use the version installed locally from your node_modules directory. This will provide other developers repeatable steps using the same Protractor dependencies to run the e2e tests. Alternatively, if you are using the globally installed version, you will now have to instruct other developers to use a specific Protractor version.

like image 88
cnishina Avatar answered Nov 03 '22 01:11

cnishina


I believe that you can create an environment.test.ts that contains the baseUrl that you want for the test environement and then you can run the test from the command-line like so:

ng e2e --environment test
like image 27
Ryan Spears Avatar answered Nov 03 '22 03:11

Ryan Spears