Django views automated testing with selenium

Selenium

Yusuf
Yusuf
Mar 4, 2015 · 2 min read

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.

Installation

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.

Demo

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.

### Links

https://docs.djangoproject.com/en/1.4/topics/testing/#live-test-server
https://code.google.com/p/chromedriver/downloads/list
https://selenium-python.readthedocs.org/

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store