Search code examples
javascriptseleniumtestingprotractorend-to-end

Load additional CONFIG file with values


I am using lot's of values for my test like username and password.

For this purpose I created a config file where I store just this data in JSON, it looks like:

{
    "login": "test",
    "password": "pass",
    "number": "1234",
}

It works for me if I request it at start of each test file (one file for login, another for something else)

How can I load this config file for once and not in every single file. Example, how I do it now:

var configFile = require('./config.json');

Can somebody help me to setup this?


Solution

  • To follow the "DRY" principle, use your protractor config and globally available browser object:

    • in your protractor config, "import" your configuration file and set it as a params value:

      var config = require("./config.js");
      exports.config = {
          // ...
      
          params: config,
      
          // ...
      }
      
    • in your tests, simply use browser.params, e.g.:

      describe('Logging in', function(){
           it('should log in', function(){
               var login = element(by.id("login"));
               login.sendKeys(browser.params.login);
      
               var password = element(by.id("password"));
               login.sendKeys(browser.params.password);
      
               element(by.id("submit")).click();
           });
       });
      

    In other words, this is "Import once - use everywhere" approach.