Commit 37e301ee 37e301ee66eb9e01dfa4715b0bf1820cad991d29 by Nicolas Perriault

refs #397 - Add setUp() and tearDown() options to Tester#begin()

1 parent 755477ed
......@@ -66,6 +66,25 @@ casper.test.begin('Casperjs.org is navigable', 2, function suite(test) {
});
```
`Tester#begin()` has also `setUp()` and `tearDown()` capabilities:
```js
var range;
casper.test.begin('range tests', 1, {
setUp: function() {
range = [1, 2, 3];
},
tearDown: function() {
range = undefined;
},
test: function(test) {
test.assertEquals(range.length, 3);
test.done();
}
});
```
Also, scraping and testing are now betterly separated in CasperJS, and bad code is now a bit less bad. That involves breaking up BC on some points though:
- The Casper object won't be created with a `test` reference if not invoked using the [`casperjs test` command](http://casperjs.org/testing.html#casper-test-command), therefore the ability to run any test without calling it has been dropped. I know, get over it.
......
......@@ -841,18 +841,29 @@ Tester.prototype.bar = function bar(text, style) {
*/
Tester.prototype.begin = function begin() {
"use strict";
/*jshint maxstatements:20*/
/*jshint maxstatements:40 maxcomplexity:20*/
if (this.started && this.running) {
return this.queue.push(arguments);
}
var description = arguments[0] || f("Untitled suite in %s", this.currentTestFile),
planned,
suiteFn;
suiteFn,
setUp,
tearDown;
if (utils.isFunction(arguments[1])) {
suiteFn = arguments[1];
} else if (utils.isObject(arguments[1])) {
suiteFn = arguments[1].test;
setUp = arguments[1].setUp;
tearDown = arguments[1].tearDown;
} else if (utils.isNumber(arguments[1]) && utils.isFunction(arguments[2])) {
planned = arguments[1];
suiteFn = arguments[2];
} else if (utils.isNumber(arguments[1]) && utils.isObject(arguments[2])) {
planned = arguments[1];
suiteFn = arguments[2].test;
setUp = arguments[2].setUp;
tearDown = arguments[2].tearDown;
} else {
throw new CasperError('Invalid call');
}
......@@ -867,6 +878,9 @@ Tester.prototype.begin = function begin() {
this.executed = 0;
this.running = this.started = true;
try {
if (utils.isFunction(setUp)) {
setUp.call(this, this, this.casper);
}
suiteFn.call(this, this, this.casper);
} catch (err) {
if (err instanceof AssertionError) {
......@@ -874,7 +888,7 @@ Tester.prototype.begin = function begin() {
} else {
this.uncaughtError(err, this.currentTestFile, err.line);
}
this.done();
this.done(tearDown);
}
if (this.options.concise) {
this.casper.echo([
......@@ -907,18 +921,25 @@ Tester.prototype.comment = function comment(message) {
/**
* Declares the current test suite done.
*
* @param Number planned Number of planned tests
* @param Function tearDown Function to call when done
*/
Tester.prototype.done = function done(planned) {
Tester.prototype.done = function done() {
"use strict";
if (arguments.length > 0) {
/*jshint maxstatements:20 maxcomplexity:20*/
var planned, tearDown;
if (utils.isNumber(arguments[0])) {
this.casper.warn('done() `planned` arg is deprecated as of 1.1');
planned = arguments[0];
} else if (utils.isFunction(arguments[0])) {
tearDown = arguments[0];
}
if (this.currentSuite && this.currentSuite.planned && this.currentSuite.planned !== this.executed) {
this.dubious(this.currentSuite.planned, this.executed, this.currentSuite.name);
} else if (planned && planned !== this.executed) {
// BC
this.dubious(planned, this.executed);
} else if (tearDown) {
tearDown.call(this, this, this.casper);
}
if (this.currentSuite) {
this.suiteResults.push(this.currentSuite);
......@@ -1068,6 +1089,16 @@ Tester.prototype.pass = function pass(message) {
});
};
Tester.prototype.prepare = function prepare(config) {
"use strict";
if (!utils.isObject(config)) {
throw new CasperError('prepare() needs a config object');
}
if ('setUp' in config) {
}
};
/**
* Processes an assertion error.
*
......
/*global casper*/
/*jshint strict:false*/
casper.test.begin('open() tests', 16, function(test) {
var current = 0,
tests = [
function(settings) {
test.assertEquals(settings, {
method: "get"
}, "Casper.open() used the expected GET settings");
},
function(settings) {
test.assertEquals(settings, {
method: "post",
data: "plop=42&chuck=norris"
}, "Casper.open() used the expected POST settings");
},
function(settings) {
test.assertEquals(settings, {
method: "put",
data: "plop=42&chuck=norris"
}, "Casper.open() used the expected PUT settings");
},
function(settings) {
test.assertEquals(settings, {
method: "get",
username: 'bob',
password: 'sinclar'
}, "Casper.open() used the expected HTTP auth settings");
},
function(settings) {
test.assertEquals(settings, {
method: "get"
}, "Casper.thenOpen() used the expected GET settings");
},
function(settings) {
test.assertEquals(settings, {
method: "post",
data: "plop=42&chuck=norris"
}, "Casper.thenOpen() used the expected POST settings");
},
function(settings) {
test.assertEquals(settings, {
method: "put",
data: "plop=42&chuck=norris"
}, "Casper.thenOpen() used the expected PUT settings");
},
function(settings) {
test.assertEquals(settings, {
method: "get",
username: 'bob',
password: 'sinclar'
}, "Casper.thenOpen() used the expected HTTP auth settings");
var usedSettings;
function onOpen(url, settings) {
usedSettings = settings;
}
function setUp(test) {
casper.start().on('open', onOpen);
}
function tearDown(test) {
usedSettings = undefined;
casper.removeListener('open', onOpen);
}
casper.test.begin('open() GET tests', 2, {
setUp: setUp,
tearDown: tearDown,
test: function(test) {
casper.open('tests/site/index.html').then(function() {
test.pass("Casper.open() can open and load a location using GET");
test.assertEquals(usedSettings, {
method: "get"
}, "Casper.open() used the expected GET settings");
});
casper.run(function() {
test.done();
});
}
});
casper.test.begin('open() POST tests', 2, {
setUp: setUp,
tearDown: tearDown,
test: function(test) {
casper.open('tests/site/index.html', {
method: 'post',
data: {
plop: 42,
chuck: 'norris'
}
];
casper.start().on('open', function(url, settings) {
tests[current++](settings);
});
// GET
casper.open('tests/site/index.html').then(function() {
test.pass("Casper.open() can open and load a location using GET");
});
// POST
casper.open('tests/site/index.html', {
method: 'post',
data: {
plop: 42,
chuck: 'norris'
}
}).then(function() {
test.pass("Casper.open() can open and load a location using POST");
});
// PUT
casper.open('tests/site/index.html', {
method: 'put',
data: {
plop: 42,
chuck: 'norris'
}
}).then(function() {
test.pass("Casper.open() can open and load a location using PUT");
});
// HTTP Auth
casper.open('tests/site/index.html', {
method: 'get',
username: 'bob',
password: 'sinclar'
}).then(function() {
test.pass("Casper.open() can open and load a location using HTTP auth");
});
// GET with thenOpen
casper.thenOpen('tests/site/index.html').then(function() {
test.pass("Casper.thenOpen() can open and load a location using GET");
});
// POST with thenOpen
casper.thenOpen('tests/site/index.html', {
method: 'post',
data: {
plop: 42,
chuck: 'norris'
}
}, function() {
test.pass("Casper.thenOpen() can open and load a location using POST");
});
// PUT with thenOpen
casper.thenOpen('tests/site/index.html', {
method: 'put',
data: {
plop: 42,
chuck: 'norris'
}
}, function() {
test.pass("Casper.thenOpen() can open and load a location using PUT");
});
// HTTP Auth with thenOpen
casper.thenOpen('tests/site/index.html', {
method: 'get',
username: 'bob',
password: 'sinclar'
}, function() {
test.pass("Casper.thenOpen() can open and load a location using HTTP auth");
});
casper.run(function() {
this.removeAllListeners('open');
test.done();
});
}).then(function() {
test.pass("Casper.open() can open and load a location using POST");
test.assertEquals(usedSettings, {
method: "post",
data: "plop=42&chuck=norris"
}, "Casper.open() used the expected POST settings");
});
casper.run(function() {
test.done();
});
}
});
casper.test.begin('open() PUT tests', 2, {
setUp: setUp,
tearDown: tearDown,
test: function(test) {
casper.thenOpen('tests/site/index.html', {
method: 'put',
data: {
plop: 42,
chuck: 'norris'
}
}).then(function() {
test.pass("Casper.open() can open and load a location using PUT");
test.assertEquals(usedSettings, {
method: "put",
data: "plop=42&chuck=norris"
}, "Casper.open() used the expected PUT settings");
});
casper.run(function() {
test.done();
});
}
});
casper.test.begin('open() PUT tests', 2, {
setUp: setUp,
tearDown: tearDown,
test: function(test) {
// HTTP Auth
casper.thenOpen('tests/site/index.html', {
method: 'get',
username: 'bob',
password: 'sinclar'
}).then(function() {
test.pass("Casper.open() can open and load a location using HTTP auth");
test.assertEquals(usedSettings, {
method: "get",
username: 'bob',
password: 'sinclar'
}, "Casper.open() used the expected HTTP auth settings");
});
casper.run(function() {
test.done();
});
}
});
casper.test.begin('open() PUT tests', 2, {
setUp: setUp,
tearDown: tearDown,
test: function(test) {
// GET with thenOpen
casper.thenOpen('tests/site/index.html').then(function() {
test.pass("Casper.thenOpen() can open and load a location using GET");
test.assertEquals(usedSettings, {
method: "get"
}, "Casper.thenOpen() used the expected GET settings");
});
casper.run(function() {
test.done();
});
}
});
casper.test.begin('open() PUT tests', 2, {
setUp: setUp,
tearDown: tearDown,
test: function(test) {
// POST with thenOpen
casper.thenOpen('tests/site/index.html', {
method: 'post',
data: {
plop: 42,
chuck: 'norris'
}
}, function() {
test.pass("Casper.thenOpen() can open and load a location using POST");
test.assertEquals(usedSettings, {
method: "post",
data: "plop=42&chuck=norris"
}, "Casper.thenOpen() used the expected POST settings");
});
casper.run(function() {
test.done();
});
}
});
casper.test.begin('open() PUT tests', 2, {
setUp: setUp,
tearDown: tearDown,
test: function(test) {
// PUT with thenOpen
casper.thenOpen('tests/site/index.html', {
method: 'put',
data: {
plop: 42,
chuck: 'norris'
}
}, function() {
test.pass("Casper.thenOpen() can open and load a location using PUT");
test.assertEquals(usedSettings, {
method: "put",
data: "plop=42&chuck=norris"
}, "Casper.thenOpen() used the expected PUT settings");
});
casper.run(function() {
test.done();
});
}
});
casper.test.begin('open() PUT tests', 2, {
setUp: setUp,
tearDown: tearDown,
test: function(test) {
// HTTP Auth with thenOpen
casper.thenOpen('tests/site/index.html', {
method: 'get',
username: 'bob',
password: 'sinclar'
}, function() {
test.pass("Casper.thenOpen() can open and load a location using HTTP auth");
test.assertEquals(usedSettings, {
method: "get",
username: 'bob',
password: 'sinclar'
}, "Casper.thenOpen() used the expected HTTP auth settings");
});
casper.run(function() {
test.done();
});
}
});
......