Page objects
A page object puts a screen's locators and flows in one class, so tests read as user journeys and a changed locator is fixed in one place. On mobile there's a catch: the same screen usually needs different locators on Android and iOS. taqwright handles that with Appium-style decorators: declare each locator once per platform, and the running session picks the right one.
import { Screen, AndroidFindBy, iOSFindBy, type Locator } from '@taqwright/taqwright';
import { HomePage } from './HomePage';
export class LoginPage extends Screen {
@AndroidFindBy({ xpath: "//*[@hint='Username']" })
@iOSFindBy({ id: 'Username' })
protected accessor usernameField!: Locator;
@AndroidFindBy({ xpath: "//*[@hint='Password']" })
@iOSFindBy({ id: 'Password' })
protected accessor passwordField!: Locator;
@AndroidFindBy({ uiAutomator: 'new UiSelector().description("Login")' })
@iOSFindBy({ id: 'Login' })
protected accessor loginButton!: Locator;
async login(username: string, password: string): Promise<HomePage> {
await this.usernameField.fill(username);
await this.passwordField.fill(password);
await this.loginButton.click();
return new HomePage(this.mobile);
}
}
import { test, expect } from '@taqwright/taqwright';
import { LoginPage } from '../pages/LoginPage';
test('log in to the demo app', async ({ mobile }) => {
const home = await new LoginPage(mobile).login('emma@demoapp.com', '10203040');
await expect(home.viewAllButton).toBeVisible();
});
The same spec runs on your android and ios projects unchanged. There's no if (platform) anywhere, and no separate Android and iOS classes.
Screen, FindBy, AndroidFindBy and iOSFindBy were added in 1.4.0. Upgrade with npm install @taqwright/taqwright@latest.
How it works
Screenis the base class. Its constructor takes themobilefixture, so build a page withnew LoginPage(mobile). Inside the class,this.mobileis the fullmobileAPI, andthis.platformisPlatform.ANDROIDorPlatform.IOS.@AndroidFindBy/@iOSFindBygive a field its locator for one platform. When the page is built, each field gets the locator for the session's platform.@FindBygives one locator for both platforms. Use it when your app exposes the same id on Android and iOS, for example a React NativetestID.- Fields are ordinary
Locators. Everything from Writing tests works on them: actions,expect(...),.filter(),.nth(),.locator(). They're lazy and re-find the element on every action, so building them up front never gives you a stale element.
Selectors
Each decorator takes exactly one strategy. Every key maps to the getBy* method of the same meaning:
| Key | Same as | Notes |
|---|---|---|
id | getById | Accessibility id (iOS) / resource-id (Android) |
testId | getByTestId | Alias of id |
accessibility | getByLabel | Accessibility label / Android content-desc. label is an alias. |
text | getByText | Visible text. Add exact: false for substring matching, or pass a RegExp. |
placeholder | getByPlaceholder | Input placeholder / hint. Accepts exact too. |
className | getByType | Class name / XCUI element type |
xpath | getByXpath | Raw XPath |
uiAutomator | getByUiSelector | Android only: allowed in @AndroidFindBy |
predicate | getByPredicate | iOS only: allowed in @iOSFindBy |
classChain | getByClassChain | iOS only: allowed in @iOSFindBy |
// One locator for both platforms
@FindBy({ testId: 'cart-checkout' })
accessor checkoutButton!: Locator;
// Different per platform
@AndroidFindBy({ uiAutomator: 'new UiSelector().descriptionContains("Black")' })
@iOSFindBy({ predicate: "label BEGINSWITH 'Black'" })
accessor productCard!: Locator;
// Shared, with an Android override (the platform decorator wins, in either order)
@FindBy({ id: 'search' })
@AndroidFindBy({ xpath: '//android.widget.EditText' })
accessor searchBox!: Locator;
A typo or misplaced key fails as soon as the class loads, not halfway through a run. For example, predicate inside @AndroidFindBy throws @AndroidFindBy: 'predicate' is iOS-only — put it in @iOSFindBy.
Trying several strategies
Pass a list to fall back between strategies, in order:
@AndroidFindBy([
{ id: 'com.app:id/login' }, // preferred
{ text: 'Log in' }, // used if the id isn't found
])
accessor loginButton!: Locator;
Actions use the first entry's element when it's on screen, and later entries only when earlier ones find nothing. This is Appium Java's @AndroidFindAll. Every entry is looked up on each action, so keep lists short.
Fluent navigation
A flow method that leaves the screen returns the next page, so specs read as a chain:
export class HomePage extends Screen {
@AndroidFindBy({ uiAutomator: 'new UiSelector().description("View All")' })
@iOSFindBy({ id: 'View All' })
accessor viewAllButton!: Locator;
async openCatalog(): Promise<SearchCataloguePage> {
await this.viewAllButton.click();
return new SearchCataloguePage(this.mobile);
}
}
test('search a dress and add it to the cart', async ({ mobile }) => {
const home = await new LoginPage(mobile).login('emma@demoapp.com', '10203040');
const catalog = await home.openCatalog();
const cart = await catalog.searchAndAddToCart('black');
// The page object exposes the locator; the spec owns the assertion.
await expect(cart.proceedToCheckout).toBeVisible();
});
Keep assertions in specs. When a spec needs to check an element, make that field public (drop protected) and expect it there.
Screens you've only recorded on one platform
Give a field only the decorator you have. If the iOS locator isn't recorded yet, leave out @iOSFindBy:
@AndroidFindBy({ uiAutomator: 'new UiSelector().description("Open navigation menu")' })
protected accessor openNavigationMenuButton!: Locator;
On iOS, using that field throws an error that names exactly what's missing:
HomePage.openNavigationMenuButton has no iOS locator — add @iOSFindBy(...) or @FindBy(...) to the field, or avoid using it on this platform.
This beats guessing a selector you haven't seen on a live screen. Record it with codegen or npx taqwright inspect, then add the decorator.
When the steps differ, not just the locators
Decorators cover locators. If a platform needs different steps, such as an iOS system alert versus an Android in-app dialog, branch on this.platform inside the method:
import { Platform } from '@taqwright/taqwright';
// … inside the page class:
async allowNotifications(): Promise<void> {
if (this.platform === Platform.IOS) await this.mobile.acceptAlert();
else await this.allowButton.click();
}
For larger differences, extend the page into AndroidXxxPage / IosXxxPage subclasses that override just those methods. They can still use decorators.
Optional: page fixtures
To skip the new LoginPage(mobile) line in every test, add pages as fixtures:
import { test as base } from '@taqwright/taqwright';
import { LoginPage } from './pages/LoginPage';
export const test = base.extend<{ loginPage: LoginPage }>({
loginPage: async ({ mobile }, use) => use(new LoginPage(mobile)),
});
export { expect } from '@taqwright/taqwright';
import { test, expect } from '../fixtures';
test('log in', async ({ loginPage }) => {
const home = await loginPage.login('emma@demoapp.com', '10203040');
await expect(home.viewAllButton).toBeVisible();
});
Rules of thumb
accessor name!: LocatorDeclare every decorated locator with the accessor keyword. A plain name!: Locator field fails to compile on Playwright 1.65+ whenever your tsconfig target is ES2022 or later, with Definitely assigned fields cannot be initialized here. accessor works with any tsconfig.
- Extend
Screenand build pages withnew Page(mobile). The decorators read the platform fromthis.mobile. - Don't enable
experimentalDecorators. These are standard TypeScript 5 decorators, which Playwright compiles out of the box. The legacy flag switches to an older decorator style, and taqwright rejects it with a message saying so. - Don't use
declareon a decorated field.declarefields are stripped at compile time, along with their decorator. - Need TypeScript 5.0+. Standard decorators and
accessorarrived in TS 5.
Coming from Appium Java
| Appium Java | taqwright |
|---|---|
@AndroidFindBy(id = "…") | @AndroidFindBy({ id: '…' }) |
@iOSXCUITFindBy(accessibility = "…") | @iOSFindBy({ accessibility: '…' }) |
@AndroidFindBy(uiAutomator = "…") | @AndroidFindBy({ uiAutomator: '…' }) |
@AndroidFindAll({ @AndroidBy(…), @AndroidBy(…) }) | @AndroidFindBy([{ … }, { … }]) |
PageFactory.initElements(new AppiumFieldDecorator(driver), this) | Not needed: extend Screen |
private MobileElement loginButton; | protected accessor loginButton!: Locator; |
There's no PageFactory step and no element proxies. taqwright locators are already lazy, so there are no stale elements to work around. Java's chained @AndroidFindBys (find a parent, then a child) has no decorator yet. Write it as a getter with .locator():
get submitInForm(): Locator {
return this.mobile.getById('login_form').locator(this.mobile.getById('submit'));
}
Full example
taqwright-page-object is a complete project against the taqwright demo app: six screens, six specs, one class per screen, running on both Android and iOS.