0

I have multiple nightwatch tests with setup and teardown in every single test. I am trying to unify it into globalModule.js in before after(path set in globals_path in nightwatch.json).

//globalModule.js    
before:function(test, callback){
// do something with test object 
}

//sampletest.js
before: function(test){
 ..
 },

'testing':function(test){
 ....
 }

My problem is test context is not available in globalsModule.js. How do i get it there? Can someone let me know?

user461112
  • 3,811
  • 3
  • 20
  • 25

1 Answers1

0

Test contex not available now. As said beatfactor, it will available soon. While it not available try use local before first file, but it hack. Also you can export all your file into one object and export it into nightwatch, but then you can use local before just in time. For example:

var tests = {};
var befores = [];
var fs =require('fs');
var requireDir = require('require-dir');
var dirs = fs.readdirSync('build');
//if you have dirs that should exclude
var usefull = dirs.filter(function(item){
    return !(item=='data')
});

usefull.forEach(function(item){
    var dirObj = requireDir('../build/' + item);
    for(key in dirObj){
        if(dirObj.hasOwnProperty(key))
        for(testMethod in dirObj[key])
            if(dirObj[key].hasOwnProperty(testMethod))
                if(testMethod == 'before')
                    befores.push(dirObj[key][testMethod]);
                else
                    tests[testMethod] = dirObj[key][testMethod];
    }
});
tests.before = function(browser){
    //some global before actions here
    //...
    befores.forEach(function(item){
        item.call(tests,browser);
    });
};
module.exports = tests;

For more information https://github.com/beatfactor/nightwatch/issues/388

Andrew Kochnev
  • 956
  • 2
  • 7
  • 10