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
exports.create = function create(options) {
"use strict";
// This is a bit of a hack to check if one is trying to override the preconfigured
// casper instance from within a test environment.
if (phantom.casperTest && window.casper) {
console.error("Fatal: you can't override the preconfigured casper instance in a test environment.");
console.error("Docs: http://docs.casperjs.org/en/latest/testing.html#test-command-args-and-options");
phantom.exit(1);
}
return new Casper(options);
};
......
......@@ -304,6 +304,13 @@ class TestCommandOutputTest(CasperExecTestBase):
], failing=True)
@timeout(20)
def test_casper_test_instance_overriding(self):
script_path = os.path.join(TEST_ROOT, 'tester', 'casper-instance-override.js')
self.assertCommandOutputContains('test ' + script_path, [
"Fatal: you can't override the preconfigured casper instance",
], failing=True)
@timeout(20)
def test_dubious_test(self):
script_path = os.path.join(TEST_ROOT, 'tester', 'dubious.js')
self.assertCommandOutputContains('test ' + script_path, [
......
// this should never happen
// http://docs.casperjs.org/en/latest/testing.html#test-command-args-and-options
var casper = require("casper").create();
casper.test.begin("foo", function(test) {
"use strict";
test.assert(true);
test.done();
});