Java OOP Concepts for Selenium Testers

 Sure! Here's a clear overview of Java OOP (Object-Oriented Programming) concepts specifically tailored for Selenium testers. Understanding these principles can greatly enhance your ability to write clean, maintainable, and reusable Selenium test automation code.


✅ 1. Class and Object

Class: A blueprint for creating objects.

Object: An instance of a class that can access its methods and variables.


Use in Selenium:

Create a LoginPage class to define elements and methods, then create objects in your tests to perform actions.


java

Copy

Edit

public class LoginPage {

    WebDriver driver;

    By username = By.id("username");

    By password = By.id("password");

    By loginButton = By.id("loginBtn");


    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(loginButton).click();

    }

}

✅ 2. Inheritance

Allows one class to inherit properties and methods from another.


Use in Selenium:

Create a BaseTest class with setup and teardown logic that other test classes can extend.


java

Copy

Edit

public class BaseTest {

    WebDriver driver;


    @BeforeMethod

    public void setup() {

        driver = new ChromeDriver();

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

    }


    @AfterMethod

    public void teardown() {

        driver.quit();

    }

}


public class LoginTest extends BaseTest {

    @Test

    public void testLogin() {

        LoginPage login = new LoginPage(driver);

        login.login("admin", "admin123");

    }

}

✅ 3. Encapsulation

Hides internal details and allows access through public methods (getters/setters).


Use in Selenium:

Store test data privately and provide public methods to access or modify it.


java

Copy

Edit

public class UserData {

    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; }

}

✅ 4. Polymorphism

One method behaves differently based on context — method overloading and overriding.


Use in Selenium:

You might overload a method to accept different parameters for flexibility.


java

Copy

Edit

public class LoginPage {

    // Overloaded login methods

    public void login(String username, String password) {

        // logic

    }


    public void login(UserData user) {

        login(user.getUsername(), user.getPassword());

    }

}

✅ 5. Abstraction

Hides implementation details and shows only functionality.


Use in Selenium:

Use abstract classes or interfaces for page components or test flows.


java

Copy

Edit

public interface Page {

    void open();

    boolean isAt();

}


public class HomePage implements Page {

    WebDriver driver;


    public void open() {

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

    }


    public boolean isAt() {

        return driver.getTitle().equals("Home");

    }

}

🧠 Benefits of OOP in Selenium Testing

Reusability: Use common methods and components across tests.


Maintainability: Easy to update code when the UI changes.


Scalability: Add more tests and features with less duplication.


Readability: Clear separation of concerns (Pages, Tests, Utilities).

Learn Selenium JAVA Course 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 + Selenium Programming Concepts

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