Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
26 views

Understanding Abstraction in Java Selenium Project

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Understanding Abstraction in Java Selenium Project

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Understanding Abstraction in Java

Selenium Project
Abstraction is one of the core concepts of Object-Oriented Programming (OOP). It allows
you to focus on the essential features of an object without worrying about its internal
details. In Selenium and Java, abstraction is a powerful tool to create flexible, reusable,
and maintainable test automation frameworks.

In this post, we will:

1. Understand what abstraction is.


2. Learn how to use abstraction in Java Selenium with simple examples.
3. Build a Selenium test automation framework with abstraction applied to a
real-world project.

What is Abstraction?

Abstraction in programming is a way of hiding the implementation details and showing


only the functionality to the user. In Java, abstraction is implemented using:

1. Abstract classes: A class that cannot be instantiated and may contain both
abstract (methods without implementation) and concrete (methods with
implementation) methods.
2. Interfaces: A contract that classes can implement to define behavior.

Examples:

1. TV Remote: Imagine you use a TV remote to change channels or adjust the


volume. You don’t need to know how the remote communicates with the TV—you
just press buttons and it works. The "remote" is the abstraction that hides the
complex functionality.
2. Car Steering Wheel: When you drive a car, you turn the steering wheel to change
direction. You don’t need to understand the mechanics of how the steering system
works; you just use it.
3. Mobile Phone: You tap an app icon to open it. The app runs behind the scenes, but
you only interact with the app interface—not the code or hardware that makes it
work.
Why is Abstraction Important in Selenium?

In Selenium, abstraction helps to:

● Separate test logic from browser-specific actions.


● Enhance reusability by creating generic methods.
● Improve maintainability and scalability of the test framework.

Abstraction in Action: Real-World Scenario

Scenario: Consider an e-commerce website where you need to test the login functionality.
Different pages (like the home page, product page, and checkout page) might have a
login button, but the behavior of the login feature remains the same.

Using abstraction, you can create a generic LoginPage class and extend or implement it in
specific page classes.

Example 1: Using Abstract Classes in Selenium

Abstract Class
public abstract class Page {
WebDriver driver;

public Page(WebDriver driver) {


this.driver = driver;
}

// Abstract method - must be implemented by subclasses


public abstract void navigateTo(String url);

// Concrete method
public void clickElement(By locator) {
driver.findElement(locator).click();
}
}

Concrete Implementation
public class LoginPage extends Page {

public LoginPage(WebDriver driver) {


super(driver);
}

@Override
public void navigateTo(String url) {
driver.get(url);
}

public void login(String username, String password) {


driver.findElement(By.id("username")).sendKeys(username);
driver.findElement(By.id("password")).sendKeys(password);
driver.findElement(By.id("loginButton")).click();
}
}

Usage in Test
public class LoginTest {

public static void main(String[] args) {


WebDriver driver = new ChromeDriver();
LoginPage loginPage = new LoginPage(driver);

loginPage.navigateTo("https://example.com/login");
loginPage.login("testuser", "password123");

driver.quit();
}
}

Step-by-Step Code Execution

1. Step 1: The LoginTest class initializes the WebDriver and creates an instance of
LoginPage.
2. Step 2: The navigateTo method is called to open the login page URL.
3. Step 3: The login method inputs the username and password into their respective
fields and clicks the login button.
4. Step 4: The browser session is closed using driver.quit() after completing the test.
Explanation of Abstraction Usage

In this example, the Page class abstracts common behaviors such as clickElement and
navigateTo, allowing subclasses like LoginPage to implement specific functionality (e.g.,
login behavior). This separation ensures reusability and reduces duplication.

Example 2: Using Interfaces in Selenium

Interface Definition
public interface PageInterface {
void navigateTo(String url);
void clickElement(By locator);
}

Implementation
public class HomePage implements PageInterface {
WebDriver driver;

public HomePage(WebDriver driver) {


this.driver = driver;
}

@Override
public void navigateTo(String url) {
driver.get(url);
}

@Override
public void clickElement(By locator) {
driver.findElement(locator).click();
}
}

Usage in Test
public class HomePageTest {

public static void main(String[] args) {


WebDriver driver = new ChromeDriver();
PageInterface homePage = new HomePage(driver);

homePage.navigateTo("https://example.com");
homePage.clickElement(By.id("searchBox"));

driver.quit();
}
}

Step-by-Step Code Execution

1. Step 1: The HomePageTest class initializes the WebDriver and creates an instance
of HomePage.
2. Step 2: The navigateTo method is invoked to open the specified URL.
3. Step 3: The clickElement method interacts with a web element (e.g., a search box).
4. Step 4: The browser session is closed using driver.quit().

Explanation of Abstraction Usage

The PageInterface defines a contract for common behaviors (navigateTo, clickElement).


The HomePage class implements these behaviors, focusing only on its specific context
(e.g., homepage interactions). This makes the code flexible and easy to extend for other
pages.
Building a Selenium Framework with Abstraction

Let’s create a real-world example for an Employee Management System where the
application allows you to manage employee data.

Step 1: Define the Abstract Layer


public abstract class BasePage {
WebDriver driver;

public BasePage(WebDriver driver) {


this.driver = driver;
}

public abstract void navigateTo(String url);

public void click(By locator) {


driver.findElement(locator).click();
}

public void enterText(By locator, String text) {


driver.findElement(locator).sendKeys(text);
}

public String getText(By locator) {


return driver.findElement(locator).getText();
}
}

Step 2: Create Specific Page Classes


public class EmployeePage extends BasePage {

public EmployeePage(WebDriver driver) {


super(driver);
}

@Override
public void navigateTo(String url) {
driver.get(url);
}

public void addEmployee(String name, String role) {


enterText(By.id("employeeName"), name);
enterText(By.id("employeeRole"), role);
click(By.id("addButton"));
}

public String getEmployeeDetails() {


return getText(By.id("employeeDetails"));
}
}

Step 3: Write Tests Using the Framework


public class EmployeeTest {

public static void main(String[] args) {


WebDriver driver = new ChromeDriver();
EmployeePage employeePage = new EmployeePage(driver);

employeePage.navigateTo("https://example.com/employee");
employeePage.addEmployee("John Doe", "Software Engineer");

String details = employeePage.getEmployeeDetails();


System.out.println("Employee Details: " + details);

driver.quit();
}
}

Step-by-Step Code Execution

1. Step 1: The EmployeeTest initializes the WebDriver and creates an instance of


EmployeePage.
2. Step 2: The navigateTo method opens the employee management page.
3. Step 3: The addEmployee method adds a new employee by filling out the form and
clicking the add button.
4. Step 4: The getEmployeeDetails method retrieves and prints the details of the
added employee.
5. Step 5: The browser session is closed using driver.quit().

Explanation of Abstraction Usage

The BasePage abstracts common operations (e.g., click, enterText) that are shared across
pages. The EmployeePage focuses only on employee-specific functionality, keeping the
code modular and reusable.

Summary

1. Abstract Classes: Use them when you need to share common behavior among
multiple related classes.
2. Interfaces: Use them when you need to define a contract for multiple unrelated
classes.
3. Abstraction in Frameworks: Separates high-level test logic from low-level
implementation, making your framework flexible and maintainable.

By applying abstraction in your Selenium Java project, you can achieve better
organization, improve reusability, and make future changes easier to implement. Try
integrating these concepts into your automation projects for cleaner and more effective
test scripts!

You might also like