Skip to content
This repository has been archived by the owner on Aug 5, 2020. It is now read-only.

Commit

Permalink
Merge pull request #41 from afgoulart/waitForUrl
Browse files Browse the repository at this point in the history
Wait for url
  • Loading branch information
ellenmobify committed Dec 8, 2015
2 parents ba6356a + 43260fe commit f8f4ca2
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 0 deletions.
81 changes: 81 additions & 0 deletions commands/waitForUrl.js
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;
31 changes: 31 additions & 0 deletions tests/src/testWaitForUrl.js
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();
}
};

0 comments on commit f8f4ca2

Please sign in to comment.