Saturday 2 January 2016

Test Data - Handling multiple test environments using configuration and conventions

Create a config,js for each environment and include a global parameter identifying the environment.


exports.config = {
   
seleniumAddress: 'http://127.0.0.1:4444/wd/hub',

   
baseUrl: '',

   
params: {
       
environment: 'sys'
    },


...

}

When running protractor you will include this config.js in the runtime parameters. So you can call protractor with different config.js files for each environment while running the same tests.

I use a function in a library which will look for a data file named the same as the spec file with -data appended before the .js extension. It also uses the above parameter to identify the correct data file for the environment I'm running my tests in.

If I have the spec file I will have a data file to go with it (convention).


  • smoketest-spec.js
  • smoketest-sys-data.js

Datafile as a Module:

Function to locate Data File:

function assignDataFile (inFile) {
    var temp = inFile.split'.js' );
    var outFile temp[0] + "-" browser.params.environment "-data.js";
    console.log("Using the data file: " + outFile );
    var DataFile = require ( outFile );
    return (new DataFile ());
}

Datafile:

module.exports = function() {
   
this.shoppingItem1 = "Toothbrush"
;
}


Datafile as JSON:

Function to locate Data File:

function assignDataFile (inFile) {
    var temp = inFile.split'.js' );
    var outFile temp[0] + "-" browser.params.environment "-data.js";
    console.log("Using the data file: " + outFile );
    var DataFile = require ( outFile );
    return (DataFile);
}

Datafile:

{
   
"
shoppingItem1": "Toothbrush"
}

Using the Data:

I then just call the above function at the start of each spec.

var testData actions.assignDataFile ( __filename );

So I am then able to refer to my test data within the spec like this:

element(by.cssContainingText('option', testData.shoppingItem1);

The above example selects 'Toothbrush' from a drop down and assumes there is only one drop down on the page and that the strings match exactly.

Commentary:

I prefer to use a module for the data file. Not sure why, I think I just picked that first as I had no previous experience with JSON files. I like the way I can require other files inside the module and create tiered data as well as reuse data across multiple environments and manage that in one place. Also being able to use date functions, moment.js etc for test data.

module.exports = function() {
    //Generic test data (ie today, tomorrow and yesterday)
    var DataHelper = require ( './dataHelper.js' );
    var dataHelper = new DataHelper () ;

    this.cartName = "Automated test -> " + dataHelper.today; 
    this.shoppingItem1 "Toothbrush";
}



No comments:

Post a Comment