- defining & ordering browsing navigation steps
- filling & submitting forms
- clicking & following links
- capturing screenshots of a page (or part of it)
- testing remote DOM
- logging events
- downloading resources, including binary ones
- writing functional test suites, saving results as JUnit XML
- scraping Web contents
django-casper is a sort of wrapper for casperjs, allowing us to run javacript/casper tests from Django's built-in test command, this is great, not only to facilitate the development process, but to take advantage of Django's own test runner to create mocks, stubs, fixtures, etc. for our front end testing.
We will need a running python and node instance with django and casperjs installed respectively. If you don't have it already on your box here is a quick howto for GNU/Linux
$ curl https://raw.github.com/creationix/nvm/v0.4.0/install.sh | sh
From nvm, install latest node version
$ nvm install latest
then open a new terminal or source ~/.zshrc or ~/.bashrc or whatever rc file for the shell you are using
Create a new virtualenv instance if you don't have virtualenv installed please refer to the official documentation http://virtualenv.readthedocs.org/en/latest/virtualenv.html Note: You can skip this step and install Django globally, but is not recommended
$ virtualenv django-casper && source django-casper/bin/activate
Now we are ready to install casperjs and django
$ pip install django
$ npm install -g casperjs
It is possible to install django-casper from pip (pip install django-casper), but we are going to use the following method, in order to get the example code for tests.
$ git clone email@example.com:dobarkod/django-casper.git
$ cd django-casper
$ python setup.py install
Django-casper comes with a testproject that implements some test tests.
$ cd testproject
$ python manage.py test testapp
This should run all the test included in the application.
Where are my tests?!!
Tests are divided in two parts, the django part where the backend stuff is prepared (fixture, backend mocks, etc) and the casper part where the actual tests are written. Lets see an example:
$ vim testapp/tests.py
from casper.tests import CasperTestCase
from django.contrib.auth.models import User
On this file we have one test that in turn calls the casper library passing the test casper should run on this case casper-tests/test.js. Inside this django test, we could add new data to the database, and test the result on the casper-tests/test.js test.
Lets see the content of the casper-tests/test.js test file
casper.test.comment('Casper+Django integration example');
var helper = require('../../../casper/jslib/djangocasper.js');
"There's a mention of django-casper on the page");
this.test.assertSelectorHasText('#messages p', 'Good times!',
"When the link is clicked, a message is added to the page");
We can observe, that the test is opening the root "/" page (helper.scenario('/'.... ), and then asserting the content of the page on the first function. The second function, clicks on a link on the page, and asserts that a message is added to the page.
These are basic tests, I encourage you to give it a try. Also take a look a Django dynamic fixture, a library to create dynamic test data for your django tests.