Commit 29907e58 29907e582958efc3b6747dd5f4e0422a6a46b1bf by Nicolas Perriault

prevent creation of casper instance in test env

This was a hard decision to take, but too many people just don't
read the docs and create new casper instance in a test env,
overriding the preconfigured one which creates cumbersome and
hard to understand situation.

Hopefully this change will inform users right from the place they
read docs the most, fatal error messages.
1 parent d9ac3180
...@@ -48,6 +48,13 @@ var defaultUserAgent = phantom.defaultPageSettings.userAgent ...@@ -48,6 +48,13 @@ var defaultUserAgent = phantom.defaultPageSettings.userAgent
48 48
49 exports.create = function create(options) { 49 exports.create = function create(options) {
50 "use strict"; 50 "use strict";
51 // This is a bit of a hack to check if one is trying to override the preconfigured
52 // casper instance from within a test environment.
53 if (phantom.casperTest && window.casper) {
54 console.error("Fatal: you can't override the preconfigured casper instance in a test environment.");
55 console.error("Docs: http://docs.casperjs.org/en/latest/testing.html#test-command-args-and-options");
56 phantom.exit(1);
57 }
51 return new Casper(options); 58 return new Casper(options);
52 }; 59 };
53 60
......
...@@ -304,6 +304,13 @@ class TestCommandOutputTest(CasperExecTestBase): ...@@ -304,6 +304,13 @@ class TestCommandOutputTest(CasperExecTestBase):
304 ], failing=True) 304 ], failing=True)
305 305
306 @timeout(20) 306 @timeout(20)
307 def test_casper_test_instance_overriding(self):
308 script_path = os.path.join(TEST_ROOT, 'tester', 'casper-instance-override.js')
309 self.assertCommandOutputContains('test ' + script_path, [
310 "Fatal: you can't override the preconfigured casper instance",
311 ], failing=True)
312
313 @timeout(20)
307 def test_dubious_test(self): 314 def test_dubious_test(self):
308 script_path = os.path.join(TEST_ROOT, 'tester', 'dubious.js') 315 script_path = os.path.join(TEST_ROOT, 'tester', 'dubious.js')
309 self.assertCommandOutputContains('test ' + script_path, [ 316 self.assertCommandOutputContains('test ' + script_path, [
......
1 // this should never happen
2 // http://docs.casperjs.org/en/latest/testing.html#test-command-args-and-options
3 var casper = require("casper").create();
4
5 casper.test.begin("foo", function(test) {
6 "use strict";
7 test.assert(true);
8 test.done();
9 });