diff --git a/Readme.md b/Readme.md index 912d9a4..db2e3b1 100644 --- a/Readme.md +++ b/Readme.md @@ -325,10 +325,18 @@ Get the URL of the current page. Determines if a selector is visible, or not, on the page. Returns a boolean. +#### .ifVisible(selector, fn) + +Determines if a selector is visible, or not, on the page. Runs a function if it is. + #### .exists(selector) Determines if the selector exists, or not, on the page. Returns a boolean. +#### .ifExists(selector, fn) + +Determines if the selector exists, or not, on the page. Runs a function if it does. + #### .count(selector) Counts the number of `selector` on the page. Returns a number. diff --git a/lib/actions.js b/lib/actions.js index e40fa0b..47cd637 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.ifExists = function(selector, fn) { + debug('.ifExists()', 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.ifVisible = function(selector, fn) { + debug('.ifVisible()', 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. diff --git a/test/index.js b/test/index.js index 3fca99b..e02409a 100644 --- a/test/index.js +++ b/test/index.js @@ -411,6 +411,38 @@ function evaluation(bool) { .be.false(); }); + it('should verify an element exists and execute callback', function() { + var horseman = new Horseman({ + timeout: defaultTimeout, + injectJquery: bool + }); + var str = 'yo'; + return horseman + .open(serverUrl) + .ifExists('a', function(){ + return str; + }) + .close() + .should.eventually + .equal(str); + }); + + it('should verify an element does not exists and not execute callback', function() { + var horseman = new Horseman({ + timeout: defaultTimeout, + injectJquery: bool + }); + var str = 'yo'; + return horseman + .open(serverUrl) + .ifExists('yeti', function(){ + return str; + }) + .close() + .should.eventually + .be.undefined(); + }); + it('should count the number of selectors', function() { var horseman = new Horseman({ timeout: defaultTimeout, @@ -575,6 +607,38 @@ function evaluation(bool) { .be.false(); }); + it('should determine that an element is visible and execute callback', function() { + var horseman = new Horseman({ + timeout: defaultTimeout, + injectJquery: bool + }); + var str = 'yo'; + return horseman + .open(serverUrl) + .ifVisible('a', function(){ + return str; + }) + .close() + .should.eventually + .equal(str); + }); + + it('should determine that an element is not visible and not execute callback', function() { + var horseman = new Horseman({ + timeout: defaultTimeout, + injectJquery: bool + }); + var str = 'yo'; + return horseman + .open(serverUrl) + .ifVisible('.login-popup', function(){ + return str; + }) + .close() + .should.eventually + .be.undefined(); + }); + it('should evaluate javascript', function() { var horseman = new Horseman({ timeout: defaultTimeout,