Load additional CONFIG file with values

12.2k views Asked by At

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?

2

There are 2 answers

5
alecxe On BEST ANSWER

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.

1
JSNinja On

You could simple use configFile as a global variable and use it in each of your tests.

describe('Description', function(){
    var configFile = require('./config.json');
    it('Test1', function(){
        //Consume configFile here
    });
    it('Test2', function(){
        //Consume configFile here
    });
});

I hope this helps.