We discussed Selenium automation with C# and Kotlin in my previous article. I hope you enjoyed those articles. We know that Python is an interpreted, high-level, general-purpose programming language. Python came before Java and C# programming languages. Python 3.0, released in 2008, was a major revision of the language that is not completely backward-compatible, and much Python 2 code does not run unmodified on Python 3. In this article, we are going to discuss Selenium automation using Python 3.x.
Some of the Advantages of Python
- Python is capable of interacting with most of the other languages and platforms.
- Python provides a large standard library which reduces the length of code to be written significantly.
- Python language is developed under an OSI-approved open source license.
- Python offers excellent readability and uncluttered simple-to-learn syntax which helps beginners to utilize this programming language.
- Python has built-in list and dictionary data structures which can be used to construct fast runtime data structures.
- Python has clean object-oriented design, provides enhanced process control capabilities, and possesses strong integration and text processing capabilities and its own unit testing framework, all of which contribute to the increase in its speed and productivity.
Kickstart Your Selenium with Python
Following are the prerequisites to start Selenium with Python,
- Download the Python executable and install it. Here, I am using the Windows system. If you open https://www.python.org/downloads/ and you can see it will automatically detect your OS and you can download the latest version from this site.
- I am using JetBrains product PyCharm for scripting the python code. You can download the PyCharm Community Edition from below link, https://www.jetbrains.com/pycharm/download/#section=windows
- Create a new project in PyCharm and while creating the project the PyCharm will automatically detect the installed Python Interpreter.
- To work with selenium with python, you need to install the selenium library. To install the selenium library, you need to open the terminal in PyCharm and type pip install selenium. After a few seconds, the selenium library gets added to your new project and now you are ready to use the selenium actions.
Step 1: Create a Python class AutomationBase to keep the logic to initiate the driver,
from selenium import webdriver
self.driver = webdriver.Chrome(” path_to_your_chromedriver_executable “)
The start_automation method helps to start a chrome driver session.
Step 2: Create a Python test case class, let’s say SearchTest to keep the test case methods,
from TestHelper.searchhelper import SearchHelp
from AutomationBase.automationbase import AutomationBase
start_session = AutomationBase()
cls.driver = start_session.start_automation()
search_help = SearchHelp(self.driver)
search_help.load_browser(” http://www.google.com “)
assert “Google” in self.driver.title
search_help = SearchHelp(self.driver)
Step 3: Create a helper class to support SearchTest class. Let say the class name as SearchHelp,
from selenium.webdriver.common.keys import Keys
def __init__(self, driver):
self.driver = driver
def load_browser(self, browser_url):
def search_data(self, search_value):
elem = self.driver.find_element_by_name(“q”)
Step 4: Create a Python file testsuite to keep the logic to run the test cases and control the HTML report,
from datetime import datetime
from AutomationReport.automationreport import HTMLTestRunner
from TestCases.searchtest import SearchTest
if __name__ == ‘__main__’:
if not os.path.exists(‘../Reports/’):
now = datetime.now()
current_date_time = now.strftime(“%d%m%Y_%H%M%S”)
suite = unittest.TestSuite()
fp = open(“../Reports/AutomationReport_”+current_date_time+”.html”, ‘wb’)
runner = HTMLTestRunner(stream=fp,
title=’selenium automation report’,
description=’selenium automation report details:’)
runResult = runner.run(suite)
Step 5: The generated report is custom made extent report style. I would like to share a screenshot of the automation report,
Below is the overall project structure of Selenium with Python,
If you would like to know more details on the custom made HTML report, please post your comments to get more details. Try to use the above step-by-step procedure in your automation world and enjoy it.
make it perfect!