All files / lib/steps page.js

42.11% Statements 24/57
30% Branches 3/10
42.86% Functions 3/7
41.07% Lines 23/56

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 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174              1x   1x 1x 1x   1x                       1x 1x         1x 1x   1x 1x   1x                                                                                   2x 2x     2x   2x 2x 2x   2x 2x                                                                                                                                                           7x 7x     1x  
"use strict";
/**
 * Steps for page.
 *
 * @mixin PageSteps
 */
 
var url = require("url");
 
var expect = require("chai").expect;
var U = require("glace-utils");
var LOG = U.logger;
 
var PageSteps = {
 
    registerPages: function () {
        /**
         * Helper to register pages.
         *
         * @memberOf PageSteps
         * @method registerPages
         * @instance
         * @arg {...Page} pages - Sequence of pages.
         */
 
        for (var page of arguments) {
            Iif (this._pages()[page.name]) {
                LOG.debug(`Page ${page.name} is registered already`);
                continue;
            };
 
            allure.step(`Register web page ${page.name}`);
            LOG.info(`Register web page ${page.name}`);
 
            page.setDriver(this.webdriver);
            this._pages()[page.name] = page;
 
            allure.pass();
        };
    },
 
    removePages: function () {
        /**
         * Helper to remove pages.
         *
         * @memberOf PageSteps
         * @method removePages
         * @instance
         * @arg {...string} pageNames - Sequence of page names.
         */
 
        for (var pageName of arguments) {
            if (!this._pages()[pageName]) {
                LOG.debug(`Page ${pageName} isn't registered`);
                continue;
            };
 
            allure.step(`Remove page ${pageName}`);
            LOG.info(`Remove page ${pageName}`);
 
            this._pages()[pageName].setDriver(null);
            delete this._pages()[pageName];
 
            allure.pass();
        };
    },
 
    openPage: async function (pageName) {
        /**
         * Step to open page.
         *
         * @async
         * @memberOf PageSteps
         * @method openPage
         * @instance
         * @arg {string} pageName - Name of page to open.
         * @return {Promise}
         */
 
        allure.step(`Open web page ${pageName}`);
        expect(this.webUrl,
            "Web URL isn't defined").to.exist;
 
        LOG.info(`Openning page '${pageName}' in browser...`);
 
        var page = this._pages()[pageName];
        var webUrl = url.resolve(this.webUrl, page.url);
        await this.openUrl(webUrl);
 
        LOG.info("Page is opened");
        allure.pass();
    },
 
    getCurrentPage: async function () {
        /**
         * Step to get current page.
         *
         * @async
         * @memberOf PageSteps
         * @method getCurrentPage
         * @instance
         * @return {Promise<Page>} - Page which corresponds to current browser URL.
         * @throws {Error} - If no one of registered pages corresponds to
         *  current browser URL.
         */
 
        allure.step("Get current web page");
        LOG.info("Getting current web page...");
 
        /* sort pages by descending page url length */
        var pages = Object.values(this._pages()).sort(
            (a, b) => a.url.length < b.url.length);
 
        var curUrl = await this.webdriver.getUrl();
        var parsedUrl = url.parse(curUrl);
 
        for (var page of pages) {
            if (parsedUrl.pathname.startsWith(page.url)) {
                LOG.info("Current web page is found");
                allure.pass();
                return page;
            }
        };
        throw new Error(
            `No one of registered pages corresponds URL '${curUrl}'`);
    },
 
    getElement: async function (name, opts) {
        /**
         * Step to get UI element.
         *
         * @async
         * @memberOf PageSteps
         * @method getElement
         * @instance
         * @arg {string} name - UI element name.
         * @arg {object} [opts] - Step options.
         * @arg {boolean} [opts.check=true] - Flag to check step result.
         * @return {Promise<object>} - UI element.
         * @throws {AssertionError} - If element wasn't found.
         */
 
        opts = U.defVal(opts, {});
        var check = U.defVal(opts.check, true);
 
        allure.step(`Get web element with name ${name}`);
        LOG.info(`Getting web element with name ${name}...`);
 
        var element = (await this.getCurrentPage())[name];
 
        if (check) {
            expect(element,
                `Undefined DOM element ${name}`).to.exist;
        };
 
        LOG.info("Web element is found");
        allure.pass();
 
        return element;
    },
    /**
     * Helper to initialize pages storage.
     *
     * @method
     * @instance
     * @protected
     */
    _pages: function () {
        if (!this.__pages) this.__pages = {};
        return this.__pages;
    },
};
module.exports = PageSteps;