|
| 1 | +# Testing Flutter Web Apps with Playwright |
| 2 | + |
| 3 | +## The Challenge |
| 4 | + |
| 5 | +Flutter web apps render their UI to a canvas element, which makes traditional DOM-based testing approaches ineffective. You cannot: |
| 6 | +- ❌ Use CSS selectors to find buttons, text, or other UI elements |
| 7 | +- ❌ Directly interact with Flutter widgets via Playwright |
| 8 | +- ❌ Inspect the visual content rendered on the canvas |
| 9 | + |
| 10 | +## The Solution: Accessibility-Based Testing |
| 11 | + |
| 12 | +Flutter creates **DOM elements for accessibility** through the `Semantics` widget. These elements are specifically designed for screen readers but also serve as a reliable testing interface. |
| 13 | + |
| 14 | +### How It Works |
| 15 | + |
| 16 | +1. **Flutter Semantics Widget** → Creates ARIA labels in the DOM |
| 17 | +2. **Screen readers** → Read these ARIA labels |
| 18 | +3. **Playwright tests** → Can also read these ARIA labels |
| 19 | + |
| 20 | +### Example |
| 21 | + |
| 22 | +**Flutter Code:** |
| 23 | +```dart |
| 24 | +Semantics( |
| 25 | + label: 'Drinks tab, browse all festival drinks', |
| 26 | + child: const Icon(Icons.local_drink_outlined), |
| 27 | +) |
| 28 | +``` |
| 29 | + |
| 30 | +**Generated DOM (simplified):** |
| 31 | +```html |
| 32 | +<flt-semantics aria-label="Drinks tab, browse all festival drinks"> |
| 33 | + <!-- Flutter renders icon to canvas --> |
| 34 | +</flt-semantics> |
| 35 | +``` |
| 36 | + |
| 37 | +**Playwright Test:** |
| 38 | +```typescript |
| 39 | +const drinksTabLabel = page.locator('[aria-label*="Drinks tab"]'); |
| 40 | +await expect(drinksTabLabel.first()).toBeAttached(); |
| 41 | +``` |
| 42 | + |
| 43 | +## What You CAN Test |
| 44 | + |
| 45 | +✅ **Page loads successfully** - Check for Flutter embedder elements |
| 46 | +✅ **URL routing** - Verify URLs change correctly during navigation |
| 47 | +✅ **Browser history** - Test back/forward button functionality |
| 48 | +✅ **Console errors** - Monitor for JavaScript errors |
| 49 | +✅ **Network requests** - Verify API calls are made |
| 50 | +✅ **Screen verification** - Use ARIA labels to confirm which screen is displayed |
| 51 | +✅ **Accessibility** - Ensure proper ARIA labels exist for screen readers |
| 52 | + |
| 53 | +## What You CANNOT Test |
| 54 | + |
| 55 | +❌ **Visual appearance** - Colors, fonts, layout (use visual regression testing or Flutter integration tests) |
| 56 | +❌ **Canvas interactions** - Clicking specific points on the canvas |
| 57 | +❌ **Gesture detection** - Swipes, drags, pinch-to-zoom |
| 58 | +❌ **Text content** - Reading text rendered on canvas (unless it has ARIA labels) |
| 59 | + |
| 60 | +## Best Practices |
| 61 | + |
| 62 | +### 1. Add Semantics to Key UI Elements |
| 63 | + |
| 64 | +Always wrap important UI elements with `Semantics` widgets: |
| 65 | + |
| 66 | +```dart |
| 67 | +// Good |
| 68 | +Semantics( |
| 69 | + label: 'View source code on GitHub', |
| 70 | + hint: 'Double tap to open GitHub repository in browser', |
| 71 | + button: true, |
| 72 | + child: IconButton( |
| 73 | + icon: Icon(Icons.code), |
| 74 | + onPressed: _openGitHub, |
| 75 | + ), |
| 76 | +) |
| 77 | +
|
| 78 | +// Bad - no Semantics, cannot be tested or used by screen readers |
| 79 | +IconButton( |
| 80 | + icon: Icon(Icons.code), |
| 81 | + onPressed: _openGitHub, |
| 82 | +) |
| 83 | +``` |
| 84 | + |
| 85 | +### 2. Use Descriptive ARIA Labels |
| 86 | + |
| 87 | +Make labels unique enough to identify specific screens: |
| 88 | + |
| 89 | +```dart |
| 90 | +// Good - unique to About screen |
| 91 | +Semantics( |
| 92 | + label: 'View source code on GitHub', |
| 93 | + // ... |
| 94 | +) |
| 95 | +
|
| 96 | +// Bad - too generic, could be on any screen |
| 97 | +Semantics( |
| 98 | + label: 'Button', |
| 99 | + // ... |
| 100 | +) |
| 101 | +``` |
| 102 | + |
| 103 | +### 3. Test What Matters for Routing |
| 104 | + |
| 105 | +For go_router navigation tests, focus on: |
| 106 | + |
| 107 | +```typescript |
| 108 | +test('should navigate to about screen', async ({ page }) => { |
| 109 | + await page.goto('http://localhost:8080/about'); |
| 110 | + await waitForPageReady(page); |
| 111 | + |
| 112 | + // 1. Verify URL changed |
| 113 | + expect(page.url()).toBe('http://localhost:8080/about'); |
| 114 | + |
| 115 | + // 2. Verify correct screen via unique ARIA label |
| 116 | + const aboutLabel = page.locator('[aria-label*="View source code on GitHub"]'); |
| 117 | + await expect(aboutLabel.first()).toBeAttached(); |
| 118 | + |
| 119 | + // 3. Verify no console errors |
| 120 | + // (setup error listeners before navigation) |
| 121 | +}); |
| 122 | +``` |
| 123 | + |
| 124 | +### 4. Keep Tests Focused |
| 125 | + |
| 126 | +Don't try to test complex user interactions in E2E tests. Use Flutter integration tests for those: |
| 127 | + |
| 128 | +```dart |
| 129 | +// This belongs in Flutter integration tests, not Playwright: |
| 130 | +testWidgets('tapping favorite button adds drink to favorites', (tester) async { |
| 131 | + await tester.pumpWidget(MyApp()); |
| 132 | + await tester.tap(find.byIcon(Icons.favorite_border)); |
| 133 | + await tester.pump(); |
| 134 | + expect(find.byIcon(Icons.favorite), findsOneWidget); |
| 135 | +}); |
| 136 | +``` |
| 137 | + |
| 138 | +## Example Test Suite Structure |
| 139 | + |
| 140 | +``` |
| 141 | +test-e2e/ |
| 142 | +├── app.spec.ts # Basic app loading tests |
| 143 | +├── routing.spec.ts # Navigation/routing tests (uses ARIA labels) |
| 144 | +└── network.spec.ts # API request tests (optional) |
| 145 | +``` |
| 146 | + |
| 147 | +## Benefits of This Approach |
| 148 | + |
| 149 | +1. **Accessibility First** - Tests ensure the app is usable by screen readers |
| 150 | +2. **Stable Selectors** - ARIA labels are less likely to change than internal Flutter DOM structure |
| 151 | +3. **Meaningful Tests** - Verifies actual user-facing behavior (navigation, errors) |
| 152 | +4. **Dual Purpose** - Same Semantics widgets benefit both testing and accessibility |
| 153 | +5. **Fast Feedback** - Catch routing issues in CI before manual testing |
| 154 | + |
| 155 | +## References |
| 156 | + |
| 157 | +- [Flutter Semantics Documentation](https://api.flutter.dev/flutter/widgets/Semantics-class.html) |
| 158 | +- [Playwright Accessibility Testing](https://playwright.dev/docs/accessibility-testing) |
| 159 | +- [Flutter Web Rendering](https://docs.flutter.dev/platform-integration/web/renderers) |
| 160 | + |
| 161 | +## Current Usage in This App |
| 162 | + |
| 163 | +This app has **24 Semantics widgets** across various screens: |
| 164 | +- Navigation tabs (Drinks, Favorites) |
| 165 | +- About screen buttons (GitHub, Issues, Licenses, Theme toggle) |
| 166 | +- Search field |
| 167 | +- Filter chips |
| 168 | +- Info buttons |
| 169 | + |
| 170 | +These provide both accessibility and testability. |
0 commit comments