All files / lib/pom index.js

100% Statements 36/36
100% Branches 12/12
100% Functions 6/6
100% Lines 34/34

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                      1x   1x   1x   22x 22x   22x   22x               1x 3x                 1x 4x     3x               1x 26x 14x 14x                     1x   21x 3x 18x   5x 1x     4x 4x 4x 4x     13x 12x 1x     11x   1x                   1x 2x     1x  
"use strict";
/**
 * Page of browser.
 *
 * @class
 * @name Page
 * @arg {string} name - Name of page.
 * @arg {string} pageUrl - URL of page.
 * @arg {object} elements - Element names and selectors dictionary.
 */
 
var _ = require("lodash");
 
var Element = require("./element");
 
var Page = function (name, pageUrl, elements) {
 
    this.name = name;
    this.url = pageUrl;
 
    this._webdriver = null;
 
    this.addElements(elements || {});
};
/**
 * Sets webdriver for page.
 *
 * @method
 * @arg {object} webdriver - Webdriver instance.
 */
Page.prototype.setDriver = function (webdriver) {
    this._webdriver = webdriver;
};
/**
 * Gets webdriver.
 *
 * @method
 * @return {object} - Webdriver instance.
 * @throws {AssertionError} - If webdriver isn't set yet.
 */
Page.prototype.getDriver = function () {
    expect(this._webdriver,
        "Webdriver isn't set")
        .to.exist;
    return this._webdriver;
};
/**
 * Adds elements to page.
 *
 * @method
 * @arg {object} elements - Element names and selectors dictionary.
 */
Page.prototype.addElements = function (elements) {
    for (var name in elements) {
        var selector = elements[name];
        this._addElement(name, selector);
    };
};
/**
 * Adds element.
 *
 * @protected
 * @method
 * @arg {string} name - Element name.
 * @arg {string} selector - Element selector.
 */
Page.prototype._addElement = function (name, selector) {
 
    if (_.isFunction(selector)) {
        this._addElement(name, selector());
    } else if (_.isArray(selector)) {
 
        if (selector.length < 1) {
            throw new Error("selectors array should have at least 1 item");
        };
 
        for (var i = 0; i < selector.length; i++) {
            var n = name + "_" + (i + 1);
            var s = selector[i];
            this._addElement(n, s);
        };
 
    } else if (_.isString(selector)) {
        if (this[name]) {
            throw new Error(
                `Page '${this.name}' already contains property '${name}'`);
        };
        this[name] = new Element(name, selector, this);
    } else {
        throw new Error("selector should be string or array or function " +
                        `but not ${typeof selector}`);
    };
};
/**
 * Removes elements from page.
 *
 * @method
 * @arg {...string} elementNames - Sequence of element names.
 */
Page.prototype.removeElements = function () {
    for (var name of arguments) delete this[name];
};
 
module.exports = Page;