Django views automated testing with selenium
Selenium is set of tools for automating browsers. It provides a lot of options and APIs for automating user interaction with the web applications. It is used for testing web applications, web scraping and automating boring repetitive tasks.
Selenium supports automation on all the major browsers including Firefox, Internet Explorer,Google Chrome, Safari, and Opera. Selenium can be also ran on android devices.
In our story we gonna use selenium WebDriver with Firefox for automating a django views test same as we were doing it manually.
Selenium WebDriver is available for various programming languages. on our case we gonna use python. To install the selenium package in a simple way, we gonna use pip.
$ sudo pip install selenium
Now our package is installed. Then, we gonna demonstrate how to write a functional test for a django project.
Our environment is ready now for writing our tests.
For a quick demo i gonna write an automated test for a the account app to create a new user account.
We will inherit LiveServerTestCase class that gonna run automatically a test server in the background. Then, our selenium tests will be ran against that server. We can run our test simply by using the following command by replacing appname with your django app name.
$ python manage.py test appname
In each django app there is a tests.py file dedicated to write and run our test. Im using the follow snippet in my tests file.
As we see, the code is so simple and all that’s is running our test.