closes #346 - emit a warning when trying to use casper.test in non-test env
Showing
2 changed files
with
32 additions
and
10 deletions
... | @@ -154,18 +154,30 @@ var Casper = function Casper(options) { | ... | @@ -154,18 +154,30 @@ var Casper = function Casper(options) { |
154 | this.started = false; | 154 | this.started = false; |
155 | this.step = -1; | 155 | this.step = -1; |
156 | this.steps = []; | 156 | this.steps = []; |
157 | if (phantom.casperTest) { | 157 | this._test = undefined; |
158 | this.test = tester.create(this); | 158 | this.__defineGetter__('test', function() { |
159 | if (!phantom.casperTest) { | ||
160 | this.emit('tester.called'); | ||
161 | return; | ||
162 | } | ||
163 | if (!utils.isObject(this._test)) { | ||
164 | this._test = tester.create(this); | ||
159 | } | 165 | } |
166 | return this._test; | ||
167 | }); | ||
168 | |||
169 | this.once('tester.called', function() { | ||
170 | this.warn('please use `casperjs test` command'); | ||
171 | }); | ||
160 | 172 | ||
161 | // init phantomjs error handler | 173 | // init phantomjs error handler |
162 | this.initErrorHandler(); | 174 | this.initErrorHandler(); |
163 | 175 | ||
164 | this.on('error', function(msg, backtrace) { | 176 | this.on('error', function(msg, backtrace) { |
165 | if (msg === this.test.SKIP_MESSAGE) { // FIXME: decouple testing | 177 | if (msg === '__termination__') { |
166 | return; | 178 | return; |
167 | } | 179 | } |
168 | if (msg.indexOf('AssertionError') === 0) { // FIXME: decouple testing | 180 | if (msg.indexOf('AssertionError') === 0) { |
169 | return; | 181 | return; |
170 | } | 182 | } |
171 | var c = this.getColorizer(); | 183 | var c = this.getColorizer(); |
... | @@ -1313,10 +1325,10 @@ Casper.prototype.runStep = function runStep(step) { | ... | @@ -1313,10 +1325,10 @@ Casper.prototype.runStep = function runStep(step) { |
1313 | var skipLog = utils.isObject(step.options) && step.options.skipLog === true, | 1325 | var skipLog = utils.isObject(step.options) && step.options.skipLog === true, |
1314 | stepInfo = f("Step %d/%d", this.step, this.steps.length), | 1326 | stepInfo = f("Step %d/%d", this.step, this.steps.length), |
1315 | stepResult; | 1327 | stepResult; |
1316 | function getCurrentSuiteNum(casper) { | 1328 | function getCurrentSuiteId(casper) { |
1317 | if (casper.test) { | 1329 | try { |
1318 | return casper.test.currentSuiteNum + "-" + casper.step; | 1330 | return casper.test.getCurrentSuiteId(); |
1319 | } else { | 1331 | } catch (e) { |
1320 | return casper.step; | 1332 | return casper.step; |
1321 | } | 1333 | } |
1322 | } | 1334 | } |
... | @@ -1326,7 +1338,7 @@ Casper.prototype.runStep = function runStep(step) { | ... | @@ -1326,7 +1338,7 @@ Casper.prototype.runStep = function runStep(step) { |
1326 | if (utils.isNumber(this.options.stepTimeout) && this.options.stepTimeout > 0) { | 1338 | if (utils.isNumber(this.options.stepTimeout) && this.options.stepTimeout > 0) { |
1327 | var stepTimeoutCheckInterval = setInterval(function _check(self, start, stepNum) { | 1339 | var stepTimeoutCheckInterval = setInterval(function _check(self, start, stepNum) { |
1328 | if (new Date().getTime() - start > self.options.stepTimeout) { | 1340 | if (new Date().getTime() - start > self.options.stepTimeout) { |
1329 | if (getCurrentSuiteNum(self) === stepNum) { | 1341 | if (getCurrentSuiteId(self) === stepNum) { |
1330 | self.emit('step.timeout'); | 1342 | self.emit('step.timeout'); |
1331 | if (utils.isFunction(self.options.onStepTimeout)) { | 1343 | if (utils.isFunction(self.options.onStepTimeout)) { |
1332 | self.options.onStepTimeout.call(self, self.options.stepTimeout, stepNum); | 1344 | self.options.onStepTimeout.call(self, self.options.stepTimeout, stepNum); |
... | @@ -1334,7 +1346,7 @@ Casper.prototype.runStep = function runStep(step) { | ... | @@ -1334,7 +1346,7 @@ Casper.prototype.runStep = function runStep(step) { |
1334 | } | 1346 | } |
1335 | clearInterval(stepTimeoutCheckInterval); | 1347 | clearInterval(stepTimeoutCheckInterval); |
1336 | } | 1348 | } |
1337 | }, this.options.stepTimeout, this, new Date().getTime(), getCurrentSuiteNum(this)); | 1349 | }, this.options.stepTimeout, this, new Date().getTime(), getCurrentSuiteId(this)); |
1338 | } | 1350 | } |
1339 | this.emit('step.start', step); | 1351 | this.emit('step.start', step); |
1340 | try { | 1352 | try { | ... | ... |
... | @@ -987,6 +987,16 @@ Tester.prototype.findTestFiles = function findTestFiles(dir) { | ... | @@ -987,6 +987,16 @@ Tester.prototype.findTestFiles = function findTestFiles(dir) { |
987 | }; | 987 | }; |
988 | 988 | ||
989 | /** | 989 | /** |
990 | * Computes current suite identifier. | ||
991 | * | ||
992 | * @return String | ||
993 | */ | ||
994 | Tester.prototype.getCurrentSuiteId = function getCurrentSuiteId() { | ||
995 | "use strict"; | ||
996 | return casper.test.currentSuiteNum + "-" + casper.step; | ||
997 | }; | ||
998 | |||
999 | /** | ||
990 | * Formats a message to highlight some parts of it. | 1000 | * Formats a message to highlight some parts of it. |
991 | * | 1001 | * |
992 | * @param String message | 1002 | * @param String message | ... | ... |
-
Please register or sign in to post a comment