Skip to content

Commit

Permalink
feat: redirect client to "return_url" if specified
Browse files Browse the repository at this point in the history
When capturing a browser, the url can specified a return url (as query param `return_url`) and Karma will redirect the client to that url after the test execution is done.
  • Loading branch information
vojtajina committed Aug 30, 2013
1 parent 2ac1651 commit 6af2c89
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 2 deletions.
11 changes: 9 additions & 2 deletions client/karma.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ var Karma = function(socket, context, navigator, location) {
var startEmitted = false;
var store = {};
var self = this;
var browserId = (location.search.match(/\?id=(.*)/) || [])[1] || util.generateId('manual-');
var queryParams = util.parseQueryParams(location.search);
var browserId = queryParams.id || util.generateId('manual-');
var returnUrl = queryParams.return_url || null;

var resultsBufferLimit = 1;
var resultsBuffer = [];
Expand Down Expand Up @@ -127,7 +129,12 @@ var Karma = function(socket, context, navigator, location) {
// tests could run in the same event loop, we wouldn't notice.
setTimeout(function() {
socket.emit('complete', result || {});
clearContext();
if (returnUrl) {
socket.disconnect();
location.href = returnUrl;
} else {
clearContext();
}
}, 0);
};

Expand Down
13 changes: 13 additions & 0 deletions client/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,16 @@ exports.isUndefined = function(value) {
exports.isDefined = function(value) {
return !exports.isUndefined(value);
};

exports.parseQueryParams = function(locationSearch) {
var params = {};
var pairs = locationSearch.substr(1).split('&');
var keyValue;

for (var i = 0; i < pairs.length; i++) {
keyValue = pairs[i].split('=');
params[decodeURIComponent(keyValue[0])] = decodeURIComponent(keyValue[1]);
}

return params;
};
13 changes: 13 additions & 0 deletions test/client/karma.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,5 +227,18 @@ describe('Karma', function() {
k.complete();
expect(spyResult).toHaveBeenCalled();
});


it('should disconnect navigate the client to return_url if specified', function() {
windowLocation.search = '?id=567&return_url=http://return.com';
socket = new MockSocket();
k = new Karma(socket, {}, windowNavigator, windowLocation);

spyOn(socket, 'disconnect');

k.complete();
expect(socket.disconnect).toHaveBeenCalled();
expect(windowLocation.href).toBe('http://return.com');
});
});
});
4 changes: 4 additions & 0 deletions test/client/mocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ var MockSocket = function() {

this.socket = {transport: {name: 'websocket'}};

this.disconnect = function() {
this.emit('disconnect');
};

// MOCK API
this._setTransportNameTo = function(transportName) {
this.socket.transport.name = transportName;
Expand Down
11 changes: 11 additions & 0 deletions test/client/util.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
var util = require('../../client/util');


describe('util', function() {

describe('parseQueryParams', function() {
var params = util.parseQueryParams('?id=123&return_url=http://whatever.com');

expect(params).toEqual({id: '123', return_url: 'http://whatever.com'});
});
});

0 comments on commit 6af2c89

Please sign in to comment.