Road to create test suite for python Webdriver scripts

In this post I will show you how to create suite file for Webdriver python UnitTest.
Following are two webdriver python unit test script.
1. WebdriverTest1.py

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import unittest, time, re

class WebdriverTest1(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(30)
        self.base_url = "http://en.wikipedia.org"
       
    def test_Wikipedia(self):
        driver = self.driver
        driver.get(self.base_url + "/wiki/Main_Page")
        driver.find_element_by_id("searchInput").clear()
        driver.find_element_by_id("searchInput").send_keys("India")
        driver.find_element_by_id("searchButton").click()    
   
    def tearDown(self):
        self.driver.quit()       

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

2. WebdriverTest2.py:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import unittest, time, re

class WebdriverTest2(unittest.TestCase):
   
    def setUp(self):
        self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(30)
        self.base_url = "https://www.google.co.in/"       
   
    def test_Google(self):
        driver = self.driver
        driver.get(self.base_url + "/")
        driver.find_element_by_id("gbqfq").clear()
        driver.find_element_by_id("gbqfq").send_keys("Testing")
       
    def tearDown(self):
        self.driver.quit()
        self.assertEqual([], self.verificationErrors)

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

I have created above two test case one for google search and wikipedia search, below is suite file for both unit test case:

import sys
import unittest
import WebdriverTest2
import WebdriverTest1


class Test_Suite(unittest.TestCase):
  
    def test_main(self):
        
        # suite of TestCases
        self.suite = unittest.TestSuite()
        self.suite.addTests([            
            unittest.defaultTestLoader.loadTestsFromTestCase(WebdriverTest1.WebdriverTest1),
            unittest.defaultTestLoader.loadTestsFromTestCase(WebdriverTest2.WebdriverTest2),
                                
            ])
        runner = unittest.TextTestRunner()
        runner.run (self.suite)

import unittest
if __name__ == "__main__":
    unittest.main()

1. Import test case in suite file.
2. Create TestSuite() object.
3. Add all test (both) in created suite object.
4. Create TextTestRunner() object and call the run method

Execution: open command prompt go to root folder of suite.py file and run below command:

>>python suite.py


3 comments:

  1. Please help me out, I am receiving this error. Although all my scripts are in same folder.
    Traceback (most recent call last):
    File "C:\Users\bgh36128\Desktop\selenium\suite.py", line 3, in
    import test1
    ImportError: No module named test1

    ReplyDelete

Leave your comments, queries, suggestion I will try to provide solution