Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Object-oriented programming (OOP) helps test automation when it separates what a test is checking from how the current interface is operated. A Page Object is one practical application: it keeps page-specific locators and interactions behind a small interface, so tests can describe user workflows and assert their outcomes. It is a useful design option, not a rule that every page or element needs a class.

What OOP solves in test automation

A UI test written entirely as a direct script often mixes scenario intent with implementation details: selectors, clicks, waits, field entry, and assertions sit together. If the same locator or interaction appears in several tests, a UI change can leave several places to update. The tests can also become harder to scan because the mechanics obscure the workflow.

OOP offers a way to give related data and behavior a clear owner. In UI automation, that often means grouping knowledge about a page or reusable page region into an object. The test calls meaningful operations on that object rather than repeating the mechanics of finding and operating controls.

Encapsulation

Encapsulation hides implementation details behind an interface. A login page object, for example, can expose a loginAs operation while keeping the username selector, password selector, submit button, and interaction sequence inside the class. When the page markup changes, the relevant page-specific code is easier to locate.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
#1 Best Overall
Elebase USB to USB C Adapter for iPhone 18 Pro Max,USBC Car Charger Adapter
  • Read Before You Buy — No Video Output: These adapters support charging and USB 2.0 data transfer, but cannot transmit video signals. Except for standard USB webcams (which use USB data only), they are not compatible with HDMI/DisplayPort cables, video-capable USB-C hubs, or docking stations with video output.
  • Convert USB-A Ports to USB-C: Designed to connect USB-C earphones, cables, flash drives, card readers, and other USB-C accessories to standard USB-A ports. Plug-and-play with no drivers or software required.
  • Aluminum Alloy Housing: Built with a sturdy aluminum alloy shell that aids in heat dissipation and protects against daily wear and scratches. Designed to maintain a stable and secure connection.
  • Compact & Travel-Friendly: The ultra-compact design allows the adapter to stay plugged into your device without blocking adjacent ports or adding bulk, reducing wear and tear on your original USB ports.
  • 12-Month Warranty: Backed by a 12-month manufacturer warranty for peace of mind. Designed to meet strict quality control standards for reliable everyday performance.

Inheritance and polymorphism

Inheritance and polymorphism are also OOP concepts that can appear in test code, but “using OOP” does not mean building a deep inheritance tree. Use inheritance only when a genuinely shared behavior or contract benefits from it. When a page is made up of reusable regions, composition—building the page object from component objects—is often a more direct fit.

What a Page Object is—and what it is not

A Page Object is an object-oriented class that acts as an interface to a page in the application under test. Tests use its page-level operations when they need to interact with that page. The object should generally describe services the page offers and hide its HTML structure from test authors.

It is not a wrapper class for every DOM element, nor a place to put the whole test scenario. Keep an object only where it provides a useful boundary: a page with meaningful operations, or a reusable region such as navigation or a product list.

Rank #2
Anker USB-C Hub, 5-in-1 USB Hub for Laptops, 4K HDMI Multiport Adapter
  • 5-in-1 USB-C Hub: Experience comprehensive connectivity featuring a Power Delivery input, two USB-A 2.0 ports, a USB-A 3.0 port, and an HDMI port. (Note: The USB-C power delivery input port is only for connecting an external wall charger to power your laptop and cannot power peripheral devices.)
  • 90W Pass-Through Charging: Achieve optimal charging with 90W pass-through power to your laptop, supported by a total input of 100W, with the hub reserving 10W for operational efficiency. (Note: Wall charger not included.)
  • Quick Data Transfers: Accelerate your productivity with rapid data transfers using a high-speed 5Gbps USB 3.0 port and two 480Mbps USB 2.0 ports.
  • 4K HDMI Display: Enhance your visual experience with a hub capable of delivering 4K resolution at 30Hz in both mirror and extend modes. Please note that this hub is compatible with MacBook (macOS 12 and newer), Windows 10 and 11, ChromeOS, and laptops equipped with DP Alt Mode and Power Delivery. Note: This device is not compatible with Linux.
  • What You Get: Anker USB-C Hub (5-in-1, 4K HDMI), welcome guide, 18-month warranty, and our friendly customer service.

Build a Page Object and keep assertions in the test

This Java example uses Selenium WebDriver. The page object owns selectors and interactions; the test owns the assertion about the behavior being tested.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Page object

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

public class LoginPage {
    private final WebDriver driver;
    private final By username = By.id("username");
    private final By password = By.id("password");
    private final By submit = By.cssSelector("button[type='submit']");
    private final By errorMessage = By.cssSelector("[role='alert']");

    public LoginPage(WebDriver driver) {
        this.driver = driver;
    }

    public void open(String baseUrl) {
        driver.get(baseUrl + "/login");
    }

    public void loginAs(String user, String secret) {
        driver.findElement(username).sendKeys(user);
        driver.findElement(password).sendKeys(secret);
        driver.findElement(submit).click();
    }

    public String errorMessage() {
        return driver.findElement(errorMessage).getText();
    }
}

The class accepts its WebDriver from the outside rather than creating a browser itself. That keeps browser setup and lifecycle decisions with the test or its test framework. The method names express page operations; the selectors remain private implementation details.

Test

import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

class LoginTest {
    @Test
    void invalidCredentialsShowAnError() {
        WebDriver driver = new ChromeDriver();
        try {
            LoginPage login = new LoginPage(driver);
            login.open("https://example.test");
            login.loginAs("wrong-user", "wrong-password");

            assertEquals("Invalid username or password", login.errorMessage());
        } finally {
            driver.quit();
        }
    }
}

The example assumes the project already has Selenium and JUnit configured, a compatible Chrome browser and WebDriver available, and an application at the example URL with the shown controls. Replace the URL, selectors, and expected message with the application’s actual values. In a real suite, use the project’s existing driver-management mechanism where one is provided.

Rank #3
Sale
Anker USB C Hub, 7in1 Multi-Port USB Adapter, 4K@60Hz USBC to HDMI Splitter
  • Sleek 7-in-1 USB-C Hub: Features an HDMI port, two USB-A 3.0 ports, and a USB-C data port, each providing 5Gbps transfer speeds. It also includes a USB-C PD input port for charging up to 100W and dual SD and TF card slots, all in a compact design.
  • Flawless 4K@60Hz Video with HDMI: Delivers exceptional clarity and smoothness with its 4K@60Hz HDMI port, making it ideal for high-definition presentations and entertainment. (Note: Only the HDMI port supports video projection; the USB-C port is for data transfer only.)
  • Double Up on Efficiency: The two USB-A 3.0 ports and a USB-C port support a fast 5Gbps data rate, significantly boosting your transfer speeds and improving productivity.
  • Fast and Reliable 85W Charging: Offers high-capacity, speedy charging for laptops up to 85W, so you spend less time tethered to an outlet and more time being productive.
  • What You Get: Anker USB-C Hub (7-in-1), welcome guide, 18-month warranty, and our friendly customer service.

Why the assertion belongs in the test

The test knows the expected result for this scenario; the page object knows how to interact with the page and expose observable information. Selenium’s guidance says page objects should not make verifications or assertions, while identifying checking that the expected page loaded as a limited exception. A getter-style method such as errorMessage() lets the test make the assertion explicitly.

When to add component objects

For a complex page, model a reusable region as a component object when it has coherent behavior or is shared across pages. A page can then hold component objects and delegate region-specific work to them. This is composition: the page is assembled from parts that reflect the interface, rather than inheriting an expanding chain of page classes.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For example, a product-list component might expose operations such as productNamed(name) or addProductToCart(name). Avoid turning every link, label, or container into a class; the abstraction should make the test clearer or keep meaningful shared behavior in one place.

Rank #4
Sale
UGREEN USB to USB C Adapter Combo 4-Pack, 10Gbps USB C Converter Space Gray
  • Dual Converters, Infinite Potential:Includes 2× USB C male to USB A female adapters and 2× USB A male to USB C female adapters. Perfect for a wide range of uses—tablets with Bluetooth keyboards, expand USB ports on macbook, and more. Two different converters for all your daily needs
  • Next-Level 10Gbps & 3A Charging: No more slow 480Mbps, this usb to usb c adapter has a transfer speed of up to 10Gbps, allowing you to do more transferring in less time. This usb adapter fits both USB A and USB C charger, supporting up to 3A fast charging
  • Upgraded Exquisite Craftsmanship: With an aluminum alloy housing and metal connector, the usbc to usb adapter is extremely durable and sturdy. Rigorously tested to withstand more than 10,000 times of plugging and unplugging, ensuring long-lasting performance
  • Broad Compatible: The usb c to usb adapter widely supports all USB C/ USB A devices like laptops, tablets, cellphones, car chargers, and phone chargers. Such as compatible with MacBook Pro/Air 2023/2022, Thunderbolt 4/3 Devices,Apple MagSafe Watch 9/8/7/SE/Ultra, iPad Pro 2022/2021, Samsung Galaxy S23/S20/S10, and iPhone 17/16/15 Pro. Plug and play
  • Please Note: To reach 10Gbps speed, keep the cable under 3.3 ft. For USB A Male to USB C adapters, try flipping the USB C connector. USB C Male to USB A adapters support bidirectional 10Gbps transfer within 3.3 ft

Direct scripts versus Page Objects

Concern Direct UI script Page Object approach
UI change Repeated selectors or interactions may need edits in multiple tests. Page-specific knowledge is centralized, so a relevant fix can often be made in the corresponding object.
Test readability Locator and browser mechanics can crowd the scenario. Page-level operations can make the workflow easier to read.
Responsibility Scenario, UI operation, and expected outcome may be mixed together. Page object operates the UI; test states and verifies the expected outcome.
Reuse Shared interaction code may be repeated. Components can reduce duplication when they represent real reusable regions, but unnecessary abstractions add coupling.
Isolation State and setup can be hard to reason about if tests depend on each other. Objects can clarify boundaries, but test independence still requires deliberate setup and state management.

Centralization is a maintenance rationale, not a quantified promise: it does not guarantee that changes take less time or that every test suite will improve. Selenium presents its material as recommendations rather than a universal best-practice prescription; choose a design that fits the application and test environment.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Keep tests independent

OOP structure does not by itself make tests reliable. Selenium’s test-practice guidance emphasizes test independence and avoiding shared state. A test should establish the conditions it needs and should not rely on another test having run first. A fresh browser per test is one way to limit browser-state leakage; the right lifecycle can depend on the framework and execution cost.

  • Do not store scenario-specific mutable state in a shared page object used by unrelated tests.
  • Make setup and cleanup explicit so a failed test does not leave later tests dependent on its result.
  • Keep page objects focused on page operations instead of turning them into global test utilities.

Or skip the browser setup

If you need a screenshot of a page rather than an interactive browser test, ScreenshotNeo offers a one-request capture API. See the ScreenshotNeo API documentation.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Best Value
Anker USB C Hub, 5-in-1 USBC to HDMI Splitter with 4K Display
  • 5-in-1 Connectivity: Equipped with a 4K HDMI port, a 5 Gbps USB-C data port, two 5 Gbps USB-A ports, and a USB C 100W PD-IN port. Note: The USB C 100W PD-IN port supports only charging and does not support data transfer devices such as headphones or speakers.
  • Powerful Pass-Through Charging: Supports up to 85W pass-through charging so you can power up your laptop while you use the hub. Note: Pass-through charging requires a charger (not included). Note: To achieve full power for iPad, we recommend using a 45W wall charger.
  • Transfer Files in Seconds: Move files to and from your laptop at speeds of up to 5 Gbps via the USB-C and USB-A data ports. Note: The USB C 5Gbps Data port does not support video output.
  • HD Display: Connect to the HDMI port to stream or mirror content to an external monitor in resolutions of up to 4K@30Hz. Note: The USB-C ports do not support video output.
  • What You Get: Anker 332 USB-C Hub (5-in-1), welcome guide, our worry-free 18-month warranty, and friendly customer service.
curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://example.com -o shot.webp
  • Cookie banners are accepted and more than 60 known consent platforms, newsletter popups, and chat widgets are removed before capture; each step can be turned off.
  • Bot checks or CAPTCHAs, blank pages, timeouts, failed loads, and cache hits are not billed; response headers report the page verdict and billing status.
  • An MCP server provides take_screenshot, get_page_info, and capture_pdf tools for AI agents and MCP clients.
  • The Free plan includes 1,000 screenshots per month with no card required; paid plans start at $5 for 3,000 screenshots.

Sign up for 1,000 free screenshots a month with no card.

Further reading

Angie Jones’s chapter “Using Object-Oriented Principles in Test Code” in 97 Things Every Java Programmer Should Know covers encapsulation, inheritance, polymorphism, and the Page Object Model. O’Reilly’s chapter page is Using Object-Oriented Principles in Test Code.

Frequently Asked Questions

Is the Page Object Model a Selenium requirement?

No. Selenium presents Page Objects as a recommended design approach, not a requirement for every test suite.

Does using a Page Object make a test independent?

No. Independence depends on test setup, state management, and avoiding dependencies between test cases; the object pattern alone does not provide it.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.