OOPs concepts in Java – Mastering Selenium with Polymorphism

Object-Oriented Programming (OOP) in Java plays a crucial role in building scalable and maintainable Selenium automation frameworks. Core OOP principles such as Inheritance, Polymorphism, and Encapsulation significantly enhance code reusability, flexibility, and structure.

In this article, we will focus on how Polymorphism is applied within Selenium automation frameworks. If you’d like to explore Inheritance in detail, refer to: OOP Concepts in Java – Mastering Selenium with Inheritance.

Polymorphism, meaning “many forms,” allows methods to perform different behaviors based on the context. It is broadly categorized into:

Method Overloading (Compile-Time Polymorphism):
Multiple methods with the same name but different parameter lists. The method to execute is determined at compile time.

Method Overriding (Runtime Polymorphism):
A subclass provides a specific implementation of a method already defined in its superclass. The method resolution happens at runtime based on the object type.

Below are examples demonstrating both Method Overloading and Method Overriding in the context of Selenium automation:

Method Overloading

In a Selenium automation framework, method overloading can be effectively used to handle dropdown interactions in a flexible and reusable way. For example, the Selenium Select class provides multiple ways to choose an option, such as selectByValue() and selectByIndex().

To simplify this, we can create overloaded utility methods that allow testers to select dropdown values using either a String value or an index, depending on the test scenario.

In the example below, we define a method named selectValue() in a utility class:

  • The first method accepts parameters WebElement element and String value, and internally uses selectByValue().
  • The second method accepts WebElement element and int index, and uses selectByIndex().

This approach improves code readability and reusability by allowing the same method name to handle different input types.

In the test implementation:

  • A test class (Class1) is created where the WebDriver instance (driver) is initialized.
  • The application URL https://practice.expandtesting.com/dropdown is launched.
  • A utility class (e.g., CustomMethods) is instantiated.
  • The overloaded selectValue() methods are then called by passing the dropdown WebElement (identified using its locator, such as ID) along with either a String value or an index, based on the test case requirement.

This use of method overloading demonstrates compile-time polymorphism and helps create cleaner, more maintainable Selenium automation frameworks.

ExampleScript:

Class1 Test Class

package tests;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;

import utilities.CustomMethods;

public class Class1 extends BaseClass {

WebDriver driver = null;
CustomMethods customMethod = new CustomMethods();

@Test
public void selectOptions() {

    driver = new ChromeDriver();
    driver.get("https://practice.expandtesting.com/dropdown");

    // Select by value (String)
    WebElement elem = driver.findElement(By.id("dropdown"));
    customMethod.selectValue(elem, "1");

    // Scroll and select by index (int)
    WebElement elemCountry = driver.findElement(By.id("country"));
    customMethod.scrollToElement(driver, elemCountry);
    customMethod.selectValue(elemCountry, 1);
}

}

CustomMethods Class from Utitlities package:

package utilities;

import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;

public class CustomMethods {

// Overloaded method - Select by value (String)
public void selectValue(WebElement element, String value) {
    Select select = new Select(element);
    select.selectByValue(value);
}

// Overloaded method - Select by index (int)
public void selectValue(WebElement element, int index) {
    Select select = new Select(element);
    select.selectByIndex(index);
}

// Scroll to element using JavaScript
public void scrollToElement(WebDriver driver, WebElement element) {
    JavascriptExecutor js = (JavascriptExecutor) driver;
    js.executeScript(
        "arguments[0].scrollIntoView({behavior: 'smooth', block: 'center', inline: 'nearest'});",
        element
    );

    try {
        Thread.sleep(500); // small pause for smooth scrolling
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
    }
}

}

Method Overriding

In the example below, the Execution class extends the Base class and overrides one of its methods. Although the method is defined in the Base class, the Execution class provides a customized implementation, which will be executed at runtime.

This demonstrates runtime polymorphism, where the method call is resolved based on the actual object type rather than the reference type.

Example Script

Base Class:

package tests;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.BeforeSuite;

public class BaseClass {

@Override
public void execution() {
    System.out.print("Class1 got executed");
}

}

Class1 Test Class:

package tests;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;

import utilities.CustomMethods;

public class Class1 extends BaseClass {

WebDriver driver = null;
CustomMethods customMethod = new CustomMethods();

@Test
public void selectOptions() {

    driver = new ChromeDriver();
    driver.get("https://practice.expandtesting.com/dropdown");

    execution(); // calling overridden method
}

@Override
public void execution() {
    System.out.println("Class1 execution method overridden and executed");
}

}

For You Tube Video Click on: OOP Concepts in Java | Master Polymorphism in Selenium Framework 

0 0 votes
Article Rating
Subscribe
Notify of
guest

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments

[…] In this article, we will focus on how Inheritance is used in Selenium with Java to promote code reusability, reduce duplication, and create a structured automation framework. If you’d like to explore Polymorphism in detail, refer to: OOP Concepts in Java – Mastering Selenium with Polymorphism. […]

Subscribe to our YouTube Channel: Testing Talks Latest
Testingtalkslatest.com - A project by CreativeHub IT Solutions.
Contact Us At: support@testingtalkslatest.com
Our Partner websites - Classified Hub , CodesToolbox
Scroll to Top
1
0
Would love your thoughts, please comment.x
()
x