Use this slide if there is no Heading
Note - Create Content inside Red Layout
[Delete Red Outline After creating slide]
Learning Outcome(Slide2)
6
Integrate with Page Objects and Test Classes.
5
Reduce duplication and improve maintainability.
4
Reuse utilities across test cases.
3
Handle screenshots, waits, navigation, and test data.
2
Create reusable methods for common tasks.
1
Understand the purpose of Utility Classes.
Recall
Java Classes & Methods – Create reusable methods.
Locators & Actions – Identify and interact with web elements.
Page Object Model (POM) – Organize page-specific code.
TestNG – Create and execute test cases.
Screenshots – Capture screenshots during test execution.
Waits – Handle synchronization and dynamic elements.
Exception Handling – Manage errors during execution.
Imagine an office where employees frequently need common items.
Office Store Room Utility Class
Common Items Utility Methods
Printer
Print-related method
Calculator
Calculation method
Stapler
Common supporting action
Employees
Test Classes
Instead of every employee keeping their own printer, calculator, and stapler, they reuse the common resources from the store room.
Similarly in Playwright:
Utility Classes contain common reusable methods that different test cases can use whenever required.
Simple idea: Create once → Reuse everywhere
Why Utility Classes Are Required ?
Utility Classes are required to:
Reduce code duplication by keeping common methods in one place.
Improve code reusability across multiple tests and page classes.
Make maintenance easier by updating common logic in one location.
Improve framework organization by separating common functions from test logic.
Simplify test cases by calling ready-to-use utility methods.
Support scalability as the automation framework grows.
Example
Instead of writing screenshot code in every test, create ScreenshotUtil.takeScreenshot() once and reuse it wherever required.
What are Utility Classes?
Utility Classes are classes that contain common and reusable methods used across multiple test cases or page classes in a Playwright automation framework.
ScreenshotUtil
Capture screenshots
WaitUtil
Handle common waits
ExcelUtil
Read test data from Excel
ConfigUtil
Read configuration values
DateUtil
Handle date-related operations
In simple words:
A Utility Class is a collection of reusable helper methods that avoids writing the same code repeatedly.
Utility Class – Project Structure
For a Playwright + Java + TestNG framework, a simple structure can be:
Role of Each Folder
Folder
Purpose
base
Browser setup and cleanup
Test scenarios and validations
tests
utilities
pages
Common reusable methods
Page-specific locators and actions
Maven dependencies
pom.xml
Test data and configuration
resources
Key idea:
Base → Setup
Pages → Page Actions
Utilities → Common Actions
Tests → Test Scenarios
Resources → Test Data
This structure keeps the Playwright framework organized, reusable, and easy to maintain
Practical Implementation
Let's create a simple Screenshot Utility Class and use it in a test.
Create ScreenshotUtil.java :
Create a utilities package
import java.nio.file.Paths;
import com.microsoft.playwright.Page;
public class ScreenshotUtil {
public static void takeScreenshot(Page page, String fileName) {
page.screenshot(
new Page.ScreenshotOptions()
.setPath(Paths.get("screenshots/" + fileName))
);
System.out.println("Screenshot captured: " + fileName);
}
}
Use the Utility in a Test Class
package tests;
import org.testng.annotations.Test;
import utilities.ScreenshotUtil;
public class LoginTest extends BaseTest {
@Test
public void loginTest() {
page.navigate("https://example.com");
page.locator("#username").fill("admin");
page.locator("#password").fill("admin123");
ScreenshotUtil.takeScreenshot(page, "login.png");
page.locator("#login").click();
}
}
Reuse the Same Utility
Another test can use the same method:
ScreenshotUtil.takeScreenshot(page, "order.png"); No need to rewrite the screenshot logic.
Key Point
Test Class
↓
ScreenshotUtil
↓
Common Screenshot Method
Code Reusability
Use the same method across multiple tests.
1
Less Code Duplication
Write common logic only once.
2
Easy Maintenance
Changes can be made in one utility class.
3
Better Organization
Keeps common functions separate from test logic.
4
Duplicate Content
Same content on multiple pages should be avoided
5
Improved Readability –
Content should be updated regularly.
6
Advantages of Utility Classes
Summary
5
Build strong branding
4
Use different marketing channels
3
Target the right audience
2
Create and communicate value
1
Understand customer needs
Choose cool, soft colors instead of vibrant colors
Max 5 Points for Summary & Min 2
Quiz
Which platform is mainly used for professional networking and B2B marketing ?
A. Facebook
B. Instagram
C. LinkedIn
D. Snapchat
Quiz-Answer
Which platform is mainly used for professional networking and B2B marketing ?
A. Facebook
B. Instagram
C. LinkedIn
D. Snapchat