Skip to content

Instantly share code, notes, and snippets.

@proclaim
Created May 15, 2017 07:25
Show Gist options
  • Save proclaim/7a242f7d4d7aa7cb0ec352beb3a3429c to your computer and use it in GitHub Desktop.
Save proclaim/7a242f7d4d7aa7cb0ec352beb3a3429c to your computer and use it in GitHub Desktop.
WaitForText custom command for nightwatchjs
var util = require('util');
var events = require('events');
function WaitForText() {
events.EventEmitter.call(this);
}
util.inherits(WaitForText, events.EventEmitter);
WaitForText.prototype.command = function(selector, expectedText, timeoutInSec, callback) {
let self = this,
timeoutRetryInMilliseconds = 100,
startTimeInMilliseconds = new Date().getTime();
if(!timeoutInSec) { return this; }
const checker = (_selector, _expectedText, _timeoutInMilliseconds) => {
this.client.api.getText(selector, result => {
let now = new Date().getTime();
if(result.status === 0 && expectedText === result.value) {
self.client.api.assert.containsText(selector, expectedText);
if (typeof callback === 'function') {
callback.call(this);
}
self.emit('complete');
}
else if(now - startTimeInMilliseconds < _timeoutInMilliseconds) {
setTimeout(() => {
checker(_selector, _expectedText, _timeoutInMilliseconds);
}, timeoutRetryInMilliseconds);
}
else {
self.emit('error', `expect ${_expectedText} but got ${result.value}`);
}
});
};
checker(selector, expectedText, timeoutInSec * 1000);
};
module.exports = WaitForText;
@proclaim
Copy link
Author

// to use this
browser.waitForText('.selector', 'Ready', 10);

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