This repository has been archived by the owner on Aug 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #41 from afgoulart/waitForUrl
Wait for url
- Loading branch information
Showing
2 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
var util = require('util'), | ||
events = require('events'); | ||
|
||
function WaitForUrl() { | ||
events.EventEmitter.call(this); | ||
this.startTimer = null; | ||
this.cb = null; | ||
this.ms = null; | ||
this.selector = null; | ||
this.protocol = require('nightwatch/lib/api/protocol.js')(this.client); | ||
} | ||
|
||
util.inherits(WaitForUrl, events.EventEmitter); | ||
|
||
/** | ||
* Waiting for url expected | ||
* @param {[type]} url [url expected] | ||
* @param {[type]} milliseconds [time for close assert] | ||
* @param {[type]} timeout [time verify] | ||
* @param {[type]} messages [message output] | ||
* @param {Function} callback [callback] | ||
* @return {[type]} [client] | ||
*/ | ||
WaitForUrl.prototype.command = function(url, milliseconds, timeout, messages, callback) { | ||
|
||
if (milliseconds && typeof milliseconds !== 'number') { | ||
throw new Error('waitForCondition expects second parameter to be number; ' + typeof (milliseconds) + ' given'); | ||
} | ||
|
||
var lastArgument = Array.prototype.slice.call(arguments, 0).pop(); | ||
if (typeof (lastArgument) === 'function') { | ||
callback = lastArgument; | ||
} | ||
|
||
if (!messages || typeof messages !== 'object') { | ||
messages = { | ||
success: 'Url expected after ', | ||
timeout: 'Timed out while waiting for url after ' | ||
}; | ||
} | ||
|
||
timeout = timeout && typeof (timeout) !== 'function' && typeof (timeout) !== 'object' ? timeout : 0; | ||
|
||
this.startTimer = new Date().getTime(); | ||
this.cb = callback || function() { | ||
}; | ||
this.ms = milliseconds || 1000; | ||
this.timeout = timeout; | ||
this.url = url; | ||
this.messages = messages; | ||
this.check(); | ||
return this; | ||
}; | ||
|
||
WaitForUrl.prototype.check = function() { | ||
var self = this; | ||
|
||
this.protocol.url(function(result) { | ||
var now = new Date().getTime(); | ||
|
||
if (result.status === 0 && result.value === self.url) { | ||
setTimeout(function() { | ||
var msg = self.messages.success + (now - self.startTimer) + ' milliseconds.'; | ||
self.cb.call(self.client.api, result.value); | ||
self.client.assertion(true, !!result.value, false, msg, true); | ||
return self.emit('complete'); | ||
}, self.timeout); | ||
} else if (now - self.startTimer < self.ms) { | ||
setTimeout(function() { | ||
self.check(); | ||
}, 500); | ||
} else { | ||
var msg = self.messages.timeout + self.ms + ' milliseconds.'; | ||
self.cb.call(self.client.api, false); | ||
self.client.assertion(false, false, false, msg, true); | ||
return self.emit('complete'); | ||
} | ||
}); | ||
}; | ||
|
||
module.exports = WaitForUrl; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
var MockServer = require('mockserver'); | ||
|
||
module.exports = { | ||
setUp: function(callback) { | ||
this.client = require('../nightwatch.js').init(); | ||
|
||
callback(); | ||
}, | ||
|
||
testSuccess: function(test) { | ||
var client = this.client.api; | ||
client.waitForUrl('http://localhost/', 100, 0, function callback(result) { | ||
test.equal(result, 'http://localhost/'); | ||
test.done(); | ||
}); | ||
}, | ||
|
||
testFailure: function(test) { | ||
var client = this.client.api; | ||
client.waitForUrl('http://localhost2/', 600, 0, function callback(result) { | ||
test.equal(result, false); | ||
test.done(); | ||
}); | ||
}, | ||
|
||
tearDown: function(callback) { | ||
this.client = null; | ||
// clean up | ||
callback(); | ||
} | ||
}; |