Selenium Testing in Django

by Shahid Ayoub

Selenium automates browsers. That’s it! What you do with that power is entirely up to you. Primarily, it is for automating web applications for testing purposes, but is certainly not limited to just that. Boring web-based administration tasks can also be automated. Django provides special support for Selenium framework which can be elaborated using LiveServerTestCase.

LiveServerTestCase launches a live Django server in the background on setup, and shuts it down on teardown. This allows use of automated test client such as Selenium. For Django, we can try selenium by installing its package:

$ pip install selenium

The default server address is ‘localhost:8081’, we can change the port number if default port is already taken using the below mentioned command:

$ python test --liveserver=localhost:8082

Now let us take a look at the working of LiveServerTestCase by automating login of a user into an application and adding an employee in the application. Below mentioned is the code for the automation of tests:


from django.test import LiveServerTestCase
from selenium.webdriver.firefox.webdriver import WebDriver

class SeleniumTests(LiveServerTestCase):


def setUpClass(cls):</span>

super(SeleniumTests, cls).setUpClass()

cls.selenium = WebDriver()


def tearDownClass(cls):


super(SeleniumTests, cls).tearDownClass()

def test_addEmployee(self):

''' Login into the application'''

self.selenium.get('%s%s' % (self.live_server_url, '/'))

username_input = self.selenium.find_element_by_name("username")


password_input = self.selenium.find_element_by_name("password")




''' Adding Employee'''

empName_input = self.selenium.find_element_by_name("empName")


designation_input = self.selenium.find_element_by_name("designation")


employ_id_input = self.selenium.find_element_by_name("employ_id")




We have to add the above mentioned code to the file of app in the django project. And it can be executed by using the following command on the command line:

$ ./ test app.tests.SeleniumTests.test_addEmployee

This code will automatically open Firefox.  Proceed to the login page, enter credentials and click on “Log in” button. Soon after, it will move to add employee page, enter employee information and click on “Add Employee” button to submit information. We can use any other browser if Firefox is not installed, selenium offers support to other drivers as well.  This is just a tiny fraction of what selenium is capable of! One has to dive deep in order to understand the full potential of selenium.

Note: This blog is intended for developers who already have some knowledge of unit testing in django.


To learn more about

Contact Us

Leave a Reply

Your email address will not be published. Required fields are marked *

Tools & Practices

Tools and Technologies we use at Applied

Contact us now

Popular Posts