Created
May 15, 2017 07:25
-
-
Save proclaim/7a242f7d4d7aa7cb0ec352beb3a3429c to your computer and use it in GitHub Desktop.
WaitForText custom command for nightwatchjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
// to use this
browser.waitForText('.selector', 'Ready', 10);