Skip to content

Commit

Permalink
Add ability to clone web element
Browse files Browse the repository at this point in the history
  • Loading branch information
schipiga committed Jul 24, 2018
1 parent 4e5d99a commit 1b205f9
Showing 1 changed file with 38 additions and 21 deletions.
59 changes: 38 additions & 21 deletions lib/pom/element.js
Expand Up @@ -2,11 +2,11 @@
/**
* Creates a new instance of `Element`.
*
* `Element` binds DOM control in browser context with virtual control in test.
* `Element` binds DOM element in browser context with virtual element in test.
*
* @class
* @name Element
* @arg {string} name - Control name.
* @arg {string} name - Element name.
* @arg {object} selector - CSS selector of DOM element.
* @arg {Page} page - Page with element.
*/
Expand Down Expand Up @@ -108,12 +108,12 @@ Element.prototype.setText = async function (text, opts) {
await this._getDriver().setValue(this.selector, text);
};
/**
* Gets DOM control location with attributes:
* Gets DOM element location with attributes:
* `x`, `y`, `midX`, `midY`, `width`, `height`.
*
* @async
* @method
* @return {Promise<object>} Location of control.
* @return {Promise<object>} Location of element.
*/
Element.prototype.location = function () {

Expand All @@ -132,7 +132,7 @@ Element.prototype.location = function () {
return this._getDriver().execute(cmd).then(result => result.value);
};
/**
* Scrolls control into browser viewport.
* Scrolls element into browser viewport.
*
* @async
* @method
Expand All @@ -154,7 +154,7 @@ Element.prototype.scrollIntoView = async function (opts) {
await this._getDriver().execute(cmd);
};
/**
* Clicks control in browser.
* Clicks element in browser.
*
* @async
* @method
Expand Down Expand Up @@ -206,7 +206,7 @@ Element.prototype.pClick = async function (opts) {
await this._event.up(loc.midX, loc.midY);
};
/**
* Taps control in browser.
* Taps element in browser.
*
* @async
* @method
Expand All @@ -218,14 +218,14 @@ Element.prototype.pClick = async function (opts) {
*/
Element.prototype.tap = Element.prototype.click;
/**
* Defines whether control is selected or no.
* 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 control is selected, `false` otherwise.
* @return {Promise<boolean>} `true` if element is selected, `false` otherwise.
*/
Element.prototype.isSelected = async function (opts) {

Expand All @@ -243,24 +243,24 @@ Element.prototype.isSelected = async function (opts) {
return result.includes("selected");
};
/**
* Defines whether control is exist or no.
* Defines whether element is exist or no.
*
* @async
* @method
* @return {Promise<boolean>} `true` if control is exist, `false` otherwise.
* @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 control is exist.
* Waits for element is exist.
*
* @async
* @method
* @arg {number} [timeout] - Timeout to wait, sec.
* @return {Promise}
* @throws {TimeoutError} If control doesn't exist after timeout.
* @throws {TimeoutError} If element doesn't exist after timeout.
*/
Element.prototype.waitForExist = function (timeout) {
timeout = U.defVal(timeout, CONF.web.uiTimeout) * 1000;
Expand All @@ -271,13 +271,13 @@ Element.prototype.waitForExist = function (timeout) {
}, timeout, errMsg);
};
/**
* Waits for control isn't exist.
* Waits for element isn't exist.
*
* @async
* @method
* @arg {number} [timeout] - Timeout to wait, sec.
* @return {Promise}
* @throws {TimeoutError} If control is still exist after timeout.
* @throws {TimeoutError} If element is still exist after timeout.
*/
Element.prototype.waitForNonExist = function (timeout) {
timeout = U.defVal(timeout, CONF.web.uiTimeout) * 1000;
Expand All @@ -288,10 +288,10 @@ Element.prototype.waitForNonExist = function (timeout) {
}, timeout, errMsg);
};
/**
* Defines whether control is visible or no.
* Defines whether element is visible or no.
*
* @method
* @return {Promise<boolean>} `true` if control is visible, `false` otherwise.
* @return {Promise<boolean>} `true` if element is visible, `false` otherwise.
*/
Element.prototype.isVisible = function () {
return this.isExist().then(result => {
Expand All @@ -300,13 +300,13 @@ Element.prototype.isVisible = function () {
});
};
/**
* Waits for control is visible.
* Waits for element is visible.
*
* @async
* @method
* @arg {number} [timeout] - Timeout to wait, sec.
* @return {Promise}
* @throws {TimeoutError} If control isn't visible after timeout.
* @throws {TimeoutError} If element isn't visible after timeout.
*/
Element.prototype.waitForVisible = function (timeout) {
timeout = U.defVal(timeout, CONF.web.uiTimeout) * 1000;
Expand All @@ -318,13 +318,13 @@ Element.prototype.waitForVisible = function (timeout) {
}, timeout, errMsg);
};
/**
* Waits for control is invisible.
* Waits for element is invisible.
*
* @async
* @method
* @arg {number} [timeout] - Timeout to wait, sec.
* @return {Promise}
* @throws {TimeoutError} If control is still visible after timeout.
* @throws {TimeoutError} If element is still visible after timeout.
*/
Element.prototype.waitForInvisible = function (timeout) {
timeout = U.defVal(timeout, CONF.web.uiTimeout) * 1000;
Expand All @@ -334,5 +334,22 @@ Element.prototype.waitForInvisible = function (timeout) {
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;

0 comments on commit 1b205f9

Please sign in to comment.