Python Automation Scripts Examples Use Django And Selenium

Django is the most popular web framework in the python world. You can use it to create a website quickly and simply. Selenium is the most popular website automation testing framework, it can be used to implement website automation function testing in source code with coding language python, java, etc. This example will tell you how to set up and run Django and Selenium to make a development environment for website functional tests.

If you do not install Python Django and selenium modules, you need to install them first. You can run the command pip list in a terminal to verify the Python Django and selenium module installation. If the two modules do not exist in the list, follow the below steps to install them.

1. Use Pip To Install Django Python Module.

192:~ $ pip install django
Collecting django
  Downloading (7.3MB)
    100% |████████████████████████████████| 7.3MB 200kB/s 
Requirement already satisfied: pytz in ./anaconda3/lib/python3.6/site-packages (from django) (2018.4)
Installing collected packages: django
Successfully installed django-2.1

2. Use Pip To Install Selenium Python Module.

192:~ $ pip install selenium
Collecting selenium
  Downloading (898kB)
    100% |████████████████████████████████| 901kB 714kB/s 
Requirement already satisfied: urllib3 in ./anaconda3/lib/python3.6/site-packages (from selenium) (1.22)
Installing collected packages: selenium
Successfully installed selenium-3.14.0

3. Create Django Project And Startup Django Web Server.

  1. Open a terminal and input the below command in your working directory.
    $ startproject TodoList
  2. Run ls -l command, then you can find the directory TodoList under the current working folder.
  3. CD into the TodoList folder, there is a folder also named TodoList and a file The inside TodoList folder is the place where all these webserver project files are saved, the file is the Django webserver management file. The _pycache_ folder is the cache folder for all the project python source files.
  4. To start up the Django project web server, please run the below command.
    192:TodoList $ python runserver
    Performing system checks...
    System check identified no issues (0 silenced).
    You have 15 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
    Run 'python migrate' to apply them.
    August 30, 2018 - 16:01:58
    Django version 2.1, using settings 'TodoList.settings'
    Starting development server at
    Quit the server with CONTROL-C.
  5. Now the Django web server has been created and started up, open a web browser and access url to see the Django project welcome page.

4. Run Selenium In Python Code To Test Above Django Server.

  1. Now save the below code in a file and execute python in a terminal, you can get the error message in the console.
    from selenium import webdriver
    import time
    def assert_django_title():
        # Create the Firefox web browser
        browser = webdriver.Firefox(executable_path = '/Users/zhaosong/Documents/WorkSpace/tool/geckodriver')
        # Access the Django web server home page.
        # Assert the web page title, the web page title do not contains Djangl so you can see error message in the console.
        assert 'Django' in browser.title
        # Sleep 10 seconds.
        # Close and quit the Firefox web browser
    if __name__ == '__main__':

5. Use Python Unittest Module To Test Django Home Page Title.

  1. Python unittest module tests the Django home page title example source code.
    from selenium import webdriver
    import time
    import unittest
    class DjangoTest(unittest.TestCase):
        # This method is invoked when test case start.
        def setUp(self):
            # Create the Firefox browser when test case setup.
            self.browser = webdriver.Firefox(executable_path = '/Users/zhaosong/Documents/WorkSpace/tool/geckodriver')
        # This method is invoked when test case complete.    
        def tearDown(self):
            # Close and quit the Firefox browser when test case tear down.
        # This is the test method.    
        def testHomePage(self):
            # Get Django home page.
            # Sleep 10 seconds to wait for the page load.
            # Assert whether the web page title contains word Djangl or not.
            self.assertIn("Djangl", self.browser.title, 'Browser title do not contains Django')
    if __name__ == '__main__':
        # Runn all test case function.
  2. Run above python code will get the below error message in the console.
    FAIL: testHomePage (__main__.DjangoTest)
    Traceback (most recent call last):
      File "/Users/zhaosong/Documents/WorkSpace/", line 23, in testHomePage
        self.assertIn("Djangl", self.browser.title, 'Browser title do not contains Django')
    AssertionError: 'Djangl' not found in 'Django: the Web framework for perfectionists with deadlines.' : Browser title do not contains Django
    Ran 1 test in 15.181s
    FAILED (failures=1)


  1. Python 3 Unittest Html And Xml Report Example

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.