Kickstart Selenium with Python

selenium_python

     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

Prerequisites

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-by-step procedure

Step 1: Create a Python class AutomationBase to keep the logic to initiate the driver,

from selenium import webdriver

class AutomationBase:

def __init__(self):
self.driver = webdriver.Chrome(” path_to_your_chromedriver_executable “)
self.driver.maximize_window()

def start_automation(self):
return self.driver

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,

import unittest

from TestHelper.searchhelper import SearchHelp
from AutomationBase.automationbase import AutomationBase

class SearchTest(unittest.TestCase):

@classmethod
def setUpClass(cls):
start_session = AutomationBase()
cls.driver = start_session.start_automation()
# print(cls.driver)s

def test_load_application(self):
search_help = SearchHelp(self.driver)
search_help.load_browser(” http://www.google.com “)
assert “Google” in self.driver.title

def test_search(self):
search_help = SearchHelp(self.driver)
search_help.search_data(“journeyofquality”)

@classmethod
def tearDownClass(cls):
cls.driver.close()

Step 3: Create a helper class to support SearchTest class. Let say the class name as SearchHelp,

from selenium.webdriver.common.keys import Keys

class SearchHelp:

def __init__(self, driver):
self.driver = driver

def load_browser(self, browser_url):
self.driver.get(browser_url)

def search_data(self, search_value):
elem = self.driver.find_element_by_name(“q”)
elem.send_keys(search_value)
elem.send_keys(Keys.RETURN)

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
import os
import unittest

from AutomationReport.automationreport import HTMLTestRunner
from TestCases.searchtest import SearchTest

if __name__ == ‘__main__’:
if not os.path.exists(‘../Reports/’):
os.mkdir(‘../Reports/’)
now = datetime.now()
current_date_time = now.strftime(“%d%m%Y_%H%M%S”)
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(SearchTest))
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)
fp.close()

Step 5: The generated report is custom made extent report style. I would like to share a screenshot of the automation report,

test report python1

Below is the overall project structure of Selenium with Python,

Selenium 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!