refs #397 - Add setUp() and tearDown() options to Tester#begin()
Showing
3 changed files
with
249 additions
and
138 deletions
... | @@ -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, | 4 | |
5 | tests = [ | 5 | function onOpen(url, settings) { |
6 | function(settings) { | 6 | usedSettings = settings; |
7 | test.assertEquals(settings, { | 7 | } |
8 | method: "get" | 8 | |
9 | }, "Casper.open() used the expected GET settings"); | 9 | function setUp(test) { |
10 | }, | 10 | casper.start().on('open', onOpen); |
11 | function(settings) { | 11 | } |
12 | test.assertEquals(settings, { | 12 | |
13 | method: "post", | 13 | function tearDown(test) { |
14 | data: "plop=42&chuck=norris" | 14 | usedSettings = undefined; |
15 | }, "Casper.open() used the expected POST settings"); | 15 | casper.removeListener('open', onOpen); |
16 | }, | 16 | } |
17 | function(settings) { | 17 | |
18 | test.assertEquals(settings, { | 18 | casper.test.begin('open() GET tests', 2, { |
19 | method: "put", | 19 | setUp: setUp, |
20 | data: "plop=42&chuck=norris" | 20 | tearDown: tearDown, |
21 | }, "Casper.open() used the expected PUT settings"); | 21 | test: function(test) { |
22 | }, | 22 | casper.open('tests/site/index.html').then(function() { |
23 | function(settings) { | 23 | test.pass("Casper.open() can open and load a location using GET"); |
24 | test.assertEquals(settings, { | 24 | test.assertEquals(usedSettings, { |
25 | method: "get", | 25 | method: "get" |
26 | username: 'bob', | 26 | }, "Casper.open() used the expected GET settings"); |
27 | password: 'sinclar' | 27 | }); |
28 | }, "Casper.open() used the expected HTTP auth settings"); | 28 | |
29 | }, | 29 | casper.run(function() { |
30 | function(settings) { | 30 | test.done(); |
31 | test.assertEquals(settings, { | 31 | }); |
32 | method: "get" | 32 | } |
33 | }, "Casper.thenOpen() used the expected GET settings"); | 33 | }); |
34 | }, | 34 | |
35 | function(settings) { | 35 | casper.test.begin('open() POST tests', 2, { |
36 | test.assertEquals(settings, { | 36 | setUp: setUp, |
37 | method: "post", | 37 | tearDown: tearDown, |
38 | data: "plop=42&chuck=norris" | 38 | test: function(test) { |
39 | }, "Casper.thenOpen() used the expected POST settings"); | 39 | casper.open('tests/site/index.html', { |
40 | }, | 40 | method: 'post', |
41 | function(settings) { | 41 | data: { |
42 | test.assertEquals(settings, { | 42 | plop: 42, |
43 | method: "put", | 43 | chuck: 'norris' |
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 | } | 44 | } |
54 | ]; | 45 | }).then(function() { |
55 | 46 | test.pass("Casper.open() can open and load a location using POST"); | |
56 | casper.start().on('open', function(url, settings) { | 47 | test.assertEquals(usedSettings, { |
57 | tests[current++](settings); | 48 | method: "post", |
58 | }); | 49 | data: "plop=42&chuck=norris" |
59 | 50 | }, "Casper.open() used the expected POST settings"); | |
60 | // GET | 51 | }); |
61 | casper.open('tests/site/index.html').then(function() { | 52 | |
62 | test.pass("Casper.open() can open and load a location using GET"); | 53 | casper.run(function() { |
63 | }); | 54 | test.done(); |
64 | 55 | }); | |
65 | // POST | 56 | } |
66 | casper.open('tests/site/index.html', { | 57 | }); |
67 | method: 'post', | 58 | |
68 | data: { | 59 | casper.test.begin('open() PUT tests', 2, { |
69 | plop: 42, | 60 | setUp: setUp, |
70 | chuck: 'norris' | 61 | tearDown: tearDown, |
71 | } | 62 | test: function(test) { |
72 | }).then(function() { | 63 | casper.thenOpen('tests/site/index.html', { |
73 | test.pass("Casper.open() can open and load a location using POST"); | 64 | method: 'put', |
74 | }); | 65 | data: { |
75 | 66 | plop: 42, | |
76 | // PUT | 67 | chuck: 'norris' |
77 | casper.open('tests/site/index.html', { | 68 | } |
78 | method: 'put', | 69 | }).then(function() { |
79 | data: { | 70 | test.pass("Casper.open() can open and load a location using PUT"); |
80 | plop: 42, | 71 | test.assertEquals(usedSettings, { |
81 | chuck: 'norris' | 72 | method: "put", |
82 | } | 73 | data: "plop=42&chuck=norris" |
83 | }).then(function() { | 74 | }, "Casper.open() used the expected PUT settings"); |
84 | test.pass("Casper.open() can open and load a location using PUT"); | 75 | }); |
85 | }); | 76 | |
86 | 77 | casper.run(function() { | |
87 | // HTTP Auth | 78 | test.done(); |
88 | casper.open('tests/site/index.html', { | 79 | }); |
89 | method: 'get', | 80 | } |
90 | username: 'bob', | 81 | }); |
91 | password: 'sinclar' | 82 | |
92 | }).then(function() { | 83 | casper.test.begin('open() PUT tests', 2, { |
93 | test.pass("Casper.open() can open and load a location using HTTP auth"); | 84 | setUp: setUp, |
94 | }); | 85 | tearDown: tearDown, |
95 | 86 | test: function(test) { | |
96 | // GET with thenOpen | 87 | // HTTP Auth |
97 | casper.thenOpen('tests/site/index.html').then(function() { | 88 | casper.thenOpen('tests/site/index.html', { |
98 | test.pass("Casper.thenOpen() can open and load a location using GET"); | 89 | method: 'get', |
99 | }); | 90 | username: 'bob', |
100 | 91 | password: 'sinclar' | |
101 | // POST with thenOpen | 92 | }).then(function() { |
102 | casper.thenOpen('tests/site/index.html', { | 93 | test.pass("Casper.open() can open and load a location using HTTP auth"); |
103 | method: 'post', | 94 | test.assertEquals(usedSettings, { |
104 | data: { | 95 | method: "get", |
105 | plop: 42, | 96 | username: 'bob', |
106 | chuck: 'norris' | 97 | password: 'sinclar' |
107 | } | 98 | }, "Casper.open() used the expected HTTP auth settings"); |
108 | }, function() { | 99 | }); |
109 | test.pass("Casper.thenOpen() can open and load a location using POST"); | 100 | |
110 | }); | 101 | casper.run(function() { |
111 | 102 | test.done(); | |
112 | // PUT with thenOpen | 103 | }); |
113 | casper.thenOpen('tests/site/index.html', { | 104 | } |
114 | method: 'put', | 105 | }); |
115 | data: { | 106 | |
116 | plop: 42, | 107 | casper.test.begin('open() PUT tests', 2, { |
117 | chuck: 'norris' | 108 | setUp: setUp, |
118 | } | 109 | tearDown: tearDown, |
119 | }, function() { | 110 | test: function(test) { |
120 | test.pass("Casper.thenOpen() can open and load a location using PUT"); | 111 | // GET with thenOpen |
121 | }); | 112 | casper.thenOpen('tests/site/index.html').then(function() { |
122 | 113 | test.pass("Casper.thenOpen() can open and load a location using GET"); | |
123 | // HTTP Auth with thenOpen | 114 | test.assertEquals(usedSettings, { |
124 | casper.thenOpen('tests/site/index.html', { | 115 | method: "get" |
125 | method: 'get', | 116 | }, "Casper.thenOpen() used the expected GET settings"); |
126 | username: 'bob', | 117 | }); |
127 | password: 'sinclar' | 118 | |
128 | }, function() { | 119 | casper.run(function() { |
129 | test.pass("Casper.thenOpen() can open and load a location using HTTP auth"); | 120 | test.done(); |
130 | }); | 121 | }); |
131 | 122 | } | |
132 | casper.run(function() { | 123 | }); |
133 | this.removeAllListeners('open'); | 124 | |
134 | test.done(); | 125 | casper.test.begin('open() PUT tests', 2, { |
135 | }); | 126 | setUp: setUp, |
127 | tearDown: tearDown, | ||
128 | test: function(test) { | ||
129 | // POST with thenOpen | ||
130 | casper.thenOpen('tests/site/index.html', { | ||
131 | method: 'post', | ||
132 | data: { | ||
133 | plop: 42, | ||
134 | chuck: 'norris' | ||
135 | } | ||
136 | }, function() { | ||
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(); | ||
146 | }); | ||
147 | } | ||
148 | }); | ||
149 | |||
150 | casper.test.begin('open() PUT tests', 2, { | ||
151 | setUp: setUp, | ||
152 | tearDown: tearDown, | ||
153 | test: function(test) { | ||
154 | // PUT with thenOpen | ||
155 | casper.thenOpen('tests/site/index.html', { | ||
156 | method: 'put', | ||
157 | data: { | ||
158 | plop: 42, | ||
159 | chuck: 'norris' | ||
160 | } | ||
161 | }, function() { | ||
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(); | ||
171 | }); | ||
172 | } | ||
173 | }); | ||
174 | |||
175 | casper.test.begin('open() PUT tests', 2, { | ||
176 | setUp: setUp, | ||
177 | tearDown: tearDown, | ||
178 | test: function(test) { | ||
179 | // HTTP Auth with thenOpen | ||
180 | casper.thenOpen('tests/site/index.html', { | ||
181 | method: 'get', | ||
182 | username: 'bob', | ||
183 | password: 'sinclar' | ||
184 | }, function() { | ||
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"); | ||
191 | }); | ||
192 | |||
193 | casper.run(function() { | ||
194 | test.done(); | ||
195 | }); | ||
196 | } | ||
136 | }); | 197 | }); | ... | ... |
-
Please register or sign in to post a comment