diff --git a/lib/actions.js b/lib/actions.js index e40fa0b..8b193de 100644 --- a/lib/actions.js +++ b/lib/actions.js @@ -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] @@ -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.