Selenium Test case in Python with HTMLTestRunner

Source code on GitHub

In  this tutorial , We are going to discuss about how Python programming language can be use to Test Web Application with the Selenium and HTMLTestRunner module.

Getting Started

I am assuming that Python and Pip are already installed on your Windows/Linux machine( If not please use this guide to install pip on windows). Install selenium and unittest by using below mentioned commands.

[html]
pip install -U selenium
pip install -U unittest
[/html]

Project Structure

Below is the image of the project structure that i have used.

 

We have following files in this project :-

  • HTMLTestRunner.py –  Library file in python to generate the HTML report after Test Case execution.
  • MainClass.py – Mainclass file to run the two UnitTest_Demo,UnitTest_Demo2.py test cases.
  • UnitTest_Demo,UnitTest_Demo2.py – Python Files with Test Cases  in to verify a web application.

Step1 : In the UniTest_Demo , We add two cases as .

Importing the Selenium module and unit test(Same as JUnit in Java) in you project and adding the following class with two function to verify the results as assert statements.

[python]
import unittest
from selenium import webdriver

class SearchTest(unittest.TestCase):
@classmethod
def setUp(cls):
cls.driver = webdriver.Firefox()
cls.driver.implicitly_wait(20)
cls.driver.maximize_window()
cls.driver.get("http://demo.magentocommerce.com")

def test_1(self):
self.search_field = self.driver.find_element_by_name("q")
self.search_field.send_keys("Phones")
self.search_field.submit()
self.button = self.driver.find_element_by_xpath(".//*[@id=’search_mini_form’]/div[1]/button")
self.button.click()
self.products = self.driver.find_elements_by_css_selector("h2.product-name")
self.assertEqual(len(self.products), 3)
print(len(self.products))

def test_2(self):
self.search_field = self.driver.find_element_by_name("q")
self.search_field.send_keys("Pant")
self.search_field.submit()
self.button = self.driver.find_element_by_xpath(".//*[@id=’search_mini_form’]/div[1]/button")
self.button.click()
self.products = self.driver.find_elements_by_css_selector("h2.product-name")
self.assertEqual(len(self.products), 4)
print(len(self.products))

@classmethod
def tearDown(cls):
cls.driver.quit()

if __name__ == "__main__":
unittest.main(verbosity=2)</pre>
[/python]

Now try to code other Test function(Not Similer to above one:P) in other file UnitTestDemo_2.py and Now Call these Test Functions in our Test Suite in the MainClass.py

[python]
__author__ = ‘WP8Dev’
import unittest
from UnitTest_Demo import SearchTest
from UnitTest_Demo2 import SearchTest1
import os
import HTMLTestRunner

def main():
search_tests = unittest.TestLoader().loadTestsFromTestCase(SearchTest)
search_tests2 = unittest.TestLoader().loadTestsFromTestCase(SearchTest1)
## Put them in the Array
smoke_tests = unittest.TestSuite([search_tests, search_tests2])
## File
dir = os.getcwd()
outfile = open(dir + "SmokeTestReport.html", "w")
runner = HTMLTestRunner.HTMLTestRunner(stream = outfile,title = ‘Test Report’,description = ‘Smoke Tests’)
runner.run(smoke_tests)

if __name__ == "__main__":
main()
[/python]

Now run the Test Suite from the MainClass.py file using command prompt :-

[java]
python MainClass.py
[/java]

And you will see a Nice Looking HTML File with Results

Thats all from my side .

Let me know what you think above Tutorial  by commenting below.

If you have any doubt, Fee free to ping me at pramoddutta@live.com

 

[contact-form-7 id=”117″ title=”Contact form 1″]

 

One thought on “Selenium Test case in Python with HTMLTestRunner

Leave a Reply

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.