|
| 1 | +var util = require('util'), |
| 2 | + events = require('events'); |
| 3 | + |
| 4 | +function WaitForUrl() { |
| 5 | + events.EventEmitter.call(this); |
| 6 | + this.startTimer = null; |
| 7 | + this.cb = null; |
| 8 | + this.ms = null; |
| 9 | + this.selector = null; |
| 10 | + this.protocol = require('nightwatch/lib/api/protocol.js')(this.client); |
| 11 | +} |
| 12 | + |
| 13 | +util.inherits(WaitForUrl, events.EventEmitter); |
| 14 | + |
| 15 | +/** |
| 16 | + * Waiting for url expected |
| 17 | + * @param {[type]} url [url expected] |
| 18 | + * @param {[type]} milliseconds [time for close assert] |
| 19 | + * @param {[type]} timeout [time verify] |
| 20 | + * @param {[type]} messages [message output] |
| 21 | + * @param {Function} callback [callback] |
| 22 | + * @return {[type]} [client] |
| 23 | + */ |
| 24 | +WaitForUrl.prototype.command = function(url, milliseconds, timeout, messages, callback) { |
| 25 | + |
| 26 | + if (milliseconds && typeof milliseconds !== 'number') { |
| 27 | + throw new Error('waitForCondition expects second parameter to be number; ' + typeof (milliseconds) + ' given'); |
| 28 | + } |
| 29 | + |
| 30 | + var lastArgument = Array.prototype.slice.call(arguments, 0).pop(); |
| 31 | + if (typeof (lastArgument) === 'function') { |
| 32 | + callback = lastArgument; |
| 33 | + } |
| 34 | + |
| 35 | + if (!messages || typeof messages !== 'object') { |
| 36 | + messages = { |
| 37 | + success: 'Url expected after ', |
| 38 | + timeout: 'Timed out while waiting for url after ' |
| 39 | + }; |
| 40 | + } |
| 41 | + |
| 42 | + timeout = timeout && typeof (timeout) !== 'function' && typeof (timeout) !== 'object' ? timeout : 0; |
| 43 | + |
| 44 | + this.startTimer = new Date().getTime(); |
| 45 | + this.cb = callback || function() { |
| 46 | + }; |
| 47 | + this.ms = milliseconds || 1000; |
| 48 | + this.timeout = timeout; |
| 49 | + this.url = url; |
| 50 | + this.messages = messages; |
| 51 | + this.check(); |
| 52 | + return this; |
| 53 | +}; |
| 54 | + |
| 55 | +WaitForUrl.prototype.check = function() { |
| 56 | + var self = this; |
| 57 | + |
| 58 | + this.protocol.url(function(result) { |
| 59 | + var now = new Date().getTime(); |
| 60 | + |
| 61 | + if (result.status === 0 && result.value === self.url) { |
| 62 | + setTimeout(function() { |
| 63 | + var msg = self.messages.success + (now - self.startTimer) + ' milliseconds.'; |
| 64 | + self.cb.call(self.client.api, result.value); |
| 65 | + self.client.assertion(true, !!result.value, false, msg, true); |
| 66 | + return self.emit('complete'); |
| 67 | + }, self.timeout); |
| 68 | + } else if (now - self.startTimer < self.ms) { |
| 69 | + setTimeout(function() { |
| 70 | + self.check(); |
| 71 | + }, 500); |
| 72 | + } else { |
| 73 | + var msg = self.messages.timeout + self.ms + ' milliseconds.'; |
| 74 | + self.cb.call(self.client.api, false); |
| 75 | + self.client.assertion(false, false, false, msg, true); |
| 76 | + return self.emit('complete'); |
| 77 | + } |
| 78 | + }); |
| 79 | +}; |
| 80 | + |
| 81 | +module.exports = WaitForUrl; |
0 commit comments