Java OOP Concepts for Selenium Testers
Object-Oriented Programming (OOP) helps Selenium testers write clean, reusable, maintainable, and scalable automation frameworks. Understanding OOP is essential for building frameworks like Page Object Model (POM), Data-Driven, Hybrid, and Keyword-Driven frameworks.
Below are the major OOP concepts with easy examples related to Selenium.
1. Class and Object
Class
A class is a blueprint or template that defines properties and behaviors.
Object
An object is an instance of a class.
Example in Selenium
public class LoginPage {
WebDriver driver;
By username = By.id("user");
By password = By.id("pass");
By loginBtn = By.id("login");
public LoginPage(WebDriver driver) {
this.driver = driver;
}
public void login(String user, String pass) {
driver.findElement(username).sendKeys(user);
driver.findElement(password).sendKeys(pass);
driver.findElement(loginBtn).click();
}
}
Creating an object:
LoginPage login = new LoginPage(driver);
login.login("admin", "12345");
2. Inheritance
Inheritance allows one class to acquire properties and methods of another class.
Why it matters for Selenium testers
Used in Base Classes
Avoids code duplication
Helps create reusable methods (e.g., browser setup)
Example
public class BaseTest {
WebDriver driver;
public void setUp() {
driver = new ChromeDriver();
driver.manage().window().maximize();
}
}
public class LoginTest extends BaseTest {
@Test
public void testLogin() {
setUp(); // inherited from BaseTest
// test steps here
}
}
3. Polymorphism
Polymorphism means one name, many forms.
Two types:
a) Compile-time (Method Overloading)
Same method name, different parameters.
public void clickElement(WebElement element) {
element.click();
}
public void clickElement(By locator) {
driver.findElement(locator).click();
}
b) Run-time (Method Overriding)
Child class modifies behavior of parent class.
Example: Browser factory
class Browser {
public void open() {
System.out.println("Opening generic browser");
}
}
class Chrome extends Browser {
public void open() {
System.out.println("Opening Chrome browser");
}
}
4. Encapsulation
Encapsulation is the practice of hiding data using private variables and providing public getters and setters.
Why it matters
Protects data
Keeps framework components modular
Prevents accidental modification
Example: Driver manager
public class DriverManager {
private WebDriver driver;
public WebDriver getDriver() {
return driver;
}
public void setDriver(WebDriver driver) {
this.driver = driver;
}
}
5. Abstraction
Abstraction hides implementation details and shows only essential information.
Why it matters
Helps define reusable actions
Simplifies complex operations
Used heavily in framework design
Example: Abstract class for common actions
public abstract class PageBase {
WebDriver driver;
public PageBase(WebDriver driver) {
this.driver = driver;
}
public abstract void waitForPageToLoad();
public void click(By locator) {
driver.findElement(locator).click();
}
}
Specific page extends the abstract class:
public class HomePage extends PageBase {
public HomePage(WebDriver driver) {
super(driver);
}
@Override
public void waitForPageToLoad() {
// page-specific wait implementation
}
}
6. Constructors in Java
A constructor initializes objects.
Use in Selenium
Passing WebDriver instance into page classes
Initializing elements and data
Example
public LoginPage(WebDriver driver) {
this.driver = driver;
PageFactory.initElements(driver, this);
}
7. Interfaces
Interfaces provide contracts for classes to implement. Selenium uses many interfaces (WebDriver, WebElement, TakesScreenshot, etc.)
Example: Interface for reporting
public interface Report {
void log(String message);
}
Implement in classes:
public class HTMLReport implements Report {
public void log(String message) {
System.out.println("HTML Log: " + message);
}
}
8. Packages and Access Modifiers
Organize your framework using packages:
pages
tests
utilities
base
drivers
Access modifiers:
public – accessible everywhere
private – only within class
protected – same package or child classes
default – package-level access
9. Static and Instance Members
Static methods are used for reusable utilities.
Example:
public class WaitUtils {
public static void sleep(int seconds) {
Thread.sleep(seconds * 1000);
}
}
Instance methods depend on objects:
loginPage.login();
10. Why OOP Matters in Selenium Frameworks
OOP helps you create:
Modular code
Reusable page objects
Cleaner tests
Easier maintenance
Scalable automation frameworks
Core design patterns like:
Page Object Model (POM)
Page Factory
Factory Pattern
Singleton Driver
Builder Pattern
are all based on OOP.
Summary
OOP Concept Use in Selenium
Class & Object Page objects, test classes
Inheritance Base classes, common setup/teardown
Polymorphism Browser factories, overloaded actions
Encapsulation Driver management, secure element handling
Abstraction PageBase, reusable actions
Interfaces WebDriver, custom utilities
Constructors Passing driver to page classes
Packages Organizing framework
Learn Selenium with JAVA Training in Hyderabad
Read More
Working with Collections in Java for Selenium Testing
Handling Exceptions in Selenium Test Scripts
Using Loops and Conditions to Control Test Flow
Java Basics for Selenium Testers
Visit Our Quality Thought Institute in Hyderabad
Subscribe by Email
Follow Updates Articles from This Blog via Email
No Comments