Skip to content

Commit

Permalink
Add .if_exists()/.if_visible() (implements #266)
Browse files Browse the repository at this point in the history
  • Loading branch information
aexaey committed Feb 28, 2017
1 parent 7cec99b commit 35fdf34
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions lib/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -1063,6 +1063,20 @@ exports.exists = function(selector) {
});
};

/**
* Determine if the selector exists, at least once, on the page and execute function if it does
* @param {string} [selector]
* @param {function} fn
*/
exports.if_exists = function(selector, fn) {
debug('.if_exists()', selector);
return this.count(selector).then(function(count) {
if (count > 0) {
return fn();
};
});
};

/**
* Get the HTML for the page, or optionally for a selector.
* @param {string} [selector]
Expand Down Expand Up @@ -1240,6 +1254,29 @@ exports.visible = function(selector) {
});
};

/**
* Determines if an element is visible and execute function if it is.
* @param {string} selector - The selector to find the visibility of.
* @param {function} fn
*/
exports.if_visible = function(selector, fn) {
debug('.if_visible()', selector);
return this
.__evaluate(function visible(selector) {
if (window.jQuery) {
return jQuery(selector).is(':visible');
} else {
var elem = document.querySelector(selector);
return elem && (elem.offsetWidth > 0 && elem.offsetHeight > 0);
}
}, selector)
.then(function(vis) {
if (vis) {
return fn();
};
});
};

/**
* Log the output from either a previous chain method,
* or a string the user passed in.
Expand Down

0 comments on commit 35fdf34

Please sign in to comment.