-
Notifications
You must be signed in to change notification settings - Fork 124
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add .if_exists()/.if_visible() (implements #266) #267
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should pass the resolution value of the previous Promise to For example ...
.return('foobar')
.ifExists('selectorThatExists', function(res) {
console.log(res); // Should log 'foobar'
}) |
||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please make the indentation consistent, as I am neurotic about that. |
||
}); | ||
}; | ||
|
||
/** | ||
* 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again, you should utilize the existing |
||
.__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. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should just use the existing
.exists()
action rather than re-implementing it.