Skip to content

Commit

Permalink
feat(config): Allow tests be to run in a new window instead of iframe
Browse files Browse the repository at this point in the history
  • Loading branch information
axemclion committed Dec 4, 2013
1 parent bd7fc5f commit 471e3a8
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 13 deletions.
24 changes: 20 additions & 4 deletions client/karma.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ var util = require('./util');


/* jshint unused: false */
var Karma = function(socket, context, navigator, location) {
var Karma = function(socket, iframe, opener, navigator, location) {
var hasError = false;
var startEmitted = false;
var reloadingContext = false;
var store = {};
var self = this;
var queryParams = util.parseQueryParams(location.search);
Expand All @@ -20,6 +21,19 @@ var Karma = function(socket, context, navigator, location) {
this.VERSION = constant.VERSION;
this.config = {};

var childWindow = null;
var navigateContextTo = function(url) {
if (self.config.useIframe === false) {
if (childWindow === null || childWindow.closed === true) {
// If this is the first time we are opening the window, or the window is closed
childWindow = opener('about:blank');
}
childWindow.location = url;
} else {
iframe.src = url;
}
};

this.setupContext = function(contextWindow) {
if (hasError) {
return;
Expand All @@ -43,7 +57,7 @@ var Karma = function(socket, context, navigator, location) {
};

contextWindow.onbeforeunload = function(e, b) {
if (context.src !== 'about:blank') {
if (!reloadingContext) {
// TODO(vojta): show what test (with explanation about jasmine.UPDATE_INTERVAL)
contextWindow.__karma__.error('Some of your tests did a full page reload!');
}
Expand Down Expand Up @@ -90,7 +104,8 @@ var Karma = function(socket, context, navigator, location) {


var clearContext = function() {
context.src = 'about:blank';
reloadingContext = true;
navigateContextTo('about:blank');
};

// error during js file loading (most likely syntax error)
Expand Down Expand Up @@ -190,8 +205,9 @@ var Karma = function(socket, context, navigator, location) {
// reset hasError and reload the iframe
hasError = false;
startEmitted = false;
reloadingContext = false;
self.config = cfg;
context.src = constant.CONTEXT_URL;
navigateContextTo(constant.CONTEXT_URL);

// clear the console before run
// works only on FF (Safari, Chrome do not allow to clear console from js source)
Expand Down
4 changes: 2 additions & 2 deletions client/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ var socket = io.connect('http://' + location.host, {

// instantiate the updater of the view
new StatusUpdater(socket, util.elm('title'), util.elm('banner'), util.elm('browsers'));

window.karma = new Karma(socket, util.elm('context'), window.navigator, window.location);
window.karma = new Karma(socket, util.elm('context'), window.open,
window.navigator, window.location);
9 changes: 9 additions & 0 deletions docs/config/01-configuration-file.md
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,15 @@ on whether all tests passed or any tests failed.
is handed off to [socket.io](https://github.com/LearnBoost/Socket.IO/wiki/Configuring-Socket.IO) (which manages the communication
between browsers and the testing server).

## client.useIframe
**Type:** Boolean

**Default:** `true`

**Description:** Run the tests inside an iframe or a new window

If true, Karma runs the tests inside an iframe. If false, Karma runs the tests in a new window. Some tests may not run in an iFrame and may need a new window to run.


## urlRoot
**Type:** String
Expand Down
3 changes: 2 additions & 1 deletion lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,8 @@ var Config = function() {
this.transports = ['websocket', 'flashsocket', 'xhr-polling', 'jsonp-polling'];
this.plugins = ['karma-*'];
this.client = {
args: []
args: [],
useIframe: true
};
this.browserDisconnectTimeout = 2000;
this.browserDisconnectTolerance = 0;
Expand Down
7 changes: 6 additions & 1 deletion static/context.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@
manner. -->
<script type="text/javascript">
// sets window.__karma__ and overrides console and error handling
window.parent.karma.setupContext(window);
// Use window.opener if this was opened by someone else - in a new window
if (window.opener) {
window.opener.karma.setupContext(window);
} else {
window.parent.karma.setupContext(window);
}

// All served files with the latest timestamps
%MAPPINGS%
Expand Down
27 changes: 22 additions & 5 deletions test/client/karma.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ var MockSocket = require('./mocks').Socket;


describe('Karma', function() {
var socket, k, spyStart, windowNavigator, windowLocation;
var socket, k, spyStart, windowNavigator, windowLocation, spywindowOpener;

var setTransportTo = function(transportName) {
socket._setTransportNameTo(transportName);
Expand All @@ -15,19 +15,36 @@ describe('Karma', function() {
socket = new MockSocket();
windowNavigator = {};
windowLocation = {search: ''};
k = new Karma(socket, {}, windowNavigator, windowLocation);
spywindowOpener = spyOn(window, 'open').andReturn({});
k = new Karma(socket, {}, window.open, windowNavigator, windowLocation);
spyStart = spyOn(k, 'start');

});


it('should start execution when all files loaded and pass config', function() {
var config = {};
var config = {
useIframe: true
};

socket.emit('execute', config);
expect(spyStart).not.toHaveBeenCalled();

k.loaded();
expect(spyStart).toHaveBeenCalledWith(config);
});

it('should open a new window when useIFrame is false', function() {
var config = {
useIframe: false
};

socket.emit('execute', config);
expect(spyStart).not.toHaveBeenCalled();

k.loaded();
expect(spyStart).toHaveBeenCalledWith(config);
expect(spywindowOpener).toHaveBeenCalledWith('about:blank');
});


Expand Down Expand Up @@ -79,7 +96,7 @@ describe('Karma', function() {
it('should report browser id', function() {
windowLocation.search = '?id=567';
socket = new MockSocket();
k = new Karma(socket, {}, windowNavigator, windowLocation);
k = new Karma(socket, {}, window.open, windowNavigator, windowLocation);

var spyInfo = jasmine.createSpy('onInfo').andCallFake(function(info) {
expect(info.id).toBe('567');
Expand Down Expand Up @@ -232,7 +249,7 @@ describe('Karma', function() {
it('should 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);
k = new Karma(socket, {}, window.open, windowNavigator, windowLocation);

spyOn(socket, 'disconnect');

Expand Down

0 comments on commit 471e3a8

Please sign in to comment.