All files / lib utils.js

54.72% Statements 29/53
53.33% Branches 16/30
75% Functions 9/12
54% Lines 27/50

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123    1x 1x   1x     3x         85x     85x         36x     36x         1x                                 1x 1x       3x               3x 3x         3x                 3x     3x 3x         3x 3x     3x                       3x 6x 3x             6x 3x                       1x  
"use strict";
 
const _ = require("lodash");
const findStrategy = require("webdriverio/build/lib/helpers/findElementStrategy");
 
module.exports = {
 
    clearElementText: value => {
        Eif (!_.isArray(value)) return value.trim();
        return value.map(v => v.trim());
    },
 
    getElementCommand: selector => {
        Iif (findStrategy(selector).using === "xpath") {
            return `${xpathFunc(selector)}[0]`;
        } else {
            return `document.querySelector("${selector}")`;
        }
    },
 
    getElementsCommand: selector => {
        Iif (findStrategy(selector).using === "xpath") {
            return xpathFunc(selector);
        } else {
            return `document.querySelectorAll("${selector}")`;
        }
    },
};
 
const xpathFunc = selector => {
    return ` \
        ((() => {
            var xpathObj = document.evaluate("${selector}", \
                document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null); \
            var result = []; \
            var count = xpathObj.snapshotLength; \
            for (var i = 0; i < count; i++) { \
                result.push(xpathObj.snapshotItem(i)); \
            } \
            return result; \
        })())`;
};
 
class OrderedCollection {
 
    constructor() {
        this.objs = {};
        this.keys = [];
    }
 
    pop(key) {
        Iif (key) {
            if (this.keys.includes(key)) {
                this.keys.splice(this.keys.indexOf(key), 1);
                return this.objs[key];
            } else {
                throw Error(`Key "${key}" isn't added yet`);
            }
        } else {
            key = this.keys.pop();
            return this.objs[key];
        }
    }
 
    push(obj, key) {
        Iif (!key) {
            for (const [k, o] of Object.entries(this.objs)) {
                if (o === obj) {
                    this.keys.push(k);
                    return;
                }
            }
            throw Error("Pushed object isn't assigned with any key");
        }
        Iif (key && this.keys.includes(key)) {
            throw Error(`Key "${key}" is used already`);
        } else {
            this.keys.push(key);
            this.objs[key] = obj;
        }
    }
 
    get(key) {
        Eif (key) {
            Iif (this.keys.includes(key)) {
                return this.objs[key];
            } else {
                return null;
            }
        } else {
            if (this.keys.length) {
                return this.objs[this.keys[0]];
            } else {
                return null;
            }
        }
    }
 
    getKey(obj) {
        for (const [k, o] of Object.entries(this.objs)) {
            if (obj === o && this.keys.includes(k)) {
                return k;
            }
        }
        return null;
    }
 
    top() {
        if (!this.keys.length) return null;
        return this.objs[this.keys[this.keys.length - 1]];
    }
 
    all() {
        const result = {};
        for (const key of this.keys) {
            result[key] = this.objs[key];
        }
        return result;
    }
};
 
module.exports.OrderedCollection = OrderedCollection;