August 09, 2020

Selenium Python Tutorial 14 | Selenium Python Unittest Framework with POM | Selenium Unit Testing

This is the next tutorial in the Selenium Python Tutorials for Beginners. It deals with Python Automation Testing with Selenium WebDriver Python. This Selenium Python beginner tutorial deals with the Selenium Python Unittest framework and Selenium unit testing. First, view the Selenium Python Unit Test Tutorial. Then read on.

Here is my Selenium Python example, which is based on the example that I showed in my Selenium Python tutorial 13. This Page Object Model (POM) based Selenium Python example uses the Python unit test framework to run the test methods in a test class. I put explanations in the comments within the code (lines starting with #).

# Test_Cases>WikipediaTestClass1.py
# Selenium WebDriver Python coding
# Import unittest library for working with Selenium Unit Testing Python.
import unittest
from selenium import webdriver
# Import the page object files in the Pages directory.
# The POM files are MainPage.py and EnglishPage.py. Their code is in Selenium Python tutorial 13.
from Pages.MainPage import MainPage
from Pages.EnglishPage import EnglishPage

class WikipediaTestClass1(unittest.TestCase):
    exec_path = r"E:\Training\SeleniumPython\Downloads\geckodriver-v0.26.0-win32\geckodriver.exe"
    base_URL = "https://www.wikipedia.org/"

    # First independent test method to test the main page
    def test_main_page(self):
        driver = webdriver.Firefox(executable_path=self.exec_path)
        driver.get(self.base_URL)
        mp = MainPage(driver)
        mp.test_title()

    # Second independent test method to test the english page
    def test_english_page(self):
        driver = webdriver.Firefox(executable_path=self.exec_path)
        driver.get(self.base_URL)
        mp = MainPage(driver)
        mp.click_english_link()
        ep = EnglishPage(driver)
        ep.search_text()

# Run all test methods using the unit test library.
# In PyCharm, click the green arrow next to this if statement and then click Run 'WikipediaTestClass1'.
if __name__ == "__main__": unittest.main()

Next, in this Selenium WebDriver Python tutorial, let us see the Selenium Python example to skip test methods using Selenium Python. I modified the above Python script to skip test methods.

# Test_Cases>WikipediaTestClass2.py
# Selenium WebDriver Python coding
import unittest
from selenium import webdriver
from Pages.MainPage import MainPage
from Pages.EnglishPage import EnglishPage

class WikipediaTestClass2(unittest.TestCase):
    # run_all_test_cases variable is used to skip unit tests conditionally.
    run_all_test_cases = False
    exec_path = r"E:\Training\SeleniumPython\Downloads\geckodriver-v0.26.0-win32\geckodriver.exe"
    base_URL = "https://www.wikipedia.org/"

    # If you want to skip unit test with condition, use the skipIf unit test annotation.
    @unittest.skipIf(run_all_test_cases==False, "This is a medium priority test case.")
    def test_main_page(self):
        driver = webdriver.Firefox(executable_path=self.exec_path)
        driver.get(self.base_URL)
        mp = MainPage(driver)
        mp.test_title()

    # If you want to skip unit test, uncomment the unit test annotation.
 # @unittest.SkipTest
    def test_english_page(self):
        driver = webdriver.Firefox(executable_path=self.exec_path)
        driver.get(self.base_URL)
        mp = MainPage(driver)
        mp.click_english_link()
        ep = EnglishPage(driver)
        ep.search_text()

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

Finally, let us see the Selenium Python example with unit testing setup method and unit testing teardown method. I added these unit test special methods, the unit testing set up method and and unit testing tear down method to the above Python script.

# Test_Cases>WikipediaTestClass3.py
# Selenium WebDriver Python coding
import unittest
from selenium import webdriver
from Pages.MainPage import MainPage
from Pages.EnglishPage import EnglishPage

class WikipediaTestClass3(unittest.TestCase):
    exec_path = r"E:\Training\SeleniumPython\Downloads\geckodriver-v0.26.0-win32\geckodriver.exe"
    base_URL = "https://www.wikipedia.org/"
    driver = webdriver.Firefox(executable_path=exec_path)

    # unit test setUp method runs before every test method. 
    #  Notice the @classmethod unit test annotation.
    @classmethod
    def setUp(self):
        print("This method runs before every test method.")
        self.driver.get(self.base_URL)

    # unit test tearDown method runs after every test method. 
    #  Notice the @classmethod unit test annotation.
    @classmethod
    def tearDown(self):
        print ("This method runs after every test method.")

    def test_main_page(self):
        mp = MainPage(self.driver)
        mp.test_title()

    def test_english_page(self):
        mp = MainPage(self.driver)
        mp.click_english_link()
        ep = EnglishPage(self.driver)
        ep.search_text()

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

Want to see the Python unit test library run the above Selenium Python examples? Then view my Python Selenium UnitTest Tutorial and Selenium Python Unit Testing with POM Tutorial. Thank you.

3 comments:

  1. Hey Nice blog keep it! up, your blog is very helpful for every person to learn software testing & Thanks for such a knowledgeable post.
    Software Testing Services
    Software Testing Company
    Software Testing Companies in USA
    QA Testing Companies
    Software Testing Services in USA

    ReplyDelete
  2. This is such a great resource that you are providing and you give it away for free. This is really a nice and informative, containing all information and also has a great impact on the new technology.
    Herbie is the foremost Chatbot Companies in UAE. Our Chatbot Development Companies. We are specialized in AI Conversational Chatbots and Virtual Assistant Chatbot. Herbie is most intelligent Virtual Assistant ever developed by AI Chatbot Companies

    ReplyDelete
  3. This is really awesome and full of knowledge. Skalable Technologies offers software with expertise in Microsoft and Oracle-NetSuite technologies.

    ReplyDelete

Note: Only a member of this blog may post a comment.