Generating A HTML Report

Once you have a Selenium test run, you need to analyze the results as well. For that you need to have  all your test reports organized clearly, and run properly so that you have the visibility and the run reports all at one place so that you can take a look at it better.

Once of the most common disadvantages of Selenium is that it doesn’t allows you the reporting capability. You need to have a third party framework to help obtain a well detailed report of the tests that you run.

For Java, we have TestNG which serves this purpose. For Python, we have

  • HTMLTestRunner
  • Allure
  • Nose

which allows us to take out a well detailed report in HTML format.

 

Today we will see how we can use the HTMLTestRunner to generate a HTML report for our Selenium tests.

 

We will

  • Create a simple suite of test cases
  • Use HTMLTestRunner to generate an HTML report of the test cases in test suite.

 

Creating A Test Suite

A Test Suite is a collection of two or more test scripts, run together using a single runner. So, in a test suite, we would combine many test scripts and then call them together so that they are executed one by one.

Let us take two simple scripts .

__author__ = 'rahul'

import unittest
from selenium import webdriver

class MyWikiTestCase(unittest.TestCase):

    def setUp(self):
        self.driver = webdriver.Firefox()
    def test_Wiki(self):
        driver = self.driver
        driver.maximize_window()
        driver.get('http://en.wikipedia.org')

        driver.find_element_by_id('searchInput').clear()
        driver.find_element_by_id('searchInput').send_keys('Sachin Tendulkar')
        driver.find_element_by_id('searchButton').click()


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


if __name__ == '__main__':
    unittest.main()

This is a very simple script, where we are opening the wikipedia page and typing Sachin Tendulkar in search box of wiki webpage.

Let’s take another simple script

__author__ = 'rahul'

import unittest
from selenium import webdriver


class MyGoogleTest(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox()

    def test_GoogleTest(self):
        driver = self.driver
        driver.maximize_window()
        driver.get('https://www.youtube.com/')

        driver.find_element_by_id('masthead-search-term').clear()
        driver.find_element_by_id('masthead-search-term').send_keys('Metallica')
        driver.find_element_by_id('search-btn').click()



if __name__ == '__main__':
    unittest.main()

In this script, we’re opening the Youtube home page and typing Metallica in the search box.

Once we have both the scripts, let’s create a test suite for both these test cases

__author__ = 'rahul'

import unittest
import GoogleTest
import WikiTest
import os
import HTMLTestRunner

direct = os.getcwd()

class MyTestSuite(unittest.TestCase):

    def test_Issue(self):

        smoke_test = unittest.TestSuite()
        smoke_test.addTests([
            unittest.defaultTestLoader.loadTestsFromTestCase(WikiTest.MyWikiTestCase),
            unittest.defaultTestLoader.loadTestsFromTestCase(GoogleTest.MyGoogleTest),
        ])

        outfile = open(direct + "\SmokeTest.html", "w")

        runner1 = HTMLTestRunner.HTMLTestRunner(
            stream=outfile,
            title='Test Report',
            description='Smoke Tests'
        )

        runner1.run(smoke_test)





if __name__ == '__main__':
    unittest.main()

Now, we have our test suite. The suite takes the HTMLTestRunner extension and runs a tests and then creates a nice report out of it.

Let’s have a look at the report

Screenshot from 2015-11-07 15:56:01

It looks good. It’s not as fancy as the one provided by Allure, but it does the work. The HTMLTestRunner along with the test suite scripts are here in my repo.