Casperjs instructions for use-test

Source: Internet
Author: User
Tags continuous integration tools xunit

Capserjs comes with a testing framework that provides a tool set that allows you to easily test your web applications. Note: Version 1.1 Changes the test framework, including all its APIs. It can only be used under the casperjs test subcommand if you use casper outside the scope of the test framework. the test attribute will report an error starting from 1.1-beta3. You can rewrite the Initialization Configuration of casper in the test environment. For more information, go to the dedicated FAQ entry. understanding Unit Test ideas Cow is the object we want to test: function Cow () {this. mowed = false; this. moo = function moo () {this. mowed = true; // mootable state: don't do that 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 () ;}); run our test with the casperjs test command: $ casperjs test cow-test.js you can see the following execution result: Make him an 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: test the browser and write a test case to test google Search: // googletesting. jscasper. test. begin ('Google search retrieves 10 or more results', 5, function suite (test) {casper. start ("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 has been submitted"); test. assertEval (function () {return _ utils __. findAll ("h3.r "). length >=10 ;}, "google search for \" casperjs \ "retrieves 10 or more results") ;}); casper. run (function () {test. done () ;}); now Run this example: $ casperjs test googletesting. js you can get the following results: Set the casper option in the test environment: You must have pre-configured the casper option in the test environment. You can use the following method to update the attribute: casper. options. optionName = optionValue; // where optionName is obviusly the desired option name casper. options. clientScripts. push ("new-script.js"); advanced technology: Tester # begin () accepts a method or object to describe a suite, object attributes allow setting setUp () and tearDown () methods: // 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 parameters and options of test: The casperjs test command sets the passed arguments as file or directory paths, it recursively scans all passed parameters and looks for the corresponding '*. js 'or '*. coffee 'and add them to the stack. Note: When writing test cases, there are two important considerations: In a test file, you cannot create multiple Casper objects only after being parsed to Tester. done, the test case will actually execute the option using "--" as the identifier -- xunit = <filename> to record the test result to the xml file in xunit format -- direct or-verbose to print a log message go to the console -- log-level = <logLevel> and set the level of log messages displayed on the console -- auto-exit = no. When all tests are completed, the test stops, it is usually used to append and execute other operations. You can manually set exit to trigger the exit event: // $ casperjs test -- auto-exit = nocasper. test. on ("exit", function () {someTediousAsyncProcess (function () {casp Er. exit () ;}); added -- des = foo. js, bar. javascript introduces foo when executing the test file. js, bar. js -- pre = pre-test.js add pre-test.js before all test cases are executed -- post = post-test.js add post-test.js after all test cases are executed -- fail-fast end execution immediately after the first failure is generated -- concise creates a more concise output result -- no-colors command line instance without color: $ 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 Note: removed from version 1.1: The -- direct option has been renamed to -- verbose. Although -- direct can still work, it does not advocate output xunit results: casperjs can output results that can be parsed by xunit in xml format. It can be compatible with continuous integration tools such as jenkins. To save the test results, use the -- xunit option $ casperjs test googletesting. js -- xunit = log. xml: <? Xml version = "1.0" encoding = "UTF-8"?> <Testsuites duration = "1.249"> <testsuite errors = "0" failures = "0" name = "Google search retrieves 10 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 has been submitted" time = "0.017"/> <testcase classname = "googletesting" name = "google search for & quot; casperjs & quot; retrieves 10 or more results "time =" 0.001 "/> <system-out/> </testsuite> </testsuites> casperjs self-test: casperjs has its own unit test set, which is placed in the tests subdirectory. You can run it like this: $ casperjs selftest. One advantage of running this test set is to find bugs on your running platform, if it fails, submit a feedback in the file an issue on github or in the case of JS mailing-list to extend the 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 expand the capabilities of the casperjs testing framework, you can write your own runner method and expand the casper object.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.