OOP Concepts in Selenium Framework with Java
Object-Oriented Programming (OOP) is a programming paradigm that uses objects and classes to
design and develop applications. When it comes to using Selenium with Java, OOP concepts help
organize and manage the code more effectively. Here?s a detailed and simplified explanation of the
key OOP concepts used in a Selenium framework:
1. Classes and Objects
- Class: A blueprint for creating objects. It defines properties (variables) and behaviors (methods)
that the objects created from the class can have.
- Object: An instance of a class. It contains real values instead of variables and can perform actions
defined in its class.
Example in Selenium:
```java
public class Browser {
WebDriver driver;
public void launchBrowser() {
driver = new ChromeDriver();
driver.get("https://example.com");
```
Here, Browser is a class with a method launchBrowser. An object of Browser can use this method to
open a browser.
2. Inheritance
- Inheritance allows a class to inherit properties and methods from another class. The class that
inherits is called the subclass, and the class being inherited from is called the superclass.
Example in Selenium:
```java
public class BaseTest {
WebDriver driver;
public void setUp() {
driver = new ChromeDriver();
driver.get("https://example.com");
public class LoginTest extends BaseTest {
public void login() {
// Code to perform login
```
LoginTest inherits from BaseTest, so it can use the setUp method defined in BaseTest.
3. Encapsulation
- Encapsulation is the concept of wrapping data (variables) and code (methods) together into a
single unit (class). It helps in hiding the internal state and requiring all interaction to be performed
through an object's methods.
Example in Selenium:
```java
public class User {
private String username;
private String password;
public String getUsername() {
return username;
public void setUsername(String username) {
this.username = username;
public String getPassword() {
return password;
public void setPassword(String password) {
this.password = password;
}
```
Here, the User class encapsulates the username and password variables. They can only be
accessed or modified through the getter and setter methods.
4. Polymorphism
- Polymorphism allows methods to do different things based on the object it is acting upon. There
are two types: compile-time (method overloading) and runtime (method overriding).
Example in Selenium:
```java
public class Browser {
public void open(String url) {
System.out.println("Opening URL: " + url);
public void open(String url, int timeout) {
System.out.println("Opening URL: " + url + " with timeout: " + timeout);
public class ChromeBrowser extends Browser {
@Override
public void open(String url) {
System.out.println("Opening Chrome browser with URL: " + url);
}
```
Here, method overloading is shown with two open methods in Browser, and method overriding is
shown in ChromeBrowser.
Applying OOP in a Selenium Framework
- Page Object Model (POM): Each web page is represented as a class. The elements on the page
are variables, and interactions with the page are methods.
Example:
```java
public class LoginPage {
WebDriver driver;
public LoginPage(WebDriver driver) {
this.driver = driver;
By usernameField = By.id("username");
By passwordField = By.id("password");
By loginButton = By.id("login");
public void enterUsername(String username) {
driver.findElement(usernameField).sendKeys(username);
}
public void enterPassword(String password) {
driver.findElement(passwordField).sendKeys(password);
public void clickLogin() {
driver.findElement(loginButton).click();
```
- Test Classes: Use inheritance to share setup and teardown methods.
- Utility Classes: Encapsulate reusable methods like reading from a file, taking screenshots, etc.
Conclusion
Using OOP concepts in Selenium with Java helps in creating a well-structured and maintainable test
automation framework. Classes and objects organize the code, inheritance promotes code reuse,
encapsulation hides complexity, and polymorphism enhances flexibility.