问题
I am learning CasperJS and how to write unit tests but my test fails. I tried the example code from the website and it fails as well! I am confused about how to use the testing framework in CasperJS.
This is my code:
var casper = require('casper').create();
casper.test.begin('The title exists', 1, function suite(test) {
casper.start('http://stackoverflow.com', function() {
test.assertExists('title');
}).run(function() {
test.done();
});
});
and execute it:
casperjs test script.js
Output:
Test file: test.js
Fatal: you can't override the preconfigured casper instance in a test environment.
Docs: http://docs.casperjs.org/en/latest/testing.html#test-command-args-and-options
Unsafe JavaScript attempt to access frame with URL about:blank from frame with URL file:///usr/local/lib/node_modules/casperjs/bin/bootstrap.js. Domains, protocols and ports must match.
回答1:
I override the preconfigured casper
instance. If I delete the first line, it works without a problem.
Remove:
var casper = require('casper').create();
回答2:
I came across this issue, too. Unfortunately, the docs don't spell out very well the different combinations that will both work and that will fail.
If you comment out/remove the first line
(var casper = require('casper').create();
) and run your command casperjs test test.js
, then your script will work.
OR
If you leave in the first line, but don't include "test" as part of your command line: casperjs test.js
then your script will work.
Notably, the second option produces no output in the console.
If you have both options - telling casper via the command line that you are using the test
function, and trying to create a new instance in the script (when that first line is included) - that is why it gives the error it does. Basically, one or the other will create a new casper instance. You don't need to create it twice.
来源:https://stackoverflow.com/questions/30352996/cant-run-the-testing-framework-in-casperjs