Skip to content

Instantly share code, notes, and snippets.

@alanshaw
Created July 21, 2017 16:38
Show Gist options
  • Save alanshaw/61fb4432a91722eeee4d357d6643c268 to your computer and use it in GitHub Desktop.
Save alanshaw/61fb4432a91722eeee4d357d6643c268 to your computer and use it in GitHub Desktop.
Nightwatch commands for getting state of multiple elements
// Get state for multiple elements
// getState is passed an element and should return a promise that gets some state
// e.g. http://nightwatchjs.org/api#elementIdText
// Callback will be called with an array of the state values once they're all resolved
exports.command = function getElementsState (selector, getState, callback) {
this.elements('css selector', selector, function (elements) {
const values = elements.value.map(getState)
Promise.all(values).then(callback)
})
return this
}
// Get text for multiple elements
// Callback is called with an array of texts for the matching elements
exports.command = function getElementsText (selector, callback) {
const getState = (element) => {
return new Promise((resolve) => {
this.elementIdText(element.ELEMENT, (text) => resolve(text.value))
})
}
return this.getElementsState(selector, getState, callback)
}
@s-kugan
Copy link

s-kugan commented Feb 27, 2020

Can you also post sample test for these commands ?.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment