Introduction
A Utility Class is a class that contains reusable helper methods used throughout an application. These methods usually perform common tasks such as string manipulation, date formatting, file handling, validation, logging, random data generation, and mathematical calculations.
Most utility classes contain static methods, allowing developers to call the methods directly using the class name without creating an object.
Utility classes help organize common functionality in one place, reduce duplicate code, and improve the maintainability of applications.
For automation engineers, utility classes are one of the most important components of an automation framework. They are commonly used for browser operations, configuration management, file handling, API requests, screenshots, waits, logging, Excel operations, CSV handling, JSON processing, and test data generation.
In this tutorial, you’ll learn how to create and use utility classes in Node.js.
What is a Utility Class?
A Utility Class is a class that provides reusable helper methods for performing common operations.
Most methods inside a utility class are declared as static methods.
Why Use Utility Classes?
Utility classes help developers:
-
Reduce duplicate code.
-
Improve code organization.
-
Increase code reusability.
-
Simplify maintenance.
-
Build modular applications.
-
Keep business logic clean.
-
Improve framework scalability.
Syntax
class MathUtil {
static add(a, b) {
return a + b;
}
}
console.log(
MathUtil.add(10, 20)
);
Example 1: Math Utility Class
class Calculator {
static multiply(a, b) {
return a * b;
}
}
console.log(
Calculator.multiply(6, 7)
);
Sample Output
42
Example 2: String Utility Class
class StringUtil {
static toUpper(text) {
return text.toUpperCase();
}
}
console.log(
StringUtil.toUpper("node.js")
);
Sample Output
NODE.JS
Example 3: Date Utility Class
class DateUtil {
static currentYear() {
return new Date().getFullYear();
}
}
console.log(
DateUtil.currentYear()
);
Sample Output
2026
Example 4: Validation Utility Class
class Validator {
static isEmail(email) {
return email.includes("@");
}
}
console.log(
Validator.isEmail("user@example.com")
);
Sample Output
true
Example 5: Random Number Utility
class RandomUtil {
static generate() {
return Math.floor(
Math.random() * 100
);
}
}
console.log(
RandomUtil.generate()
);
Sample Output
58
Automation Testing Example
Utility classes are essential in automation frameworks.
Playwright Example
Reusable screenshot utility.
class ScreenshotUtil {
static capture() {
console.log(
"Screenshot captured."
);
}
}
ScreenshotUtil.capture();
Selenium Example
Reusable browser utility.
class BrowserUtil {
static maximize() {
console.log(
"Browser maximized."
);
}
}
BrowserUtil.maximize();
Cypress Example
Reusable navigation utility.
class NavigationUtil {
static visitHome() {
console.log(
"Opening Home Page."
);
}
}
NavigationUtil.visitHome();
API Testing Example
Reusable API helper.
class ApiUtil {
static getUsers() {
console.log(
"Fetching users."
);
}
}
ApiUtil.getUsers();
Data-Driven Testing Example
Reusable CSV utility.
class CsvUtil {
static readFile() {
console.log(
"Reading CSV file."
);
}
}
CsvUtil.readFile();
Common Utility Classes in Automation Frameworks
Automation frameworks commonly include utility classes such as:
-
BrowserUtil
-
WaitUtil
-
ScreenshotUtil
-
ConfigUtil
-
LoggerUtil
-
DateUtil
-
StringUtil
-
FileUtil
-
CsvUtil
-
ExcelUtil
-
JsonUtil
-
ApiUtil
-
DatabaseUtil
-
RandomDataUtil
-
ReportUtil
Common Mistakes
Creating Objects for Utility Classes
Since most utility methods are static, there is usually no need to create an object.
Incorrect:
const util =
new Calculator();
util.multiply(2, 3);
Correct:
Calculator.multiply(2, 3);
Mixing Business Logic with Utility Methods
Utility classes should contain only reusable helper functions, not application-specific business logic.
Creating Large Utility Classes
Split unrelated helper methods into multiple focused utility classes.
Best Practices
-
Keep utility classes focused on a specific purpose.
-
Use static methods whenever possible.
-
Group related helper methods together.
-
Use meaningful class names.
-
Avoid storing object-specific data.
-
Keep methods reusable and independent.
-
Organize utility classes into separate folders.
Conclusion
Utility classes are an essential part of modern Node.js applications and automation frameworks. They centralize reusable helper methods, reduce duplicate code, and improve the organization of projects.
For automation engineers, utility classes simplify framework development by providing reusable methods for browser operations, configuration management, file handling, logging, API testing, reporting, data generation, and many other common tasks.
By designing well-structured utility classes, you can build automation frameworks that are modular, scalable, reusable, and easy to maintain.
Frequently Asked Questions (FAQs)
What is a utility class?
A utility class is a class that contains reusable helper methods for common operations.
Why do utility classes use static methods?
Static methods can be called directly using the class name without creating an object.
What are common examples of utility classes?
Examples include DateUtil, StringUtil, FileUtil, LoggerUtil, BrowserUtil, and ApiUtil.
Why are utility classes important in automation testing?
They centralize reusable framework functionality, reduce duplicate code, and make automation scripts easier to maintain.
Should utility classes store object-specific data?
No. Utility classes should contain reusable helper methods and shared functionality rather than object-specific data.
Key Takeaways
-
Utility classes contain reusable helper methods.
-
Most utility methods are declared as
static. -
Utility classes improve code organization.
-
They reduce duplicate code.
-
They simplify framework maintenance.
-
Group related helper methods into dedicated utility classes.
-
Avoid mixing business logic with utility methods.
-
Utility classes are widely used in Playwright, Selenium, Cypress, and API testing frameworks.
-
Well-designed utility classes improve scalability and reusability.
-
Utility classes are a fundamental part of professional Node.js automation frameworks.
