All files / lib/pom element.js

87.02% Statements 114/131
60.61% Branches 20/33
85.19% Functions 23/27
87.7% Lines 107/122

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 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381                          1x 1x 1x   1x 1x 1x   1x   36x 36x 36x   36x 36x 36x 36x               1x 34x 21x   34x                 1x 3x                 1x                   1x                           1x   4x 4x   4x       4x 4x   3x 3x   2x 2x   1x                             1x   2x 2x 2x 2x   2x 2x       2x   2x                   1x   1x                       1x                       1x   4x 4x   4x 2x     4x 4x                         1x   1x 1x 1x   1x 1x       1x                         1x   1x 1x 1x   1x 1x         1x 1x 1x 1x                         1x                     1x   2x 2x   2x       2x 2x   1x 1x                 1x 2x 2x                     1x 1x 1x   1x 1x                       1x 1x 1x   1x 1x                 1x 2x 2x 2x                       1x 2x 2x     2x 2x                       1x 1x 1x   1x 1x                           1x             1x  
"use strict";
/**
 * Creates a new instance of `Element`.
 *
 * `Element` binds DOM element in browser context with virtual element in test.
 *
 * @class
 * @name Element
 * @arg {string} name - Element name.
 * @arg {object} selector - CSS selector of DOM element.
 * @arg {Page} page - Page with element.
 */
 
const _ = require("lodash");
var weak = require("weak");
var U = require("glace-utils");
 
var CONF = require("../config");
var PointerEvents = require("./event");
const utils = require("../utils");
 
var Element = function (name, selector, page) {
 
    this.name = name;
    this.selector = selector;
    this._event = new PointerEvents(selector, page);
 
    this._page = weak(page);
    this._elCmd = utils.getElementCommand(selector);
    this._elsCmd = utils.getElementsCommand(selector);
    this._webdriver = null;
};
/**
 * Helper to get webdriver.
 *
 * @method
 * @return {object} webdriver instance
 */
Element.prototype._getDriver = function () {
    if (!this._webdriver) {
        this._webdriver = this._page.getDriver();
    };
    return this._webdriver;
};
/**
 * Gets webdriver element.
 *
 * @async
 * @method
 * @return {Promise<object>} Webdriver element.
 */
Element.prototype.getElement = function () {
    return this._getDriver().element(this.selector);
};
/**
 * Gets webdriver elements.
 *
 * @async
 * @method
 * @return {Promise<array<object>>} Webdriver elements.
 */
Element.prototype.getElements = function () {
    return this._getDriver().elements(this.selector);
};
/**
 * Gets count of elements in browser.
 *
 * @async
 * @method
 * @return {Promise<integer>} Count of elements
 */
Element.prototype.getCount = async function () {
    const cmd = `return ${this._elsCmd}.length`;
    return this._getDriver().execute(cmd).then(result => result.value);
};
/**
 * Gets text content of DOM element.
 *
 * @async
 * @method
 * @arg {object} [opts] - Options.
 * @arg {boolean} [opts.now=false] - Make it immediately.
 * @arg {?number} [opts.timeout] - Time to wait for element visibility, sec.
 * @return {Promise<string>} Text value or null.
 */
Element.prototype.getText = async function (opts) {
 
    opts = U.defVal(opts, {});
    var now = U.defVal(opts.now, false);
 
    Iif (!now) {
        await this.waitForVisible(opts.timeout);
    };
 
    var value = await this._getDriver().getText(this.selector);
    if (!_.isEmpty(value)) return utils.clearElementText(value);
 
    value = await this._getDriver().getAttribute(this.selector, "value");
    if (!_.isEmpty(value)) return utils.clearElementText(value);
 
    value = await this._getDriver().getAttribute(this.selector, "innerHTML");
    if (!_.isEmpty(value)) return utils.clearElementText(value);
 
    return null;
};
/**
 * Sets text to DOM element.
 *
 * @async
 * @method
 * @arg {string} text - Text value to assign.
 * @arg {object} [opts] - Options.
 * @arg {boolean} [opts.now=false] - Make it immediately.
 * @arg {?number} [opts.timeout] - Time to wait for element visibility, sec.
 * @arg {boolean} [opts.scroll=true] - Scroll to element.
 * @arg {boolean} [opts.enter=false] - Send `Enter` after text.
 * @return {Promise}
 */
Element.prototype.setText = async function (text, opts) {
 
    opts = U.defVal(opts, {});
    var now = U.defVal(opts.now, false);
    var scroll = U.defVal(opts.scroll, true);
    const enter = U.defVal(opts.enter, false);
 
    Eif (scroll) {
        await this.scrollIntoView(opts);
    } else if (!now) {
        await this.waitForVisible(opts.timeout);
    };
    if (enter) text += "\n";
 
    await this._getDriver().setValue(this.selector, text);
};
/**
 * Gets DOM element location with attributes:
 *  `x`, `y`, `midX`, `midY`, `width`, `height`.
 *
 * @async
 * @method
 * @return {Promise<object>} Location of element.
 */
Element.prototype.location = function () {
 
    var cmd = ` \
        var ctrl = ${this._elCmd}; \
        var rect = ctrl.getBoundingClientRect(); \
        var location = {}; \
        location.x = Math.ceil(rect.left); \
        location.y = Math.ceil(rect.top); \
        location.width = Math.ceil(rect.width); \
        location.height = Math.ceil(rect.height); \
        location.midX = Math.ceil(location.x + location.width / 2); \
        location.midY = Math.ceil(location.y + location.height / 2); \
        return location;`;
 
    return this._getDriver().execute(cmd).then(result => result.value);
};
/**
 * Scrolls element into browser viewport.
 *
 * @async
 * @method
 * @arg {object} [opts] - Options.
 * @arg {boolean} [opts.now=false] - Make it immediately.
 * @arg {?number} [opts.timeout] - Time to wait for element visibility, sec.
 * @return {Promise}
 */
Element.prototype.scrollIntoView = async function (opts) {
 
    opts = U.defVal(opts, {});
    var now = U.defVal(opts.now, false);
 
    if (!now) {
        await this.waitForVisible(opts.timeout);
    };
 
    var cmd = "return " + this._elCmd + ".scrollIntoView();";
    await this._getDriver().execute(cmd);
};
/**
 * Clicks element in browser.
 *
 * @async
 * @method
 * @arg {object} [opts] - Options.
 * @arg {boolean} [opts.now=false] - Make it immediately.
 * @arg {?number} [opts.timeout] - Time to wait for element visibility, sec.
 * @arg {boolean} [opts.scroll=true] - Scroll to element.
 * @return {Promise}
 */
Element.prototype.click = async function (opts) {
 
    opts = U.defVal(opts, {});
    var now = U.defVal(opts.now, false);
    var scroll = U.defVal(opts.scroll, true);
 
    Eif (scroll) {
        await this.scrollIntoView(opts);
    } else if (!now) {
        await this.waitForVisible(opts.timeout);
    };
    await this._getDriver().click(this.selector);
};
/**
 * Clicks element in browser via pointer events.
 *
 * @async
 * @method
 * @arg {object} [opts] - Options.
 * @arg {boolean} [opts.now=false] - Make it immediately.
 * @arg {?number} [opts.timeout] - Time to wait for element visibility, sec.
 * @arg {boolean} [opts.scroll=true] - Scroll to element.
 * @return {Promise}
 */
Element.prototype.pClick = async function (opts) {
 
    opts = U.defVal(opts, {});
    var now = U.defVal(opts.now, false);
    var scroll = U.defVal(opts.scroll, true);
 
    Eif (scroll) {
        await this.scrollIntoView(opts);
    } else if (!now) {
        await this.waitForVisible(opts.timeout);
    };
 
    var loc = await this.location();
    await this._event.move(loc.midX, loc.midY);
    await this._event.down(loc.midX, loc.midY);
    await this._event.up(loc.midX, loc.midY);
};
/**
 * Taps element in browser.
 *
 * @async
 * @method
 * @arg {object} [opts] - Options.
 * @arg {boolean} [opts.now=false] - Make it immediately.
 * @arg {?number} [opts.timeout] - Time to wait for element visibility, sec.
 * @arg {boolean} [opts.scroll=true] - Scroll to element.
 * @return {Promise}
 */
Element.prototype.tap = Element.prototype.click;
/**
 * Defines whether element is selected or no.
 *
 * @async
 * @method
 * @arg {object} [opts] - Options.
 * @arg {boolean} [opts.now=false] - Make it immediately.
 * @arg {?number} [opts.timeout] - Time to wait for element visibility, sec.
 * @return {Promise<boolean>} `true` if element is selected, `false` otherwise.
 */
Element.prototype.isSelected = async function (opts) {
 
    opts = U.defVal(opts, {});
    var now = U.defVal(opts.now, false);
 
    Iif (!now) {
        await this.waitForVisible(opts.timeout);
    };
 
    var result = await this._getDriver().isSelected(this.selector);
    if (result) return true;
 
    result = await this._getDriver().getAttribute(this.selector, "class");
    return result.includes("selected");
};
/**
 * Defines whether element is exist or no.
 *
 * @async
 * @method
 * @return {Promise<boolean>} `true` if element is exist, `false` otherwise.
 */
Element.prototype.isExist = function () {
    var cmd = "return !!" + this._elCmd + ";";
    return this._getDriver().execute(cmd).then(result => result.value);
};
/**
 * Waits for element is exist.
 *
 * @async
 * @method
 * @arg {number} [timeout] - Timeout to wait, sec.
 * @return {Promise}
 * @throws {TimeoutError} If element doesn't exist after timeout.
 */
Element.prototype.waitForExist = function (timeout) {
    timeout = U.defVal(timeout, CONF.web.uiTimeout) * 1000;
    var errMsg = `${this.name} (${this.selector}) doesn't exist after ${timeout}s`;
 
    return this._getDriver().waitUntil(async () => {
        return await this.isExist();
    }, timeout, errMsg);
};
/**
 * Waits for element isn't exist.
 *
 * @async
 * @method
 * @arg {number} [timeout] - Timeout to wait, sec.
 * @return {Promise}
 * @throws {TimeoutError} If element is still exist after timeout.
 */
Element.prototype.waitForNonExist = function (timeout) {
    timeout = U.defVal(timeout, CONF.web.uiTimeout) * 1000;
    var errMsg = `${this.name} (${this.selector}) still exists after ${timeout}s`;
 
    return this._getDriver().waitUntil(async () => {
        return !(await this.isExist());
    }, timeout, errMsg);
};
/**
 * Defines whether element is visible or no.
 *
 * @method
 * @return {Promise<boolean>} `true` if element is visible, `false` otherwise.
 */
Element.prototype.isVisible = function () {
    return this.isExist().then(result => {
        Iif (!result) return false;
        return this._getDriver().isVisible(this.selector);
    });
};
/**
 * Waits for element is visible.
 *
 * @async
 * @method
 * @arg {number} [timeout] - Timeout to wait, sec.
 * @return {Promise}
 * @throws {TimeoutError} If element isn't visible after timeout.
 */
Element.prototype.waitForVisible = function (timeout) {
    timeout = U.defVal(timeout, CONF.web.uiTimeout) * 1000;
    var errMsg = `${this.name} (${this.selector}) isn't visible ` +
                 `after ${timeout}s`;
 
    return this._getDriver().waitUntil(async () => {
        return await this.isVisible();
    }, timeout, errMsg);
};
/**
 * Waits for element is invisible.
 *
 * @async
 * @method
 * @arg {number} [timeout] - Timeout to wait, sec.
 * @return {Promise}
 * @throws {TimeoutError} If element is still visible after timeout.
 */
Element.prototype.waitForInvisible = function (timeout) {
    timeout = U.defVal(timeout, CONF.web.uiTimeout) * 1000;
    var errMsg = `${this.name} (${this.selector}) is still visible after ${timeout}s`;
 
    return this._getDriver().waitUntil(async () => {
        return !(await this.isVisible());
    }, timeout, errMsg);
};
/**
 * Clones element with custom properties.
 *
 * @async
 * @method
 * @arg {object} [opts] - Clone options.
 * @arg {string} [opts.name] - Element name.
 * @arg {object} [opts.selector] - CSS selector of DOM element.
 * @arg {Page} [opts.page] - Page with element.
 * @return {Element} Cloned element.
 */
Element.prototype.clone = function (opts = {}) {
    const name = U.defVal(opts.name, this.name);
    const selector = U.defVal(opts.selector, this.selector);
    const page = U.defVal(opts.page, weak.get(this._page));
    return new this.constructor(name, selector, page);
};
 
module.exports = Element;