0
Feb. 18, 2017  
In Articles by Iunary

In this short article we gonna learn how to automate your testing against django views using selenium.

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.

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.

from django.test import LiveServerTestCase
from selenium import webdriver
from selenium.webdriver.common.keys import Keys


class AccountTestCase(LiveServerTestCase):

    def setUp(self):
        self.selenium = webdriver.Firefox()
        super(AccountTestCase, self).setUp()

    def tearDown(self):
        self.selenium.quit()
        super(AccountTestCase, self).tearDown()

    def test_register(self):
        selenium = self.selenium
        #Opening the link we want to test
        selenium.get('http://127.0.0.1:8000/accounts/register/')
        #find the form element
        first_name = selenium.find_element_by_id('id_first_name')
        last_name = selenium.find_element_by_id('id_last_name')
        username = selenium.find_element_by_id('id_username')
        email = selenium.find_element_by_id('id_email')
        password1 = selenium.find_element_by_id('id_password1')
        password2 = selenium.find_element_by_id('id_password2')

        submit = selenium.find_element_by_name('register')

        #Fill the form with data
        first_name.send_keys('Yusuf')
        last_name.send_keys('Unary')
        username.send_keys('unary')
        email.send_keys('[email protected]')
        password1.send_keys('123456')
        password2.send_keys('123456')

        #submitting the form
        submit.send_keys(Keys.RETURN)

        #check the returned result
        assert 'Check your email' in selenium.page_source

As we see, the code is so simple and all that’s is running our test.

Selenium demo

Links


0 Comments

There are currently no comments. You can be first!

Sign in