| name | playwright-selectors |
| description | Best practices for writing… |
Playwright E2E Testing for Expo + GlueStack UI
The #1 Rule: Browser First, Code Second
Before writing ANY Playwright test, open the target page in a browser and manually walk through the flow. Never write tests blind from code reading alone.
Expo/GlueStack apps have complex rendering pipelines — what you see in source code is not always what renders in the DOM. Components may have state-dependent behavior (a button that opens an actionsheet OR a confirm dialog depending on data), elements may live on different tabs than you expect, and testIDs may or may not forward to the web DOM depending on the component type.
Workflow for each new test
- Navigate to the page using Playwright MCP browser tools
- Click through the exact user flow the test will cover
- Run
document.querySelectorAll('[data-testid]') to see which testIDs are actually in the DOM
- Note any conditional UI states, loading sequences, and tab navigation required
- Then write the test code matching exactly what you observed
Verify testIDs before scaling
Before writing a batch of tests that depend on testIDs, verify ONE testID end-to-end:
document.querySelectorAll('[data-testid]').length
Accessibility snapshots from Playwright MCP may NOT show data-testid attributes. Always use document.querySelectorAll for ground truth. Pages may show very few testIDs before full render (e.g., 3 elements on initial load vs 80+ after data loads) — wait for the page to settle before checking.
Selector Strategy
Priority order
getByTestId — most stable, survives copy changes and redesigns
getByRole — good for interactive elements (button, tab, switch, heading)
getByLabel — form elements with labels
getByPlaceholder — inputs with placeholder text
getByText — fragile, use only when no testID or role is available
The generic web testing advice to prefer getByRole over getByTestId doesn't fully apply to React Native Web apps because ARIA role mapping is inconsistent across GlueStack components. testIDs are more reliable when properly set up.
await expect(page.getByTestId("settings:dark-mode-toggle")).toBeVisible();
await page.getByRole("button", { name: "Close dialog" }).click();
await page.getByPlaceholder("Search players...").fill("Messi");
await expect(page.getByText("Settings").first()).toBeVisible();
Fallback pattern for migration periods
When adding testIDs to components that are deployed separately from tests, use .or() to fall back gracefully:
const heading = page
.getByTestId("feature:heading")
.or(page.getByText("Feature Title").first());
await expect(heading.first()).toBeVisible();
Remove the .or() fallback once the testID is confirmed deployed and working.
testID Naming Convention
Use a namespaced pattern with colons as separators: screen:element
Format
{screen}:{element}
- screen: lowercase screen/feature name (e.g.,
home, profile, settings)
- element: lowercase element identifier (e.g.,
container, title, submit-button)
Examples
| testID | Description |
|---|
home:container | Main container on home screen |
home:title | Title text on home screen |
profile:avatar | User avatar on profile screen |
settings:dark-mode-toggle | Dark mode toggle in settings |
auth:login-button | Login button on auth screen |
Rules
- Use lowercase only
- Use colons (
:) to separate screen from element
- Use hyphens (
-) for multi-word elements
- Be descriptive but concise
- Avoid redundant words (e.g.,
home:home-title should be home:title)
testID Forwarding: Which Components Support What
This is the most critical technical knowledge for this stack. GlueStack UI components have different testID behavior depending on their render pipeline.
The render chain
React Native Web converts testID → data-testid through its createDOMProps function. But this only happens for components that go through RN Web's createElement path. GlueStack wraps many components with NativeWind utilities (withStyleContext, tva) that bypass this path.
Rules by component type
| Component | testID approach | Why |
|---|
Pressable (GlueStack) | testID={value} | Wraps RN Pressable → View → createDOMProps ✅ |
View (react-native) | testID={value} | Goes through createDOMProps ✅ |
Text (react-native) | testID={value} | Goes through createDOMProps ✅ |
Text (GlueStack @/components/ui/text) | {...{"data-testid": value}} | NativeWind wrapper renders <span>, bypasses createDOMProps |
HStack / VStack / Box (GlueStack) | {...{"data-testid": value}} | Same — NativeWind wrapper bypasses pipeline |
Heading (GlueStack) | data-testid={value} | Renders raw <h1>–<h6> HTML elements |
Button (GlueStack) | Unreliable — verify first | May or may not forward depending on version |
| Third-party (e.g., BouncyCheckbox) | Usually not possible | Use text/role selectors instead |
How to tell which path a component uses
Trace the component's render chain:
GlueStack Pressable → createPressable({ Root: withStyleContext(RNPressable) })
→ withStyleContext passes {...props} to RNPressable
→ RN Web Pressable renders <View {...rest}>
→ View goes through createElement → createDOMProps
→ createDOMProps converts testID → data-testid ✅
vs:
GlueStack Text → tva-styled component
→ Renders <span> or <p> directly
→ Never hits createDOMProps
→ testID prop is silently ignored ❌
Adding a testID to a new component
- Check the component type against the table above
- If it's a
Pressable or RN View/Text, use testID={value} directly
- If it's GlueStack
Text, HStack, VStack, Box, use {...{"data-testid": value}}
- If it's GlueStack
Heading, use data-testid={value} as a JSX attribute
- Build and verify in the browser before writing tests against it
<Pressable testID="feature:action-button" onPress={handlePress}>
<Text>Click me</Text>
</Pressable>
<Text {...{"data-testid": "feature:section-heading"}}>
Section Title
</Text>
<HStack {...{"data-testid": "feature:row"}} className="items-center">
<Icon as={Star} />
<Text>Rating</Text>
</HStack>
<Heading data-testid="feature:page-title" size="lg">
Page Title
</Heading>
When to Add testID
Add testID To
- Interactive elements that E2E tests will click/interact with
- Key structural containers for page load verification
- Dynamic content areas that change based on state
- Form elements that lack semantic labels
Do Not Add testID To
- Every element (over-testing creates maintenance burden)
- Elements with good semantic selectors (use getByRole instead)
- Decorative elements not needed for testing
- Elements inside third-party components (may not propagate)
Accessibility Best Practices
Prefer semantic selectors and aria-labels over testID when possible. This benefits both testing and screen reader users.
aria-label for Testing and Accessibility
<Pressable
accessibilityLabel="Close dialog"
onPress={handleClose}
>
<XIcon />
</Pressable>
await page.getByRole("button", { name: "Close dialog" }).click();
accessibilityRole for Semantic Elements
<Box accessibilityRole="banner" testID="header:container">
<Text accessibilityRole="heading">Welcome</Text>
</Box>
await expect(page.getByRole("banner")).toBeVisible();
await expect(page.getByRole("heading", { name: "Welcome" })).toBeVisible();
CI Architecture: Test the PR's Own Code
Playwright must test against the code in the PR, not a remote deployed environment. If CI tests against a deployed app, new testIDs and component changes are invisible until deployed — creating a frustrating push-wait-fail cycle.
Expo web setup for CI
The CI pipeline should:
- Build the web app:
npx expo export --platform web (creates dist/)
- Serve it locally:
npx serve dist -l 8081 -s
- Run Playwright against
http://localhost:8081/
playwright.config.ts
import { defineConfig } from "@playwright/test";
export default defineConfig({
...(process.env.CI
? {
webServer: {
command: "npx serve dist -l 8081 -s",
port: 8081,
reuseExistingServer: false,
},
}
: {}),
use: {
baseURL: process.env.CI
? "http://localhost:8081/"
: "https://dev.example.com/",
},
});
Writing Robust Tests
Data independence
Never assert on data-dependent elements as required. The CI test user may have different data than your local environment.
const tableRows = page.locator("table tr");
await expect(tableRows.first()).toBeVisible();
expect(await tableRows.count()).toBeGreaterThan(1);
const tableRows = page.locator("table tbody tr");
const rowCount = await tableRows.count();
if (rowCount === 0) {
await expect(page.getByPlaceholder("Search...")).toBeVisible();
return;
}
await tableRows.first().click();
Timeouts
Use environment-aware timeouts from a shared constants file. CI runners are slower than local machines.
export const TIMEOUT = {
test: isCI ? 90_000 : 60_000,
expect: isCI ? 30_000 : 15_000,
navigation: isCI ? 45_000 : 30_000,
};
await expect(element).toBeVisible({ timeout: TIMEOUT.navigation });
Serial vs parallel mode
Use serial mode for tests that mutate shared backend state. Read-only tests can run in parallel.
test.describe("Feature with mutations", () => {
test.describe.configure({ mode: "serial" });
});
State-dependent UI
Some UI elements behave differently depending on application state. Discover this during the browser-first step, then handle both cases:
const addButton = page.getByTestId("feature:add-button").first();
await expect(addButton).toBeVisible();
const modal = page.getByText("Add to List");
const isModalVisible = await modal.isVisible();
if (isModalVisible) {
await page.getByText("Done").click();
} else {
await expect(addButton).toBeVisible();
}
Common Pitfalls
SonarCloud security hotspots
These patterns trigger SonarCloud security hotspot warnings that block PR merges:
page.on("dialog", dialog => dialog.dismiss());
const result = await element.waitFor().catch(() => false);
const isVisible = await element.isVisible();
const count = await elements.count();
Page not fully rendered
Always wait for a content-dependent element before asserting on testIDs:
const count = await page.evaluate(() =>
document.querySelectorAll('[data-testid]').length
);
await page.waitForLoadState("domcontentloaded");
const item = page.getByTestId("feature:item").first();
await item.waitFor({ state: "visible", timeout: 15000 });
Third-party component testIDs
Components from third-party libraries (e.g., react-native-bouncy-checkbox, react-native-gifted-chat) generally do NOT forward testID to the web DOM. Use text, role, or structural selectors for these.
Implementation Checklist
When adding E2E test coverage to a component:
Example Component
export const ProfileScreen = () => (
<Box testID="profile:container" className="flex-1 p-4">
<Image
testID="profile:avatar"
source={{ uri: user.avatarUrl }}
accessibilityLabel={`${user.name}'s profile photo`}
/>
<Text accessibilityRole="heading">
{user.name}
</Text>
<Pressable
accessibilityLabel="Edit profile"
onPress={handleEdit}
>
<Text>Edit</Text>
</Pressable>
</Box>
);
Corresponding E2E Test
test.describe("Profile Screen", () => {
test.use({ viewport: VIEWPORT.desktop });
test.beforeEach(async ({ auth }) => {
await auth.login();
});
test("displays user information", async ({ page }) => {
await page.goto("/profile");
await page.waitForLoadState("domcontentloaded");
await expect(page.getByTestId("profile:container")).toBeVisible();
await expect(page.getByRole("heading")).toHaveText("John Doe");
await expect(
page.getByRole("button", { name: "Edit profile" })
).toBeVisible();
await expect(page.getByTestId("profile:avatar")).toBeVisible();
});
});