Capserjs comes with a test framework that provides a toolset that makes it easier for you to test your Web app.
Attention:
1.1 Version Changes
This test framework, including all of its APIs, can be used only under the CASPERJS test subcommand
If you use the Casper.test property outside of the scope of the test frame, the error is reported
starting with 1.1-beta3, you can rewrite Casper's initialization configuration in a test environment and want to know more, you can go to Dedicated FAQ entry . Understand
Unit Test
Imagine cow as the object we want to test:
function Cow () { this.mowed = false; This.moo = function Moo () { this.mowed = true;//mootable State:don ' t do, at home return ' moo! '; };}
Let's write a test suite:
Cow-test.jscasper.test.begin (' Cow can moo ', 2, Function Suite (test) { var cow = new Cow (); Test.assertequals (Cow.moo (), ' moo! '); Test.assert (cow.mowed); Test.done ();});
Use the casperjs Test command to run our tests:
$ CASPERJS Test Cow-test.js
You can see the following execution results:
Make him error:
Casper.test.begin (' Cow can Moo ', 2, Function Suite (test) { var Cow = new Cow (); Test.assertequals (Cow.moo (), ' bazinga! '); Test.assert (cow.mowed); Test.done ();});
You will get the following results:
Browser testing
Now write a test case for Google search:
Googletesting.jscasper.test.begin (' Google search retrieves ' or more results ', 5, Function Suite (test) {Casper.sta RT ("http://www.google.fr/", function () {test.asserttitle ("Google", "Google homepage title is the one expected"); Test.assertexists (' form[action= '/search "] '," main form is found "); This.fill (' form[action= '/search "] ', {q:" Casperjs "}, True); }); Casper.then (function () {test.asserttitle ("Casperjs-recherche Google", "Google title is OK"); Test.asserturlmatch (/q=casperjs/, "search term had been submitted"); Test.asserteval (function () {return __utils__.findall ("H3.R"). length >= 10; }, "Google search for \" Casperjs\ "retrieves ten or more results"); }); Casper.run (function () {test.done (); });});
Now run this use case:
$ CASPERJS Test Googletesting.js
You can get the following results:
In the test environment, set the Casper option:
You have to test the Casper option in the environment that has been preconfigured, you can update the properties in the following ways:
Casper.options.optionName = OptionValue; Where Optionname is obviously the desired option Namecasper.options.clientScripts.push ("New-script.js");
Advanced Technology:
Tester#begin () accepts a method or object to describe a suite, and the object properties allow the Setup ()TearDown () method to be set:
Cow-test.jscasper.test.begin (' Cow can moo ', 2, { setup:function (test) { This.cow = new Cow (); }, Teardown:function (test) { This.cow.destroy (); }, test:function (test) { test.assertequals ( This.cow.moo (), ' moo! '); Test.assert (this.cow.mowed); Test.done (); }});
command-line arguments and options for test:
Parameter Settings command
The CASPERJS Test command will pass the arguments as a file or directory path, and he will recursively scan all passed parameters and look for the corresponding ' *.js ' or ' *.coffee ' and add them to the stack.
Attention:
When you are writing test cases, there are two important considerations:
In a test file, you cannot create multiple Casper objects
Only when parsing to Tester.done () does the test case actually execute
Options
Option use "--" as an identifier
--xunit=<filename> record test results into an XML file in xunit format
--direct or–verbose print a log message to the console
--log-level=<loglevel> setting the level of log messages displayed in the console
--auto-exit=no when all tests have been completed to prevent the end of the test run, he is usually used to append additional actions, and the exit event can be triggered by manually setting exit:
$ CASPERJS Test--auto-exit=nocasper.test.on ("Exit", function () { sometediousasyncprocess ()} (function () { Casper.exit (); });
1.0 version Added
--includes=foo.js,bar.js Introducing foo.js,bar.js when testing file execution
--pre=pre-test.js Add pre-test.js before all test cases are executed
--post=post-test.js adds post-test.js After all test cases have been executed
--fail-fast end execution As soon as the first failure occurs
--concise Create a cleaner output result
--no-colors Output without color
Command-line instance:
$ casperjs Test--includes=foo.js,bar.js --pre=pre-test.js --post=post-test.js --direct -- Log-level=debug --fail-fast test1.js test2.js/path/to/some/test/dir
Attention:
Obsolete since version 1.1:
The--direct option has been renamed to--verbose, although--direct can still work, but his non-advocated
Output Xunit Results:
Casperjs is able to output results that can be xunit parsed in XML format, and he can be supported by continuous integration tools such as Jenkins, to save test results, and to use the --xunit option
$ CASPERJS Test Googletesting.js--xunit=log.xml
You'll get a pretty good result on the bottom side:
<?xml version= "1.0" encoding= "UTF-8"? ><testsuites duration= "1.249" > <testsuite errors= "0" failures= "0" name= "Google search retrieves ten or more results" package= "googletesting" tests= "5" time= "1.249" timestamp= " 2012-12-30t21:27:26.320z "> <testcase classname=" googletesting "name=" Google homepage title is the One Expected "time=" 0.813 "/> <testcase classname=" googletesting "name=" main form is found "time=" 0.002 "/> <testcase classname= "googletesting" name= "Google title is OK" time= "0.416"/> <testcase classname= " Googletesting "name=" search term have been submitted "time=" 0.017 "/> <testcase classname=" googletesting "name = "Google search for "casperjs" Retrieves or more results "time=" 0.001 "/> <system-out/> </testsuite></testsuites>
Casperjs Self-test:
CASPERJS has its own unit test set, which is placed under the tests subdirectory and can run it like this:
$ Casperjs SelfTest
One of the benefits of running this test set is to find the bug that you are running the platform on, and if it fails, on GitHub's file an issue or in Casperjs mailing-list feedback
Extended CASPERJS Test Framework:
This command:
$ CASPERJS test [path]
Is the truncation of this command:
$ casperjs/path/to/casperjs/tests/run.js [path]
If you want to extend the capabilities of the CASPERJS test framework, you can write your own runner methods and expand Casper Objects