Skip to content

Commit

Permalink
Expand web element functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
schipiga committed Jul 24, 2018
1 parent 0f23c4b commit 8444e50
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 6 deletions.
35 changes: 30 additions & 5 deletions lib/pom/element.js
Expand Up @@ -11,11 +11,13 @@
* @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) {

Expand All @@ -24,7 +26,8 @@ var Element = function (name, selector, page) {
this._event = new PointerEvents(selector, page);

this._page = weak(page);
this._elCmd = `document.querySelector("${this.selector}")`;
this._elCmd = utils.getElementCommand(selector);
this._elsCmd = utils.getElementsCommand(selector);
this._webdriver = null;
};
/**
Expand All @@ -42,12 +45,34 @@ Element.prototype._getDriver = function () {
/**
* Gets webdriver element.
*
* @async
* @method
* @return {object} Webdriver element.
* @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.
*
Expand All @@ -68,13 +93,13 @@ Element.prototype.getText = async function (opts) {
};

var value = await this._getDriver().getText(this.selector);
if (value) return value.trim();
if (!_.isEmpty(value)) return utils.clearElementText(value);

value = await this._getDriver().getAttribute(this.selector, "value");
if (value) return value.trim();
if (!_.isEmpty(value)) return utils.clearElementText(value);

value = await this._getDriver().getAttribute(this.selector, "innerHTML");
if (value) return value.trim();
if (!_.isEmpty(value)) return utils.clearElementText(value);

return null;
};
Expand Down
4 changes: 3 additions & 1 deletion lib/pom/event.js
Expand Up @@ -11,10 +11,12 @@

var weak = require("weak");

const utils = require("../utils");

var PointerEvents = function (selector, page) {
this._selector = selector;
this._page = weak(page);
this._elCmd = `document.querySelector("${this._selector}")`;
this._elCmd = utils.getElementCommand(selector);
this._webdriver = null;
};
/**
Expand Down
42 changes: 42 additions & 0 deletions lib/utils.js
@@ -0,0 +1,42 @@
"use strict";

const _ = require("lodash");
const findStrategy = require("webdriverio/build/lib/helpers/findElementStrategy");

module.exports = {

clearElementText: value => {
if (!_.isArray(value)) return value.trim();
return value.map(v => v.trim());
},

getElementCommand: selector => {
if (findStrategy(selector).using === "xpath") {
return `${xpathFunc(selector)}[0]`;
} else {
return `document.querySelector("${selector}")`;
}
},

getElementsCommand: selector => {
if (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; \
})())`;
};

0 comments on commit 8444e50

Please sign in to comment.