Page objects in C#: keeping UI tests readable as they grow
By Denis Bojkovski, Test Automation Architect · Published November 25, 2026
The problem they solve
Without structure, the same selector is written in forty tests. When the sign-in button changes, forty tests change. A page object puts knowledge of one screen in one class: where things are and what a user can do there. The test then reads like behaviour.
Expose actions, not elements
public sealed class LoginPage(IPage page)
{
private ILocator Email => page.GetByLabel("Email");
private ILocator Password => page.GetByLabel("Password");
private ILocator Submit => page.GetByRole(AriaRole.Button, new() { Name = "Sign in" });
public ILocator Error => page.GetByRole(AriaRole.Alert);
public Task OpenAsync() => page.GotoAsync("/login");
public async Task SignInAsync(string email, string password)
{
await Email.FillAsync(email);
await Password.FillAsync(password);
await Submit.ClickAsync();
}
}
The inputs are private, because no test should care that there are two of them. The error is public, because tests assert on it. A useful check: if the form is split into two steps tomorrow, how many tests change? With this design, none.
Keep assertions in the tests
Page objects that assert internally hide what the test is proving. Compare:
await login.SignInAndVerifyErrorAsync("a@example.com", "wrong");
await login.SignInAsync("a@example.com", "wrong");
await Expect(login.Error).ToHaveTextAsync("Email or password is incorrect");
The second says what is expected. The exception is a guard, such as waiting until the page has loaded, which protects later steps rather than testing anything.
Model components, not only pages
A header, a date picker or a results table appears on many screens. Give it its own class with a root locator:
public sealed class OrderRow(ILocator root)
{
public ILocator Status => root.GetByTestId("status");
public Task CancelAsync() =>
root.GetByRole(AriaRole.Button, new() { Name = "Cancel" }).ClickAsync();
}
public sealed class OrdersPage(IPage page)
{
public OrderRow Row(string orderNumber) =>
new(page.GetByRole(AriaRole.Row).Filter(new() { HasText = orderNumber }));
}
Find rows by what identifies them, never by position. "The third row" is correct only until somebody sorts the table.
Navigation: return the next page or not?
Some teams make SignInAsync return a DashboardPage. It reads well for a fixed flow, but the same action can lead to several places: dashboard, error, second factor. Returning nothing and constructing the next page in the test is plainer. Choose one convention for the whole suite.
Signs the layer has gone wrong
- A
BasePagewith hundreds of lines of wrappers such asClickElement(locator). The library already hasClickAsync. - Boolean parameters:
SubmitAsync(true, false). - Page objects that create test data or call the database. Put that in builders or API clients.
- Methods named after implementation, such as
ClickBlueButton. - One class per page with fifty methods. Split it by component.
Do you need them at all?
For five tests, local functions are enough. Introduce a class when the second test needs the same knowledge. Page object appeared by name in 4 of the 86 full descriptions on QAAnywhere on 25 November 2026, while "framework" appeared in 56: employers rarely ask for the pattern by name, but they do ask for maintainable frameworks, and this is the part an interviewer can review in ten minutes.
A refactoring exercise
Take three existing tests that share a screen. List each selector and count its repeats. Move repeated ones into one class with private locators, rename methods after user goals, and run the tests. Then change one selector in the application deliberately: exactly one file should need an edit. Continue with designing a test automation framework.