Learn how to test WebView interactions in your Flutter app with Patrol
WebViews allow you to display web content within your Flutter app. Patrol’s native automation enables you to interact with elements inside WebViews, test navigation, and verify content on both Android and iOS.
Open the screen in your app that contains the WebView:
import 'package:patrol/patrol.dart';void main() { patrolTest('interacts with WebView', ($) async { await $.pumpWidgetAndSettle(const MyApp()); // Navigate to screen with WebView await $('Open WebView').scrollTo().tap(); });}
2
Wait for WebView to load
WebViews need time to load content. Add appropriate delays:
// Wait for WebView to loadawait $.pump(Duration(seconds: 5));await $.pumpAndSettle();
3
Interact with WebView content
Use native selectors to interact with elements inside the WebView:
// Tap on a button inside the WebViewawait $.native.tap( Selector(text: 'Accept Cookies'),);// Wait for content to appearawait $.native.waitUntilVisible( Selector(text: 'Welcome'),);
Patrol can also open URLs in the system browser or WebView:
import 'package:patrol/patrol.dart';patrolTest('opens external URL', ($) async { await $.pumpWidgetAndSettle(const MyApp()); // Open URL in system browser await $.platform.mobile.openUrl('https://example.com'); // Wait for browser to open await Future<void>.delayed(const Duration(seconds: 3)); // Interact with the opened page await $.native.waitUntilVisible( Selector(textContains: 'Example Domain'), ); // Return to app await $.platform.mobile.pressBack();});
WebViews need time to load content. Use generous timeouts:
// Load WebViewawait $('Open WebView').tap();// Wait for load - use both pump and pumpAndSettleawait $.pump(Duration(seconds: 5));await $.pumpAndSettle();
Handle cookie consent dialogs gracefully
Cookie dialogs may or may not appear. Use try-catch:
// Good - wait for elementawait $.native.waitUntilVisible( Selector(text: 'Content'), timeout: Duration(seconds: 10),);// Bad - assume element existsawait $.native.tap(Selector(text: 'Content'));
Test on real devices when possible
WebView behavior can differ between emulators and physical devices. Test on real devices for:
WebView testing can be more fragile than native UI testing. External websites may change without notice, affecting your tests. Consider using local HTML files or controlled test environments when possible.
For testing web-specific features like authentication flows, consider using mock servers or test environments that you control, rather than production websites.