How to run Protractor e2e tests using different Angular environment variables

protractor environment variables
angular e2e --environment
angular e2e testing
ng e2e pass parameters to protractor
protractor style guide
protractor typescript
protractor browser wait
protractor filereplacements

I use Angular environment variables to configure API endpoints:

.\src\environments:
    environment.ts
    environment.test.ts
    environment.prod.ts

The environtment files contain settings like the following which are different for local dev and CI servers:

export const environment = {
  ...
  apiUrl: "https://api.sample.com"
};

That works fine when I need to build or start the application. I can simply specify the environment parameter:

ng serve --environment=test

... but it appeared that it's impossible to set a specific environment when running e2e Protractor tests. The following command simply ignores the environment (which seems to be expected according to this comment). The default environment is always used:

ng e2e --environment=test    // same as `ng e2e`

Are there any other ways to run the tests using a specific environment?


With the new Angular 6 angular.json config file: I was able to use my file environment.test.ts on my e2e tests. I had to create a new architect on my project, I called it serve-e2e.

    "serve-e2e": {
      "builder": "@angular-devkit/build-angular:dev-server",
      "options": {
        "browserTarget": "q-desktop-v2:build:test"
      }
    }

My build:test config uses the "fileReplacements" configurations :

    "test": {
      "fileReplacements": [
        {
          "replace": "src/environments/environment.ts",
          "with": "src/environments/environment.test.ts"
        }
      ]
    }

Then in my project-e2e I use my customized "serve-e2e" option for the devServerTarget:

      "options": {
        "protractorConfig": "./protractor.conf.js",
        "devServerTarget": "q-desktop-v2:serve-e2e"
      }

This makes it easy to add or ignore specific code if the app is running on test environment :

    if (!environment.test) {
     // do something
    }

E2E testing with env variables with Protractor and Angular 6, Loving how the new angular.json in the Angular 6 CLI just solved the polling issue with protractor/ e2e testing for me. I was attempting to use NgZone. Loving how the new angular.json in the Angular 6 CLI just solved the polling issue with protractor/ e2e testing for me. I was attempting to use NgZone.runOutsideAngular(), which still doesn’t


I was able to successfully use a different environment by adding it to the .angular-cli.json

  "environments": {
    "dev": "environments/environment.ts",
    "test": "environments/environment.test.ts",
    "prod": "environments/environment.prod.ts"
  }

then calling

ng e2e --environment test

How to run Protractor e2e tests with different environment variables , I use Angular environment variables to configure API endpoints: .\src\​environments: environment.ts environment.test.ts environment.prod.ts The environtment  Protractor is a module built especially for E2E testing in angular applications. My objective here is to take you through the journey of executing the test cases in a docker container.


Angular 6 removed support for the --environment option. For build or serve you can just switch to ng build --configuration test or ng serve --configuration test. However, at least in my project, the Angular upgrade actually created a whole other project configuration named <myproject>-e2e in my angular.json file.

Inside, it might look something like this.

"myproject-e2e": {
  "root": "",
  "sourceRoot": "",
  "projectType": "application",
  "architect": {
    "e2e": {
      "builder": "@angular-devkit/build-angular:protractor",
      "options": {
        "protractorConfig": "./protractor.conf.js",
        "devServerTarget": "jglite:serve"
      }
    },
    "lint": {
      "builder": "@angular-devkit/build-angular:tslint",
      "options": {
        "tsConfig": [
          "e2e/tsconfig.e2e.json"
        ],
        "exclude": [
          "**/node_modules/**"
        ]
      }
    }
  }
}

The "devServerTarget": "jglite:serve" line is pointing the configuration at my default serve configuration.

In my case, I want to always run my e2e tests with my dev configuration, so I just changed this line to "devServerTarget": "jglite:serve:dev", and then I could run the environment I needed by just calling ng e2e.

`ng e2e --environment=foo` doesn't work as expected · Issue #4982 , In e2e test I want to have environment variable similar to the one as in Angular app. Looks like e2e test project builder "builder":  You can also use a task manager (e.g. grunt and grunt-protractor-runner) and configure different tasks for running tests in a different environment setting different baseUrls.


Chapter 10: Advanced Protractor topics, Using the Selenium Standalone Server allows you to run tests that control Sauce Labs is another paid service and is also out of the scope of this book, but Listing 10.3 Test using headless Chrome—test_capabilities/e2e/test.e2e-spec.​ts Listing 10.8 Using environment variables—test_environment/protractor.conf.​js. Protractor is an e2e testing framework for AngularJS applications. Protractor is a Node.js program and uses webdriver-manager to interact with the browser and replicate user actions. It uses Jasmine for its testing syntax.


How to import and use Angular Environment variables in `protractor , Is it possible to import and use env variables from the Angular app environments files. I did create a normal JavaScript file containing an object  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.


Protractor End-to-End Testing for AngularJS, Protractor is a great framework for writing E2E tests: Use a Configuration File for Environment Variables. It is also Having this file makes your tests more maintainable and more accessible to other developers on your team. Here protractor is actually telling us how we can implement our tests. You can see that the protractor-cucumber framework works using Regular Expressions to find the right tests to run for each