all files / jWebDriver/lib/ mixpromise.js

100% Statements 45/45
85% Branches 17/20
100% Functions 12/12
100% Lines 45/45
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80                  41× 41×   34× 16× 16× 16× 16× 16× 16×                                 76× 76×   57×                
/*jshint unused:false*/
'use strict';
function mixPromise(JWebDriver, Browser, Elements){
    var JWebDriverPrototype = JWebDriver.prototype;
    // copy Elements method
    let arrBlackList = ['constructor', 'init', 'log', 'exec', 'sleep', 'toJSON', 'find', 'equal'];
    let objBlackList = {};
    arrBlackList.forEach(function(name){
        objBlackList[name] = true;
    });
    let objMethodMap = {
        'sendKeys': 'sendElementKeys',
        'scrollTo': 'scrollElementTo',
        'sendActions': 'sendElementActions'
    };
    let protoNames = Object.getOwnPropertyNames(Elements.prototype);
    // console.log('============= Elements ==============');
    protoNames.slice(1).forEach(function(name){
        let newName = name !== 'constructor' && objMethodMap[name] || name;
        if(!JWebDriverPrototype[newName] && !objBlackList[newName]){
            // console.log(name);
            JWebDriver.addMethod(newName, function(done){
                var args = Array.prototype.slice.call(arguments, 0);
                var callback = args.pop();
                var lastElements = this.lastElements;
                Eif(lastElements){
                    lastElements[name].apply(lastElements, args).then(function(ret){
                        callback(null, ret);
                    }, callback);
                }
            });
        }
    });
 
    // copy Browser method
    ['find', 'findVisible', 'wait'].forEach(function(name){
        JWebDriver.addMethod(name, function(done){
            var self = this;
            var args = Array.prototype.slice.call(arguments, 0);
            var callback = args.pop();
            var lastBrowser = self.lastBrowser;
            Eif(lastBrowser){
                lastBrowser[name].apply(lastBrowser, args).then(function(ret){
                    self.lastElements = ret;
                    callback(null, ret);
                }, callback);
            }
        });
    });
    arrBlackList = ['constructor', 'MouseButtons', 'Keys', 'init', 'log', 'exec', 'isStickyKey'];
    objBlackList = {};
    arrBlackList.forEach(function(name){
        objBlackList[name] = true;
    });
    objMethodMap = {
        'size': 'windowSize',
        'position': 'windowPosition'
    };
    // console.log('============= Browser ==============');
    protoNames = Object.getOwnPropertyNames(Browser.prototype);
    protoNames.forEach(function(name){
        let newName = name !== 'constructor' && objMethodMap[name] || name;
        if(!JWebDriverPrototype[newName] && !objBlackList[newName]){
            // console.log(name);
            JWebDriver.addMethod(newName, function(done){
                var args = Array.prototype.slice.call(arguments, 0);
                var callback = args.pop();
                var lastBrowser = this.lastBrowser;
                Eif(lastBrowser){
                    lastBrowser[name].apply(lastBrowser, args).then(function(ret){
                        callback(null, ret);
                    }, callback);
                }
            });
        }
    });
}
 
module.exports = mixPromise;