Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run Protractor e2e tests using different Angular environment variables

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?

like image 321
Serhii Shushliapin Avatar asked Jul 26 '17 14:07

Serhii Shushliapin


People also ask

What is protractor E2E testing?

Protractor is an end-to-end test framework for Angular and AngularJS applications. Protractor runs tests against your application running in a real browser, interacting with it as a user would. Protractor provides a very large APIs that we’ll use for our e2e testing, and these are the main ones:

What are the best tools for E2E testing?

In this tutorial we will be using protractor and jasmine as our tools for e2e testing. Protractor is an end-to-end test framework for Angular and AngularJS applications. Protractor runs tests against your application running in a real browser, interacting with it as a user would.

What is protractor in selenium?

The protractor is an E2E testing framework. It runs tests by sending requests to the Webdriver via Selenium Server. It interacts with the app from the end user’s perspective. Thanks to WebDriver we gain speed and stability in our tests by running them in the real web browsers.

What is protractor and how does it work?

It was developed by a Google team and released in 2013 in open source. The protractor is an E2E testing framework. It runs tests by sending requests to the Webdriver via Selenium Server. It interacts with the app from the end user’s perspective. Thanks to WebDriver we gain speed and stability in our tests by running them in the real web browsers.


2 Answers

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     } 
like image 192
Mubramaj Avatar answered Sep 20 '22 15:09

Mubramaj


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 
like image 23
Jozzhart Avatar answered Sep 21 '22 15:09

Jozzhart