From 35fdf34819cc043c2aea409b60e5263be50aba9e Mon Sep 17 00:00:00 2001 From: Aex Aey Date: Tue, 28 Feb 2017 05:34:40 +0100 Subject: [PATCH 1/3] Add .if_exists()/.if_visible() (implements #266) --- lib/actions.js | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) 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. From f9e842971991c776b0e8d19e466dace4e8224714 Mon Sep 17 00:00:00 2001 From: Aex Aey Date: Tue, 7 Mar 2017 21:43:49 +0100 Subject: [PATCH 2/3] use headlessCamelCase for ifExists/ifVisible --- lib/actions.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/actions.js b/lib/actions.js index 8b193de..47cd637 100644 --- a/lib/actions.js +++ b/lib/actions.js @@ -1068,8 +1068,8 @@ exports.exists = function(selector) { * @param {string} [selector] * @param {function} fn */ -exports.if_exists = function(selector, fn) { - debug('.if_exists()', selector); +exports.ifExists = function(selector, fn) { + debug('.ifExists()', selector); return this.count(selector).then(function(count) { if (count > 0) { return fn(); @@ -1259,8 +1259,8 @@ exports.visible = function(selector) { * @param {string} selector - The selector to find the visibility of. * @param {function} fn */ -exports.if_visible = function(selector, fn) { - debug('.if_visible()', selector); +exports.ifVisible = function(selector, fn) { + debug('.ifVisible()', selector); return this .__evaluate(function visible(selector) { if (window.jQuery) { From 97bd9312de0e1933df8ca7c1ba0466aa647b3743 Mon Sep 17 00:00:00 2001 From: Aex Aey Date: Wed, 8 Mar 2017 00:52:12 +0100 Subject: [PATCH 3/3] Add tests & docs for ifExists/ifVisible --- Readme.md | 8 +++++++ test/index.js | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) 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/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,