0

I have written below .js file to call below defined function. objectRepositoryLoader.readObjectRepository() returns me a hashmap from where i have to use values in enterUserName(), enterPassword(), clickLoginButton() functions.

var path = require('path');
var elementRepoMap = {}

var LandingPage = function(){
    var fileName = path.basename(module.filename, path.extname(module.filename))

    objectRepositoryLoader.readObjectRepository(fileName+'.xml' , function(elementRepo){
        console.log(elementRepo) //values are being printed here
        this.elementRepoMap = elementRepo
    }); 

    this.enterUserName = function(value){
        console.log(elementRepoMap) //values are not being printed here
        //Some Code     
    };

    this.enterPassword = function(value){
        //Some Code
    };

    this.clickLoginButton = function(){
        //Some Code
    };  
};
module.exports = new LandingPage();

The objectRepositoryLoader.readObjectRepository() function defined in another file is as below:

var ObjectRepositoryLoader = function() {

    this.readObjectRepository = function(fileName, callback) {        
        var filePath = './elementRepository/'+fileName;
        this.loadedMap = this.objectRepoLoader(filePath, function(loadedMap){
            return callback(loadedMap);
        });
    }

    this.objectRepoLoader = function(filePath, callback){
        if (filePath.includes(".xml")) {
            this.xmlObjectRepositoryLoader(filePath, function(loadedMap){
                return callback(loadedMap);                
        });
    }

    this.xmlObjectRepositoryLoader = function (xmlPath, callback){
        var innerMap = {};
        var elementName;
        fs.readFile(xmlPath, "utf-8",function(err, data) {
            if(err){
                console.log('File not found!!')
            }
            else{
                var doc = domparser.parseFromString(data,"text/xml");
                var elements = doc.getElementsByTagName("A1");
                for(var i =0 ; i< elements.length;i++){
                    var elm = elements[i];
                    elementName = elm.getAttribute("name");
                    var params = elm.getElementsByTagName("AS");
                    innerMap = {};
                    for(var j =0 ; j< params.length;j++){
                        var param = params[j];
                        var locatorType = param.getAttribute("type");
                        var locatorValue = param.getAttribute("value");
                        innerMap[locatorType] = locatorValue;
                    }
                    loadedMap[elementName] = innerMap;
                    innerMap={};
                };
            }
            return callback(loadedMap);         
        });        
    };

How can I call enterUserName(), enterPassword(), clickLoginButton() function from spec.js file and is there any way I can avoid using callback and use async.js and call enterUserName(), enterPassword(), clickLoginButton() from spec.js file ?

EDIT I have modified my file like below:

this.xmlObjectRepositoryLoader = function (xmlPath){
        var innerMap = {};
        var elementName;
        var filePath = xmlPath+'.xml'
        var self = this
        return new Promise(
            function(resolve, reject){
                console.log("In xmlObjectRepositoryLoader : "+filePath)
                self.readFilePromisified(filePath)
                .then(text => {
                    var doc = domparser.parseFromString(text,"text/xml");
                    var elements = doc.getElementsByTagName("Element");
                    for(var i =0 ; i< elements.length;i++){
                        var elm = elements[i];
                        elementName = elm.getAttribute("name");
                        var params = elm.getElementsByTagName("param");
                        innerMap = {};
                        for(var j =0 ; j< params.length;j++){
                            var param = params[j];
                            var locatorType = param.getAttribute("type");
                            var locatorValue = param.getAttribute("value");
                            innerMap[locatorType] = locatorValue;
                        }
                        map[elementName] = innerMap;
                        innerMap={};
                    }
                    console.log(map) // prints the map
                    resolve(text)
                })
                .catch(error => {
                    reject(error)
                });
            });
        }

this.readFilePromisified = function(filename) {
        console.log("In readFilePromisified : "+filename)
        return new Promise(
            function (resolve, reject) {
                fs.readFile(filename, { encoding: 'utf8' },
                (error, data) => {
                    if (error) {
                        reject(error);
                    } else {
                        resolve(data);
                    }
                })
            })
        }

I am calling above function from another file as below:

objectRepositoryLoader.readObjectRepository(fileName)
    .then(text => {
        console.log(text);
    })
    .catch(error => {
        console.log(error);
    });

But it gives me error as

     .then(text => {   ^

TypeError: Cannot read property 'then' of undefined

In this case how can I use promise to call another promise function and then use the returned value in one more promise function and return calculated value to calling function where I can use the value in other functions. I sound a bit confused. Please help

Abhinav
  • 1,037
  • 6
  • 20
  • 43
  • You can call functions from spec file with rewire https://github.com/jhnns/rewire. What exactly the problem with the callback? – Amiram Korach Sep 12 '16 at 06:08
  • I need to retain value so that I can use the value read from file using callback throughout the js file. I read that we should avoid callback and go for async. For my requirement how could I do that ? – Abhinav Sep 12 '16 at 06:12
  • Async is good but promise is better than both. I think you don't get the value in enterUserName because the file is being read async. Use file.readFileSync and you won't need a callback. – Amiram Korach Sep 12 '16 at 06:17
  • I will check promise and file.readFileSync. – Abhinav Sep 12 '16 at 06:26
  • BTW, using the sync way is not good if you're making it as part of a web request. It all depends what is the context here. – Amiram Korach Sep 12 '16 at 06:29
  • I am using protractor to start with web automation testing for which I need many modules in different js files – Abhinav Sep 12 '16 at 06:36
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/123131/discussion-between-abhinav-and-amiram-korach). – Abhinav Sep 12 '16 at 06:37
  • @AmiramKorach can you please look at the update. I have tried using promise and right now I am stuck – Abhinav Sep 13 '16 at 07:41
  • readObjectRepository should return a promise. Does it? BTW, you can use the promisify package and not do it yourself – Amiram Korach Sep 13 '16 at 10:24
  • It was not returning promise earlier. I changed the code to :`this.readObjectRepository = function(fileName) { var filePath = '../'+fileName; var self = this return new Promise( function(resolve, reject){ self.xmlObjectRepositoryLoader(filePath, (error, data) => { if (error) { reject(error); } else { resolve(data); } }) }) }` Still same error :( – Abhinav Sep 13 '16 at 10:31
  • @AmiramKorach : this issue is resolved. Can you post answer so that I can mark it as answer – Abhinav Sep 13 '16 at 12:00

1 Answers1

0

You can use async.waterfall and async.parallel to perform this task

see the reference

I just tried your code to make it working, I explained the way of implementation in comment.

async.waterfall([
function(next){
    objectRepositoryLoader.readObjectRepository(fileName+'.xml' ,next)//pass this next as parameter in this function defination and after manipulation return result with callback like this(null,result)

}
  ],function(err,result){

    if(!err){
      //Do wahtever you want with result

async.parallel([
    function(callback){
          this.enterUserName = function(value){
        console.log(elementRepoMap)
        //Some Code     
    };
     },
    function(callback){ 
          this.enterPassword = function(value){
        //Some Code
    };
    },
        function(callback){ 

    this.clickLoginButton = function(){
        //Some Code
    };
        }
], function(err, results) {
    // optional callback
};
    }
  })
abdulbarik
  • 6,101
  • 5
  • 38
  • 59