This repository has been archived by the owner on Jan 4, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[JENKINS-40662] custom nightwatch command to watch for location chang…
…es, to avoid brittle use of pause
- Loading branch information
Cliff Meyers
committed
Jan 3, 2017
1 parent
e9087cb
commit e703781
Showing
1 changed file
with
60 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,60 @@ | ||
/** | ||
* @module waitForLocationChange | ||
* @memberof custom_commands | ||
*/ | ||
const util = require('util'); | ||
const events = require('events'); | ||
|
||
function Cmd() { | ||
events.EventEmitter.call(this); | ||
} | ||
util.inherits(Cmd, events.EventEmitter); | ||
|
||
var POLLING_FREQUENCY = 100; | ||
var TIMEOUT = 5000; | ||
|
||
/** | ||
* @description Nightwatch command to wait for the browser's href to change. | ||
* Polls the browser at a given frequency to wait for any change to location.href | ||
*/ | ||
const waitForLocationChange = function () { | ||
var self = this; | ||
// var startTime = new Date().getTime(); | ||
var locationPollingTimeout = null; | ||
|
||
self.api.url(function(response) { | ||
var initialHref = response.value; | ||
|
||
var checkForUrlChange = function() { | ||
self.api.url(function (response) { | ||
if (response.value !== initialHref) { | ||
// var ellapsed = new Date().getTime() - startTime; | ||
// console.log('url changed from: ' + initialHref + ' to: ' + response.value + 'after ' + ellapsed + ' milliseconds.'); | ||
cleanUp(); | ||
self.emit('complete'); | ||
} | ||
else { | ||
locationPollingTimeout = setTimeout(checkForUrlChange, POLLING_FREQUENCY); | ||
} | ||
}); | ||
}; | ||
|
||
var errorTimeout = setTimeout(function() { | ||
cleanUp(); | ||
var error = new Error('timed out waiting for location change'); | ||
self.emit('error', error); | ||
}, TIMEOUT); | ||
|
||
var cleanUp = function() { | ||
clearTimeout(locationPollingTimeout); | ||
clearTimeout(errorTimeout); | ||
}; | ||
|
||
checkForUrlChange(); | ||
}); | ||
|
||
return this; | ||
}; | ||
|
||
Cmd.prototype.command = waitForLocationChange; | ||
module.exports = Cmd; |