Page class abstract

Base class for the Page Object Pattern.

The Page class is part of the Page Object Pattern, a design pattern that creates an object representation of each page in your application. This abstraction helps separate test logic from page implementation details, making tests more maintainable and readable.

Each page class encapsulates the structure of a specific page, including its URL, elements, and common interactions.

Example

class LoginPage extends Page {
  LoginPage(Browser browser) : super(browser);

  @override
  String get url => '/login';

  // Page elements
  String get emailInput => 'input[name="email"]';
  String get passwordInput => 'input[name="password"]';
  String get submitButton => 'button[type="submit"]';

  // Page interactions
  Future<void> login({required String email, required String password}) async {
    await browser.type(emailInput, email);
    await browser.type(passwordInput, password);
    await browser.click(submitButton);
  }

  // Page assertions
  Future<void> assertHasError(String message) async {
    await browser.assertSeeIn('.error-message', message);
  }
}

Usage in Tests

void main() {
  browserTest('user can log in', (browser) async {
    final loginPage = LoginPage(browser);
    await loginPage.navigate();

    await loginPage.login(
      email: 'user@example.com',
      password: 'password123',
    );

    // Assert we're redirected to dashboard
    await DashboardPage(browser).assertOnPage();
  });
}

Constructors

Page.new(Browser browser)
Creates a new page with the specified browser.

Properties

browser Browser
The browser instance for interacting with the page.
final
hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
url String
The URL of this page.
no setter

Methods

assertOnPage() Future<void>
Asserts that the browser is currently on this page.
Navigates to this page's URL.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited