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

Commit f8f4ca2

Browse files
committed
Merge pull request #41 from afgoulart/waitForUrl
Wait for url
2 parents ba6356a + 43260fe commit f8f4ca2

File tree

2 files changed

+112
-0
lines changed

2 files changed

+112
-0
lines changed

commands/waitForUrl.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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;

tests/src/testWaitForUrl.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
var MockServer = require('mockserver');
2+
3+
module.exports = {
4+
setUp: function(callback) {
5+
this.client = require('../nightwatch.js').init();
6+
7+
callback();
8+
},
9+
10+
testSuccess: function(test) {
11+
var client = this.client.api;
12+
client.waitForUrl('http://localhost/', 100, 0, function callback(result) {
13+
test.equal(result, 'http://localhost/');
14+
test.done();
15+
});
16+
},
17+
18+
testFailure: function(test) {
19+
var client = this.client.api;
20+
client.waitForUrl('http://localhost2/', 600, 0, function callback(result) {
21+
test.equal(result, false);
22+
test.done();
23+
});
24+
},
25+
26+
tearDown: function(callback) {
27+
this.client = null;
28+
// clean up
29+
callback();
30+
}
31+
};

0 commit comments

Comments
 (0)