`_.
.. seealso:: `assertFalsy()`_
.. index:: Type
``assertType()``
-------------------------------------------------------------------------------
**Signature:** ``assertType(mixed input, String type[, String message])``
Asserts that the provided input is of the given type::
casper.test.begin('assertType() tests', 1, function suite(test) {
test.assertType(42, "number", "Okay, 42 is a number");
test.assertType([1, 2, 3], "array", "We can test for arrays too!");
test.done();
});
.. note:: Type names are always expressed in lower case.
.. index:: InstanceOf
``assertInstanceOf()``
-------------------------------------------------------------------------------
**Signature:** ``assertInstanceOf(mixed input, Function constructor[, String message])``
.. versionadded:: 1.1
Asserts that the provided input is of the given constructor::
function Cow() {
this.moo = function moo() {
return 'moo!';
};
}
casper.test.begin('assertInstanceOf() tests', 2, function suite(test) {
var daisy = new Cow();
test.assertInstanceOf(daisy, Cow, "Ok, daisy is a cow.");
test.assertInstanceOf(["moo", "boo"], Array, "We can test for arrays too!");
test.done();
});
.. index:: URL
``assertUrlMatch()``
-------------------------------------------------------------------------------
**Signature:** ``assertUrlMatch(Regexp pattern[, String message])``
Asserts that the current page url matches the provided RegExp pattern::
casper.test.begin('assertUrlMatch() tests', 1, function(test) {
casper.start('http://www.google.fr/', function() {
test.assertUrlMatch(/^http:\/\//, 'google.fr is served in http://');
}).run(function() {
test.done();
});
});
.. index:: DOM
``assertVisible()``
-------------------------------------------------------------------------------
**Signature:** ``assertVisible(String selector[, String message])``
Asserts that at least one element matching the provided :ref:`selector expression
` is visible::
casper.test.begin('assertVisible() tests', 1, function(test) {
casper.start('http://www.google.fr/', function() {
test.assertVisible('h1');
}).run(function() {
test.done();
});
});
.. seealso::
- `assertAllVisible()`_
- `assertNotVisible()`_
.. index:: DOM
``assertAllVisible()``
-------------------------------------------------------------------------------
**Signature:** ``assertAllVisible(String selector[, String message])``
Asserts that all elements matching the provided :ref:`selector expression ` are visible::
casper.test.begin('assertAllVisible() tests', 1, function(test) {
casper.start('http://www.google.fr/', function() {
test.assertAllVisible('input[type="submit"]');
}).run(function() {
test.done();
});
});
.. seealso::
- `assertVisible()`_
- `assertNotVisible()`_
.. _tester_begin:
.. index:: Test suite, planned tests, Asynchronicity, Termination
``begin()``
-------------------------------------------------------------------------------
**Signatures:**
- ``begin(String description, Number planned, Function suite)``
- ``begin(String description, Function suite)``
- ``begin(String description, Number planned, Object config)``
- ``begin(String description, Object config)``
.. versionadded:: 1.1
Starts a suite of ```` tests (if defined). The ``suite`` callback will get the current ``Tester`` instance as its first argument::
function Cow() {
this.mowed = false;
this.moo = function moo() {
this.mowed = true; // mootable state: don't do that
return 'moo!';
};
}
// unit style synchronous test case
casper.test.begin('Cow can moo', 2, function suite(test) {
var cow = new Cow();
test.assertEquals(cow.moo(), 'moo!');
test.assert(cow.mowed);
test.done();
});
.. note::
The ``planned`` argument is especially useful in case a given test script is abruptly interrupted leaving you with no obvious way to know it and an erroneously successful status.
A more asynchronous example::
casper.test.begin('Casperjs.org is navigable', 2, function suite(test) {
casper.start('http://casperjs.org/', function() {
test.assertTitleMatches(/casperjs/i);
this.clickLabel('Testing');
});
casper.then(function() {
test.assertUrlMatches(/testing\.html$/);
});
casper.run(function() {
test.done();
});
});
.. important::
`done()`_ **must** be called in order to terminate the suite. This is specially important when doing asynchronous tests so ensure it's called when everything has actually been performed.
.. seealso:: `done()`_
.. _tester_begin_configuration:
``Tester#begin()`` also accepts a test configuration object, so you can add ``setUp()`` and ``tearDown()`` methods::
// cow-test.js
casper.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();
}
});
.. index:: Colors
``colorize()``
-------------------------------------------------------------------------------
**Signature:** ``colorize(String message, String style)``
Render a colorized output. Basically a proxy method for ``Casper.Colorizer#colorize()``.
``comment()``
-------------------------------------------------------------------------------
**Signature:** ``comment(String message)``
Writes a comment-style formatted message to stdout::
casper.test.comment("Hi, I'm a comment");
.. _tester_done:
.. index:: Test suite, Asynchronicity, Termination, done()
``done()``
-------------------------------------------------------------------------------
**Signature:** ``done()``
.. versionchanged:: 1.1 ``planned`` parameter is deprecated
Flag a test suite started with `begin()`_ as processed::
casper.test.begin('my test suite', 2, function(test) {
test.assert(true);
test.assertNot(false);
test.done();
});
More asynchronously::
casper.test.begin('Casperjs.org is navigable', 2, function suite(test) {
casper.start('http://casperjs.org/', function() {
test.assertTitleMatches(/casperjs/i);
this.clickLabel('Testing');
});
casper.then(function() {
test.assertUrlMatches(/testing\.html$/);
});
casper.run(function() {
test.done();
});
});
.. seealso:: `begin()`_
``error()``
-------------------------------------------------------------------------------
**Signature:** ``error(String message)``
Writes an error-style formatted message to stdout::
casper.test.error("Hi, I'm an error");
.. index:: Test failure
``fail()``
-------------------------------------------------------------------------------
**Signature:** ``fail(String message [, Object option])``
Adds a failed test entry to the stack::
casper.test.fail("Georges W. Bush");
casper.test.fail("Here goes a really long and expressive message", {name:'shortfacts'});
.. seealso:: `pass()`_
``formatMessage()``
-------------------------------------------------------------------------------
**Signature:** ``formatMessage(String message, String style)``
Formats a message to highlight some parts of it. Only used internally by the tester.
``getFailures()``
-------------------------------------------------------------------------------
**Signature:** ``getFailures()``
.. versionadded:: 1.0
.. deprecated:: 1.1
Retrieves failures for current test suite::
casper.test.assertEquals(true, false);
require('utils').dump(casper.test.getFailures());
casper.test.done();
That will give something like this:
.. code-block:: text
$ casperjs test test-getFailures.js
Test file: test-getFailures.js
FAIL Subject equals the expected value
# type: assertEquals
# subject: true
# expected: false
{
"length": 1,
"cases": [
{
"success": false,
"type": "assertEquals",
"standard": "Subject equals the expected value",
"file": "test-getFailures.js",
"values": {
"subject": true,
"expected": false
}
}
]
}
FAIL 1 tests executed, 0 passed, 1 failed.
Details for the 1 failed test:
In c.js:0
assertEquals: Subject equals the expected value
.. note::
In CasperJS 1.1, you can recorded test failures by listening to the tester ``fail`` event::
var failures = [];
casper.test.on("fail", function(failure) {
failures.push(failure);
});
``getPasses()``
-------------------------------------------------------------------------------
**Signature:** ``getPasses()``
.. versionadded:: 1.0
.. deprecated:: 1.1
Retrieves a report for successful test cases in the current test suite::
casper.test.assertEquals(true, true);
require('utils').dump(casper.test.getPasses());
casper.test.done();
That will give something like this::
$ casperjs test test-getPasses.js
Test file: test-getPasses.js
PASS Subject equals the expected value
{
"length": 1,
"cases": [
{
"success": true,
"type": "assertEquals",
"standard": "Subject equals the expected value",
"file": "test-getPasses.js",
"values": {
"subject": true,
"expected": true
}
}
]
}
PASS 1 tests executed, 1 passed, 0 failed.
.. note::
In CasperJS 1.1, you can recorded test successes by listening to the tester ``success`` event::
var successes = [];
casper.test.on("success", function(success) {
successes.push(success);
});
``info()``
-------------------------------------------------------------------------------
**Signature:** ``info(String message)``
Writes an info-style formatted message to stdout::
casper.test.info("Hi, I'm an informative message.");
.. index:: Test success
``pass()``
-------------------------------------------------------------------------------
**Signature:** ``pass(String message)``
Adds a successful test entry to the stack::
casper.test.pass("Barrack Obama");
.. seealso:: `fail()`_
``renderResults()``
-------------------------------------------------------------------------------
**Signature:** ``renderResults(Boolean exit, Number status, String save)``
Render test results, save results in an XUnit formatted file, and optionally exits phantomjs::
casper.test.renderResults(true, 0, 'test-results.xml');
.. note::
This method is not to be called when using the ``casperjs test`` command (see documentation for :doc:`testing <../testing>`), where it's done automatically for you.
``setUp()``
-------------------------------------------------------------------------------
**Signature:** ``setUp([Function fn])``
Defines a function which will be executed before every test defined using `begin()`_::
casper.test.setUp(function() {
casper.start().userAgent('Mosaic 0.1');
});
To perform asynchronous operations, use the ``done`` argument::
casper.test.setUp(function(done) {
casper.start('http://foo').then(function() {
// ...
}).run(done);
});
.. warning::
Don't specify the ``done`` argument if you don't intend to use the method asynchronously.
.. seealso:: `tearDown()`_
``skip()``
-------------------------------------------------------------------------------
**Signature:** ``skip(Number nb, String message)``
Skips a given number of planned tests::
casper.test.begin('Skip tests', 4, function(test) {
test.assert(true, 'First test executed');
test.assert(true, 'Second test executed');
test.skip(2, 'Two tests skipped');
test.done();
});
``tearDown()``
-------------------------------------------------------------------------------
**Signature:** ``tearDown([Function fn])``
Defines a function which will be executed after every test defined using `begin()`_::
casper.test.tearDown(function() {
casper.echo('See ya');
});
To perform asynchronous operations, use the ``done`` argument::
casper.test.tearDown(function(done) {
casper.start('http://foo/goodbye').then(function() {
// ...
}).run(done);
});
.. warning::
Don't specify the ``done`` argument if you don't intend to use the method asynchronously.
.. seealso:: `setUp()`_