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) { ...@@ -66,6 +66,25 @@ casper.test.begin('Casperjs.org is navigable', 2, function suite(test) {
66 }); 66 });
67 ``` 67 ```
68 68
69 `Tester#begin()` has also `setUp()` and `tearDown()` capabilities:
70
71 ```js
72 var range;
73
74 casper.test.begin('range tests', 1, {
75 setUp: function() {
76 range = [1, 2, 3];
77 },
78 tearDown: function() {
79 range = undefined;
80 },
81 test: function(test) {
82 test.assertEquals(range.length, 3);
83 test.done();
84 }
85 });
86 ```
87
69 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: 88 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:
70 89
71 - 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. 90 - 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) { ...@@ -841,18 +841,29 @@ Tester.prototype.bar = function bar(text, style) {
841 */ 841 */
842 Tester.prototype.begin = function begin() { 842 Tester.prototype.begin = function begin() {
843 "use strict"; 843 "use strict";
844 /*jshint maxstatements:20*/ 844 /*jshint maxstatements:40 maxcomplexity:20*/
845 if (this.started && this.running) { 845 if (this.started && this.running) {
846 return this.queue.push(arguments); 846 return this.queue.push(arguments);
847 } 847 }
848 var description = arguments[0] || f("Untitled suite in %s", this.currentTestFile), 848 var description = arguments[0] || f("Untitled suite in %s", this.currentTestFile),
849 planned, 849 planned,
850 suiteFn; 850 suiteFn,
851 setUp,
852 tearDown;
851 if (utils.isFunction(arguments[1])) { 853 if (utils.isFunction(arguments[1])) {
852 suiteFn = arguments[1]; 854 suiteFn = arguments[1];
855 } else if (utils.isObject(arguments[1])) {
856 suiteFn = arguments[1].test;
857 setUp = arguments[1].setUp;
858 tearDown = arguments[1].tearDown;
853 } else if (utils.isNumber(arguments[1]) && utils.isFunction(arguments[2])) { 859 } else if (utils.isNumber(arguments[1]) && utils.isFunction(arguments[2])) {
854 planned = arguments[1]; 860 planned = arguments[1];
855 suiteFn = arguments[2]; 861 suiteFn = arguments[2];
862 } else if (utils.isNumber(arguments[1]) && utils.isObject(arguments[2])) {
863 planned = arguments[1];
864 suiteFn = arguments[2].test;
865 setUp = arguments[2].setUp;
866 tearDown = arguments[2].tearDown;
856 } else { 867 } else {
857 throw new CasperError('Invalid call'); 868 throw new CasperError('Invalid call');
858 } 869 }
...@@ -867,6 +878,9 @@ Tester.prototype.begin = function begin() { ...@@ -867,6 +878,9 @@ Tester.prototype.begin = function begin() {
867 this.executed = 0; 878 this.executed = 0;
868 this.running = this.started = true; 879 this.running = this.started = true;
869 try { 880 try {
881 if (utils.isFunction(setUp)) {
882 setUp.call(this, this, this.casper);
883 }
870 suiteFn.call(this, this, this.casper); 884 suiteFn.call(this, this, this.casper);
871 } catch (err) { 885 } catch (err) {
872 if (err instanceof AssertionError) { 886 if (err instanceof AssertionError) {
...@@ -874,7 +888,7 @@ Tester.prototype.begin = function begin() { ...@@ -874,7 +888,7 @@ Tester.prototype.begin = function begin() {
874 } else { 888 } else {
875 this.uncaughtError(err, this.currentTestFile, err.line); 889 this.uncaughtError(err, this.currentTestFile, err.line);
876 } 890 }
877 this.done(); 891 this.done(tearDown);
878 } 892 }
879 if (this.options.concise) { 893 if (this.options.concise) {
880 this.casper.echo([ 894 this.casper.echo([
...@@ -907,18 +921,25 @@ Tester.prototype.comment = function comment(message) { ...@@ -907,18 +921,25 @@ Tester.prototype.comment = function comment(message) {
907 /** 921 /**
908 * Declares the current test suite done. 922 * Declares the current test suite done.
909 * 923 *
910 * @param Number planned Number of planned tests 924 * @param Function tearDown Function to call when done
911 */ 925 */
912 Tester.prototype.done = function done(planned) { 926 Tester.prototype.done = function done() {
913 "use strict"; 927 "use strict";
914 if (arguments.length > 0) { 928 /*jshint maxstatements:20 maxcomplexity:20*/
929 var planned, tearDown;
930 if (utils.isNumber(arguments[0])) {
915 this.casper.warn('done() `planned` arg is deprecated as of 1.1'); 931 this.casper.warn('done() `planned` arg is deprecated as of 1.1');
932 planned = arguments[0];
933 } else if (utils.isFunction(arguments[0])) {
934 tearDown = arguments[0];
916 } 935 }
917 if (this.currentSuite && this.currentSuite.planned && this.currentSuite.planned !== this.executed) { 936 if (this.currentSuite && this.currentSuite.planned && this.currentSuite.planned !== this.executed) {
918 this.dubious(this.currentSuite.planned, this.executed, this.currentSuite.name); 937 this.dubious(this.currentSuite.planned, this.executed, this.currentSuite.name);
919 } else if (planned && planned !== this.executed) { 938 } else if (planned && planned !== this.executed) {
920 // BC 939 // BC
921 this.dubious(planned, this.executed); 940 this.dubious(planned, this.executed);
941 } else if (tearDown) {
942 tearDown.call(this, this, this.casper);
922 } 943 }
923 if (this.currentSuite) { 944 if (this.currentSuite) {
924 this.suiteResults.push(this.currentSuite); 945 this.suiteResults.push(this.currentSuite);
...@@ -1068,6 +1089,16 @@ Tester.prototype.pass = function pass(message) { ...@@ -1068,6 +1089,16 @@ Tester.prototype.pass = function pass(message) {
1068 }); 1089 });
1069 }; 1090 };
1070 1091
1092 Tester.prototype.prepare = function prepare(config) {
1093 "use strict";
1094 if (!utils.isObject(config)) {
1095 throw new CasperError('prepare() needs a config object');
1096 }
1097 if ('setUp' in config) {
1098
1099 }
1100 };
1101
1071 /** 1102 /**
1072 * Processes an assertion error. 1103 * Processes an assertion error.
1073 * 1104 *
......
1 /*global casper*/ 1 /*global casper*/
2 /*jshint strict:false*/ 2 /*jshint strict:false*/
3 casper.test.begin('open() tests', 16, function(test) { 3 var usedSettings;
4 var current = 0,
5 tests = [
6 function(settings) {
7 test.assertEquals(settings, {
8 method: "get"
9 }, "Casper.open() used the expected GET settings");
10 },
11 function(settings) {
12 test.assertEquals(settings, {
13 method: "post",
14 data: "plop=42&chuck=norris"
15 }, "Casper.open() used the expected POST settings");
16 },
17 function(settings) {
18 test.assertEquals(settings, {
19 method: "put",
20 data: "plop=42&chuck=norris"
21 }, "Casper.open() used the expected PUT settings");
22 },
23 function(settings) {
24 test.assertEquals(settings, {
25 method: "get",
26 username: 'bob',
27 password: 'sinclar'
28 }, "Casper.open() used the expected HTTP auth settings");
29 },
30 function(settings) {
31 test.assertEquals(settings, {
32 method: "get"
33 }, "Casper.thenOpen() used the expected GET settings");
34 },
35 function(settings) {
36 test.assertEquals(settings, {
37 method: "post",
38 data: "plop=42&chuck=norris"
39 }, "Casper.thenOpen() used the expected POST settings");
40 },
41 function(settings) {
42 test.assertEquals(settings, {
43 method: "put",
44 data: "plop=42&chuck=norris"
45 }, "Casper.thenOpen() used the expected PUT settings");
46 },
47 function(settings) {
48 test.assertEquals(settings, {
49 method: "get",
50 username: 'bob',
51 password: 'sinclar'
52 }, "Casper.thenOpen() used the expected HTTP auth settings");
53 }
54 ];
55 4
56 casper.start().on('open', function(url, settings) { 5 function onOpen(url, settings) {
57 tests[current++](settings); 6 usedSettings = settings;
58 }); 7 }
8
9 function setUp(test) {
10 casper.start().on('open', onOpen);
11 }
59 12
60 // GET 13 function tearDown(test) {
14 usedSettings = undefined;
15 casper.removeListener('open', onOpen);
16 }
17
18 casper.test.begin('open() GET tests', 2, {
19 setUp: setUp,
20 tearDown: tearDown,
21 test: function(test) {
61 casper.open('tests/site/index.html').then(function() { 22 casper.open('tests/site/index.html').then(function() {
62 test.pass("Casper.open() can open and load a location using GET"); 23 test.pass("Casper.open() can open and load a location using GET");
24 test.assertEquals(usedSettings, {
25 method: "get"
26 }, "Casper.open() used the expected GET settings");
63 }); 27 });
64 28
65 // POST 29 casper.run(function() {
30 test.done();
31 });
32 }
33 });
34
35 casper.test.begin('open() POST tests', 2, {
36 setUp: setUp,
37 tearDown: tearDown,
38 test: function(test) {
66 casper.open('tests/site/index.html', { 39 casper.open('tests/site/index.html', {
67 method: 'post', 40 method: 'post',
68 data: { 41 data: {
...@@ -71,10 +44,23 @@ casper.test.begin('open() tests', 16, function(test) { ...@@ -71,10 +44,23 @@ casper.test.begin('open() tests', 16, function(test) {
71 } 44 }
72 }).then(function() { 45 }).then(function() {
73 test.pass("Casper.open() can open and load a location using POST"); 46 test.pass("Casper.open() can open and load a location using POST");
47 test.assertEquals(usedSettings, {
48 method: "post",
49 data: "plop=42&chuck=norris"
50 }, "Casper.open() used the expected POST settings");
74 }); 51 });
75 52
76 // PUT 53 casper.run(function() {
77 casper.open('tests/site/index.html', { 54 test.done();
55 });
56 }
57 });
58
59 casper.test.begin('open() PUT tests', 2, {
60 setUp: setUp,
61 tearDown: tearDown,
62 test: function(test) {
63 casper.thenOpen('tests/site/index.html', {
78 method: 'put', 64 method: 'put',
79 data: { 65 data: {
80 plop: 42, 66 plop: 42,
...@@ -82,22 +68,64 @@ casper.test.begin('open() tests', 16, function(test) { ...@@ -82,22 +68,64 @@ casper.test.begin('open() tests', 16, function(test) {
82 } 68 }
83 }).then(function() { 69 }).then(function() {
84 test.pass("Casper.open() can open and load a location using PUT"); 70 test.pass("Casper.open() can open and load a location using PUT");
71 test.assertEquals(usedSettings, {
72 method: "put",
73 data: "plop=42&chuck=norris"
74 }, "Casper.open() used the expected PUT settings");
85 }); 75 });
86 76
77 casper.run(function() {
78 test.done();
79 });
80 }
81 });
82
83 casper.test.begin('open() PUT tests', 2, {
84 setUp: setUp,
85 tearDown: tearDown,
86 test: function(test) {
87 // HTTP Auth 87 // HTTP Auth
88 casper.open('tests/site/index.html', { 88 casper.thenOpen('tests/site/index.html', {
89 method: 'get', 89 method: 'get',
90 username: 'bob', 90 username: 'bob',
91 password: 'sinclar' 91 password: 'sinclar'
92 }).then(function() { 92 }).then(function() {
93 test.pass("Casper.open() can open and load a location using HTTP auth"); 93 test.pass("Casper.open() can open and load a location using HTTP auth");
94 test.assertEquals(usedSettings, {
95 method: "get",
96 username: 'bob',
97 password: 'sinclar'
98 }, "Casper.open() used the expected HTTP auth settings");
94 }); 99 });
95 100
101 casper.run(function() {
102 test.done();
103 });
104 }
105 });
106
107 casper.test.begin('open() PUT tests', 2, {
108 setUp: setUp,
109 tearDown: tearDown,
110 test: function(test) {
96 // GET with thenOpen 111 // GET with thenOpen
97 casper.thenOpen('tests/site/index.html').then(function() { 112 casper.thenOpen('tests/site/index.html').then(function() {
98 test.pass("Casper.thenOpen() can open and load a location using GET"); 113 test.pass("Casper.thenOpen() can open and load a location using GET");
114 test.assertEquals(usedSettings, {
115 method: "get"
116 }, "Casper.thenOpen() used the expected GET settings");
99 }); 117 });
100 118
119 casper.run(function() {
120 test.done();
121 });
122 }
123 });
124
125 casper.test.begin('open() PUT tests', 2, {
126 setUp: setUp,
127 tearDown: tearDown,
128 test: function(test) {
101 // POST with thenOpen 129 // POST with thenOpen
102 casper.thenOpen('tests/site/index.html', { 130 casper.thenOpen('tests/site/index.html', {
103 method: 'post', 131 method: 'post',
...@@ -107,8 +135,22 @@ casper.test.begin('open() tests', 16, function(test) { ...@@ -107,8 +135,22 @@ casper.test.begin('open() tests', 16, function(test) {
107 } 135 }
108 }, function() { 136 }, function() {
109 test.pass("Casper.thenOpen() can open and load a location using POST"); 137 test.pass("Casper.thenOpen() can open and load a location using POST");
138 test.assertEquals(usedSettings, {
139 method: "post",
140 data: "plop=42&chuck=norris"
141 }, "Casper.thenOpen() used the expected POST settings");
142 });
143
144 casper.run(function() {
145 test.done();
110 }); 146 });
147 }
148 });
111 149
150 casper.test.begin('open() PUT tests', 2, {
151 setUp: setUp,
152 tearDown: tearDown,
153 test: function(test) {
112 // PUT with thenOpen 154 // PUT with thenOpen
113 casper.thenOpen('tests/site/index.html', { 155 casper.thenOpen('tests/site/index.html', {
114 method: 'put', 156 method: 'put',
...@@ -118,8 +160,22 @@ casper.test.begin('open() tests', 16, function(test) { ...@@ -118,8 +160,22 @@ casper.test.begin('open() tests', 16, function(test) {
118 } 160 }
119 }, function() { 161 }, function() {
120 test.pass("Casper.thenOpen() can open and load a location using PUT"); 162 test.pass("Casper.thenOpen() can open and load a location using PUT");
163 test.assertEquals(usedSettings, {
164 method: "put",
165 data: "plop=42&chuck=norris"
166 }, "Casper.thenOpen() used the expected PUT settings");
167 });
168
169 casper.run(function() {
170 test.done();
121 }); 171 });
172 }
173 });
122 174
175 casper.test.begin('open() PUT tests', 2, {
176 setUp: setUp,
177 tearDown: tearDown,
178 test: function(test) {
123 // HTTP Auth with thenOpen 179 // HTTP Auth with thenOpen
124 casper.thenOpen('tests/site/index.html', { 180 casper.thenOpen('tests/site/index.html', {
125 method: 'get', 181 method: 'get',
...@@ -127,10 +183,15 @@ casper.test.begin('open() tests', 16, function(test) { ...@@ -127,10 +183,15 @@ casper.test.begin('open() tests', 16, function(test) {
127 password: 'sinclar' 183 password: 'sinclar'
128 }, function() { 184 }, function() {
129 test.pass("Casper.thenOpen() can open and load a location using HTTP auth"); 185 test.pass("Casper.thenOpen() can open and load a location using HTTP auth");
186 test.assertEquals(usedSettings, {
187 method: "get",
188 username: 'bob',
189 password: 'sinclar'
190 }, "Casper.thenOpen() used the expected HTTP auth settings");
130 }); 191 });
131 192
132 casper.run(function() { 193 casper.run(function() {
133 this.removeAllListeners('open');
134 test.done(); 194 test.done();
135 }); 195 });
196 }
136 }); 197 });
......