Creating Utility Classes for Common Selenium Functions

 ✅ Key Features of the Utility Class

You can include methods like:


Finding elements


Clicking elements


Sending text


Waiting for elements


Taking screenshots


Handling dropdowns, alerts, and frames


🧱 Sample Utility Class in Python

python

Copy

Edit

from selenium.webdriver.support.ui import WebDriverWait

from selenium.webdriver.support import expected_conditions as EC

from selenium.webdriver.common.by import By

from selenium.common.exceptions import TimeoutException, NoSuchElementException

from selenium.webdriver.support.ui import Select

import time


class SeleniumUtils:

    def __init__(self, driver, timeout=10):

        self.driver = driver

        self.timeout = timeout


    def find_element(self, by, value):

        try:

            return WebDriverWait(self.driver, self.timeout).until(

                EC.presence_of_element_located((by, value))

            )

        except TimeoutException:

            print(f"Element not found: {value}")

            return None


    def click_element(self, by, value):

        try:

            element = self.find_element(by, value)

            if element:

                element.click()

        except Exception as e:

            print(f"Error clicking element: {e}")


    def send_keys(self, by, value, text):

        try:

            element = self.find_element(by, value)

            if element:

                element.clear()

                element.send_keys(text)

        except Exception as e:

            print(f"Error sending keys: {e}")


    def wait_for_element_visible(self, by, value):

        try:

            return WebDriverWait(self.driver, self.timeout).until(

                EC.visibility_of_element_located((by, value))

            )

        except TimeoutException:

            print(f"Element not visible: {value}")

            return None


    def take_screenshot(self, filename):

        try:

            self.driver.save_screenshot(filename)

        except Exception as e:

            print(f"Error taking screenshot: {e}")


    def select_dropdown_by_visible_text(self, by, value, text):

        try:

            element = self.find_element(by, value)

            if element:

                select = Select(element)

                select.select_by_visible_text(text)

        except Exception as e:

            print(f"Dropdown selection failed: {e}")


    def switch_to_frame(self, frame_reference):

        try:

            self.driver.switch_to.frame(frame_reference)

        except Exception as e:

            print(f"Error switching to frame: {e}")


    def switch_to_default_content(self):

        self.driver.switch_to.default_content()


    def handle_alert_accept(self):

        try:

            WebDriverWait(self.driver, self.timeout).until(EC.alert_is_present())

            alert = self.driver.switch_to.alert

            alert.accept()

        except TimeoutException:

            print("No alert present to accept.")

✅ Example Usage

python

Copy

Edit

from selenium import webdriver

from selenium.webdriver.common.by import By


driver = webdriver.Chrome()

driver.get("https://example.com")


utils = SeleniumUtils(driver)


utils.send_keys(By.NAME, "username", "my_user")

utils.send_keys(By.NAME, "password", "my_pass")

utils.click_element(By.ID, "login-button")

utils.take_screenshot("screenshot.png")


driver.quit()

🛠️ You Can Extend It Further By Adding:

Logging instead of print()


Custom exceptions


Retry mechanisms


Browser-specific handling


Implicit/explicit wait toggles


Mobile testing utility support

Learn Selenium JAVA Course in Hyderabad

Read More

Java OOP Concepts for Selenium Testers

Working with Collections in Java for Selenium Testing

Handling Exceptions in Selenium Test Scripts

Using Loops and Conditions to Control Test Flow

Visit Our IHUB Talent Training in Hyderabad

Get Directions

Comments

Popular posts from this blog

How to Install and Set Up Selenium in Python (Step-by-Step)

Tosca for API Testing: A Step-by-Step Tutorial

Waits in Playwright: Explicit, Implicit, and Auto