Wait for an element to be enabled in NightwatchJS
Published: 2020-07-16
In NightwatchJS, you can wait for an element to be present, or to be visible. But there is no built-in command to wait for an element to be enabled. Luckily, this can be done by using the :enabled
CSS pseudo-class, see MDN docs.
# Sample
The sample test below does the following things:
- Maximizes the browser window
- Navigates to the DuckDuckGo URL
- Waits for the search bar to be enabled and visible for up to 10 seconds
- Sets the search bar value
- Sends the RETURN key
- Saves a screenshot
- Ends the browser session
module.exports = {
'Search DuckDuckGo': (browser) => {
const selector = '#search_form_input_homepage:enabled';
const wait = 10 * 1000;
browser
.maximizeWindow()
.url('https://duckduckgo.com')
.waitForElementVisible(selector, wait)
.setValue(selector, 'NightwatchJS')
.keys(browser.Keys.RETURN)
.saveScreenshot('./screenshot.png')
.end();
}
};
# Acknowledgements
Shoutout to this stackoverflow answer for the tip.