In the page I\'m testing, two buttons may be displayed: BASIC or ADVANCED.
I want to be able to tell if the ADVANCED button is showing -- and if so, click it.
<
You seem to be on the right track with isVisible. From the nightwatch documentation, we see that in the callback you can check the result.value
property to see whether or not the element was visible, i.e:
browser.isVisible('#advanced-search', results => {
if (results.value) { /* is visible */ }
else { /* is not visible */ }
});
Alternatively, you could use the approach suggested by Saifur. Call the selenium api's .elements
command and then check the result array's length:
browser.elements('css selector', '#advanced-search', results => {
if (results.value.length > 0) { /* element exists */ }
else { /* element does not exist */ }
});
This in fact could be wrapped into a custom command:
// isPresent.js
module.exports.command = function (selector, callback) {
return this.elements('css selector', selector, results => {
if (results.status !== 0) { // some error occurred, handle accordingly
}
callback(results.value.length > 0);
});
};
then in normal code you could call it like this:
browser.isPresent('#advanced-search', advancedSearchPresent => {
// make decisions here
}
If you are going to be making additional api calls in the callback, it may be wise to wrap it all in a .perform
call:
browser.perform((_, done) => {
browser.isPresent('#advanced-search', advancedSearchPresent => {
// ...do more stuff...
done();
});
});
As to why the .perform
is necessary, this might be helpful.