Skip to content

Instantly share code, notes, and snippets.

@maxmarkus
Created February 2, 2016 13:07
Show Gist options
  • Save maxmarkus/4bc27a8202467d311ff7 to your computer and use it in GitHub Desktop.
Save maxmarkus/4bc27a8202467d311ff7 to your computer and use it in GitHub Desktop.
nightwatch command for gracefully clicking an element within a parent
'use strict';
/**
* clicks on an element found in a parent container
*
* @param {string} dropdownSelector mostly dropdown, but can be also other list or container
* @param {string} elementSelector element selector to click on
* @param {boolean} graceful if elementSelector is not present, either continue (true), or throw an error (false)
* @param {boolean} maxTimeout timeout to wait for element
* @return {mixed} graceful = true: continues, graceful = false: throws error
*/
exports.command = function(dropdownSelector, elementSelector, graceful, maxTimeout) {
console.log('searching inside', dropdownSelector, 'clicking', elementSelector);
this.useCss()
.pause(500)
.waitForElementVisible(dropdownSelector, maxTimeout)
.click(dropdownSelector)
.element('css selector', elementSelector, function(visible) {
if(visible.status !== -1){
console.log('Dropdown element found: ' + elementSelector);
this
.getText(elementSelector, function(result) {
console.log('Clicking on: ', result.value);
})
.waitForElementVisible(elementSelector, 10000)
.click(elementSelector);
} else {
if(graceful){
console.log('No element '+elementSelector+' inside '+dropdownSelector+' not found. Ok.');
} else {
throw new Error('Element '+elementSelector+' inside '+dropdownSelector+' not found. Stopping.');
}
}
return this;
})
// .click(elementSelector)
.pause(1000);
return this;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment