3. Creating Test Cases Using Python and Unittest IN Selenium

Selenium is mostly used for writing test cases and BOT making. The selenium package itself doesn’t provide a testing tool. You can write test cases using Python’s unittest module.

I use unittest as the framework for testing purpose. Here is an example which uses unittest module. This is a test for google.com search functionality:
 
import unittest                    #python in-built library 
from selenium import webdriver     # import webdriver for choosing a web browser
from selenium.webdriver.common.keys import Keys  #Keys for sending keyboard buttons

class PythonOrgSearch(unittest.TestCase):  #In unittest, TestCase is the base class

    def setUp(self):                        #keep in mind it is case sensitive and fix 
        self.driver = webdriver.Firefox()    #OOPs style for writing codes

    def test_google(self):     # Your test cases start from here
        driver = self.driver
        driver.get("http://www.googel.com")
        elem = driver.find_element_by_name("q")
        elem.send_keys("TARLABS")
        elem.send_keys(Keys.ENTER)

    def tearDown(self):
        self.driver.close()

if __name__ == "__main__":
    unittest.main()
You can run the above test case from a shell like this:
python test1.py


Ran 1 test in 15.566s

OK

The above results shows that, the test has been successfully completed.

Explanation

import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

The unittest module is a built-in Python based on Java’s JUnit. This module provides the framework for organizing the test cases. The selenium.webdriver module provides all the WebDriver implementations.  

class PythonOrgSearch(unittest.TestCase):

The test case class is inherited(python OOPs Concept) from unittest.TestCase. Inheriting from TestCase class is the way to tell unittest module that, this is a test case:
 
def setUp(self):
    self.driver = webdriver.Firefox()

 NOTE: if you change the name of the setUp() to other name test cases may not run

The setUp is part of initialization, this method will get called before every test function which you are going to write in this test case class. Here you are creating the instance of Firefox WebDriver.
def test_google(self):
    driver = self.driver

This is the test case method. The first line inside this method create a local reference to the driver object created in setUp method.

driver.get("http://www.google.com")

The driver.get method will navigate to a page given by the URL.
 
elem = driver.find_element_by_name("q")
 
WebDriver offers a number of ways to find elements using one of the find_element_by_* methods. For example, the input text element can be located by its name attribute using find_element_by_name method.
 
elem.send_keys("TARLABS")
elem.send_keys(Keys.ENTER)

Sending keys is similar to entering keys using your keyboard. Special keys can be send using Keys class imported from selenium.webdriver.common.keys:
 
def tearDown(self):
    self.driver.close()

The tearDown method will get called after every test method. This is a place to do all cleanup actions. In the current method, the browser window is closed. You can also call quit method instead of close. The quit will exit entire browser, where as close will close a tab, but if it is just one tab, by default most browser will exit entirely.

Final lines are some fixed code to run the test suite:
if __name__ == "__main__":
    unittest.main() 

No comments: