diff --git a/packages/webview_flutter/webview_flutter/example/integration_test/webview_flutter_test.dart b/packages/webview_flutter/webview_flutter/example/integration_test/webview_flutter_test.dart index 896366798b18..3379bafa2346 100644 --- a/packages/webview_flutter/webview_flutter/example/integration_test/webview_flutter_test.dart +++ b/packages/webview_flutter/webview_flutter/example/integration_test/webview_flutter_test.dart @@ -18,6 +18,13 @@ import 'package:integration_test/integration_test.dart'; void main() { IntegrationTestWidgetsFlutterBinding.ensureInitialized(); + // URLs to navigate to in tests. These need to be URLs that we are confident will + // always be accessible, and won't do redirection. (E.g., just + // 'https://www.google.com/' will sometimes redirect traffic that looks + // like it's coming from a bot, which is true of these tests). + const String primaryUrl = 'https://flutter.dev/'; + const String secondaryUrl = 'https://www.google.com/robots.txt'; + const bool _skipDueToIssue86757 = true; // TODO(bparrishMines): skipped due to https://github.com/flutter/flutter/issues/86757. @@ -29,7 +36,7 @@ void main() { textDirection: TextDirection.ltr, child: WebView( key: GlobalKey(), - initialUrl: 'https://flutter.dev/', + initialUrl: primaryUrl, onWebViewCreated: (WebViewController controller) { controllerCompleter.complete(controller); }, @@ -38,7 +45,7 @@ void main() { ); final WebViewController controller = await controllerCompleter.future; final String? currentUrl = await controller.currentUrl(); - expect(currentUrl, 'https://flutter.dev/'); + expect(currentUrl, primaryUrl); }, skip: _skipDueToIssue86757); // TODO(bparrishMines): skipped due to https://github.com/flutter/flutter/issues/86757. @@ -50,7 +57,7 @@ void main() { textDirection: TextDirection.ltr, child: WebView( key: GlobalKey(), - initialUrl: 'https://flutter.dev/', + initialUrl: primaryUrl, onWebViewCreated: (WebViewController controller) { controllerCompleter.complete(controller); }, @@ -58,9 +65,9 @@ void main() { ), ); final WebViewController controller = await controllerCompleter.future; - await controller.loadUrl('https://www.example.com/'); + await controller.loadUrl(secondaryUrl); final String? currentUrl = await controller.currentUrl(); - expect(currentUrl, 'https://www.example.com/'); + expect(currentUrl, secondaryUrl); }, skip: _skipDueToIssue86757); // TODO(bparrishMines): skipped due to https://github.com/flutter/flutter/issues/86757. @@ -74,7 +81,7 @@ void main() { textDirection: TextDirection.ltr, child: WebView( key: GlobalKey(), - initialUrl: 'https://flutter.dev/', + initialUrl: primaryUrl, onWebViewCreated: (WebViewController controller) { controllerCompleter.complete(controller); }, @@ -292,7 +299,7 @@ void main() { textDirection: TextDirection.ltr, child: WebView( key: _globalKey, - initialUrl: 'https://flutter.dev/', + initialUrl: primaryUrl, javascriptMode: JavascriptMode.unrestricted, onWebViewCreated: (WebViewController controller) { controllerCompleter.complete(controller); @@ -1099,12 +1106,11 @@ void main() { await pageLoads.stream.first; // Wait for initial page load. final WebViewController controller = await controllerCompleter.future; - await controller - .evaluateJavascript('location.href = "https://www.example.com/"'); + await controller.evaluateJavascript('location.href = "$secondaryUrl"'); await pageLoads.stream.first; // Wait for the next page load. final String? currentUrl = await controller.currentUrl(); - expect(currentUrl, 'https://www.example.com/'); + expect(currentUrl, secondaryUrl); }); testWidgets('onWebResourceError', (WidgetTester tester) async { @@ -1271,12 +1277,11 @@ void main() { await pageLoads.stream.first; // Wait for initial page load. final WebViewController controller = await controllerCompleter.future; - await controller - .evaluateJavascript('location.href = "https://www.example.com"'); + await controller.evaluateJavascript('location.href = "$secondaryUrl"'); await pageLoads.stream.first; // Wait for second page to load. final String? currentUrl = await controller.currentUrl(); - expect(currentUrl, 'https://www.example.com/'); + expect(currentUrl, secondaryUrl); }); }); @@ -1292,7 +1297,7 @@ void main() { height: 300, child: WebView( key: GlobalKey(), - initialUrl: 'https://flutter.dev/', + initialUrl: primaryUrl, gestureNavigationEnabled: true, onWebViewCreated: (WebViewController controller) { controllerCompleter.complete(controller); @@ -1303,7 +1308,7 @@ void main() { ); final WebViewController controller = await controllerCompleter.future; final String? currentUrl = await controller.currentUrl(); - expect(currentUrl, 'https://flutter.dev/'); + expect(currentUrl, primaryUrl); }); testWidgets('target _blank opens in same window', @@ -1327,11 +1332,10 @@ void main() { ), ); final WebViewController controller = await controllerCompleter.future; - await controller - .evaluateJavascript('window.open("https://flutter.dev/", "_blank")'); + await controller.evaluateJavascript('window.open("$primaryUrl", "_blank")'); await pageLoaded.future; final String? currentUrl = await controller.currentUrl(); - expect(currentUrl, 'https://flutter.dev/'); + expect(currentUrl, primaryUrl); }, // Flaky on Android: https://github.com/flutter/flutter/issues/86757 skip: Platform.isAndroid && _skipDueToIssue86757); @@ -1355,25 +1359,24 @@ void main() { onPageFinished: (String url) { pageLoaded.complete(); }, - initialUrl: 'https://flutter.dev', + initialUrl: primaryUrl, ), ), ); final WebViewController controller = await controllerCompleter.future; - expect(controller.currentUrl(), completion('https://flutter.dev/')); + expect(controller.currentUrl(), completion(primaryUrl)); await pageLoaded.future; pageLoaded = Completer(); - await controller - .evaluateJavascript('window.open("https://www.example.com/")'); + await controller.evaluateJavascript('window.open("$secondaryUrl")'); await pageLoaded.future; pageLoaded = Completer(); - expect(controller.currentUrl(), completion('https://www.example.com/')); + expect(controller.currentUrl(), completion(secondaryUrl)); expect(controller.canGoBack(), completion(true)); await controller.goBack(); await pageLoaded.future; - expect(controller.currentUrl(), completion('https://flutter.dev/')); + expect(controller.currentUrl(), completion(primaryUrl)); }, skip: _skipDueToIssue86757, ); diff --git a/packages/webview_flutter/webview_flutter_android/example/integration_test/webview_flutter_test.dart b/packages/webview_flutter/webview_flutter_android/example/integration_test/webview_flutter_test.dart index 3dab048c8ff2..c57d2bd55580 100644 --- a/packages/webview_flutter/webview_flutter_android/example/integration_test/webview_flutter_test.dart +++ b/packages/webview_flutter/webview_flutter_android/example/integration_test/webview_flutter_test.dart @@ -22,6 +22,13 @@ import 'package:webview_flutter_platform_interface/webview_flutter_platform_inte void main() { IntegrationTestWidgetsFlutterBinding.ensureInitialized(); + // URLs to navigate to in tests. These need to be URLs that we are confident will + // always be accessible, and won't do redirection. (E.g., just + // 'https://www.google.com/' will sometimes redirect traffic that looks + // like it's coming from a bot, which is true of these tests). + const String primaryUrl = 'https://flutter.dev/'; + const String secondaryUrl = 'https://www.google.com/robots.txt'; + const bool _skipDueToIssue86757 = true; // TODO(bparrishMines): skipped due to https://github.com/flutter/flutter/issues/86757. @@ -34,7 +41,7 @@ void main() { textDirection: TextDirection.ltr, child: WebView( key: GlobalKey(), - initialUrl: 'https://flutter.dev/', + initialUrl: primaryUrl, onWebViewCreated: (WebViewController controller) { controllerCompleter.complete(controller); }, @@ -44,7 +51,7 @@ void main() { ); final WebViewController controller = await controllerCompleter.future; final String? currentUrl = await controller.currentUrl(); - expect(currentUrl, 'https://flutter.dev/'); + expect(currentUrl, primaryUrl); }, skip: _skipDueToIssue86757); // TODO(bparrishMines): skipped due to https://github.com/flutter/flutter/issues/86757. @@ -56,7 +63,7 @@ void main() { textDirection: TextDirection.ltr, child: WebView( key: GlobalKey(), - initialUrl: 'https://flutter.dev/', + initialUrl: primaryUrl, onWebViewCreated: (WebViewController controller) { controllerCompleter.complete(controller); }, @@ -64,9 +71,9 @@ void main() { ), ); final WebViewController controller = await controllerCompleter.future; - await controller.loadUrl('https://www.example.com/'); + await controller.loadUrl(secondaryUrl); final String? currentUrl = await controller.currentUrl(); - expect(currentUrl, 'https://www.example.com/'); + expect(currentUrl, secondaryUrl); }, skip: _skipDueToIssue86757); // TODO(bparrishMines): skipped due to https://github.com/flutter/flutter/issues/86757. @@ -80,7 +87,7 @@ void main() { textDirection: TextDirection.ltr, child: WebView( key: GlobalKey(), - initialUrl: 'https://flutter.dev/', + initialUrl: primaryUrl, onWebViewCreated: (WebViewController controller) { controllerCompleter.complete(controller); }, @@ -298,7 +305,7 @@ void main() { textDirection: TextDirection.ltr, child: WebView( key: _globalKey, - initialUrl: 'https://flutter.dev/', + initialUrl: primaryUrl, javascriptMode: JavascriptMode.unrestricted, onWebViewCreated: (WebViewController controller) { controllerCompleter.complete(controller); @@ -1053,12 +1060,11 @@ void main() { await pageLoads.stream.first; // Wait for initial page load. final WebViewController controller = await controllerCompleter.future; - await controller - .evaluateJavascript('location.href = "https://www.example.com/"'); + await controller.evaluateJavascript('location.href = "$secondaryUrl"'); await pageLoads.stream.first; // Wait for the next page load. final String? currentUrl = await controller.currentUrl(); - expect(currentUrl, 'https://www.example.com/'); + expect(currentUrl, secondaryUrl); }); testWidgets('onWebResourceError', (WidgetTester tester) async { @@ -1220,12 +1226,11 @@ void main() { await pageLoads.stream.first; // Wait for initial page load. final WebViewController controller = await controllerCompleter.future; - await controller - .evaluateJavascript('location.href = "https://www.example.com"'); + await controller.evaluateJavascript('location.href = "$secondaryUrl"'); await pageLoads.stream.first; // Wait for second page to load. final String? currentUrl = await controller.currentUrl(); - expect(currentUrl, 'https://www.example.com/'); + expect(currentUrl, secondaryUrl); }); }); @@ -1241,7 +1246,7 @@ void main() { height: 300, child: WebView( key: GlobalKey(), - initialUrl: 'https://flutter.dev/', + initialUrl: primaryUrl, gestureNavigationEnabled: true, onWebViewCreated: (WebViewController controller) { controllerCompleter.complete(controller); @@ -1252,7 +1257,7 @@ void main() { ); final WebViewController controller = await controllerCompleter.future; final String? currentUrl = await controller.currentUrl(); - expect(currentUrl, 'https://flutter.dev/'); + expect(currentUrl, primaryUrl); }); testWidgets('target _blank opens in same window', @@ -1276,11 +1281,10 @@ void main() { ), ); final WebViewController controller = await controllerCompleter.future; - await controller - .evaluateJavascript('window.open("https://flutter.dev/", "_blank")'); + await controller.evaluateJavascript('window.open("$primaryUrl", "_blank")'); await pageLoaded.future; final String? currentUrl = await controller.currentUrl(); - expect(currentUrl, 'https://flutter.dev/'); + expect(currentUrl, primaryUrl); }, // Flaky on Android: https://github.com/flutter/flutter/issues/86757 skip: _skipDueToIssue86757); @@ -1304,25 +1308,24 @@ void main() { onPageFinished: (String url) { pageLoaded.complete(); }, - initialUrl: 'https://flutter.dev', + initialUrl: primaryUrl, ), ), ); final WebViewController controller = await controllerCompleter.future; - expect(controller.currentUrl(), completion('https://flutter.dev/')); + expect(controller.currentUrl(), completion(primaryUrl)); await pageLoaded.future; pageLoaded = Completer(); - await controller - .evaluateJavascript('window.open("https://www.example.com/")'); + await controller.evaluateJavascript('window.open("$secondaryUrl")'); await pageLoaded.future; pageLoaded = Completer(); - expect(controller.currentUrl(), completion('https://www.example.com/')); + expect(controller.currentUrl(), completion(secondaryUrl)); expect(controller.canGoBack(), completion(true)); await controller.goBack(); await pageLoaded.future; - expect(controller.currentUrl(), completion('https://flutter.dev/')); + expect(controller.currentUrl(), completion(primaryUrl)); }, skip: _skipDueToIssue86757, ); diff --git a/packages/webview_flutter/webview_flutter_wkwebview/example/integration_test/webview_flutter_test.dart b/packages/webview_flutter/webview_flutter_wkwebview/example/integration_test/webview_flutter_test.dart index c080c46120ec..8ba17a2428c5 100644 --- a/packages/webview_flutter/webview_flutter_wkwebview/example/integration_test/webview_flutter_test.dart +++ b/packages/webview_flutter/webview_flutter_wkwebview/example/integration_test/webview_flutter_test.dart @@ -20,6 +20,13 @@ import 'package:webview_flutter_wkwebview_example/web_view.dart'; void main() { IntegrationTestWidgetsFlutterBinding.ensureInitialized(); + // URLs to navigate to in tests. These need to be URLs that we are confident will + // always be accessible, and won't do redirection. (E.g., just + // 'https://www.google.com/' will sometimes redirect traffic that looks + // like it's coming from a bot, which is true of these tests). + const String primaryUrl = 'https://flutter.dev/'; + const String secondaryUrl = 'https://www.google.com/robots.txt'; + // Set to `false` to include all flaky tests in the test run. See also https://github.com/flutter/flutter/issues/86757. const bool _skipDueToIssue86757 = false; @@ -32,7 +39,7 @@ void main() { textDirection: TextDirection.ltr, child: WebView( key: GlobalKey(), - initialUrl: 'https://flutter.dev/', + initialUrl: primaryUrl, onWebViewCreated: (WebViewController controller) { controllerCompleter.complete(controller); }, @@ -41,7 +48,7 @@ void main() { ); final WebViewController controller = await controllerCompleter.future; final String? currentUrl = await controller.currentUrl(); - expect(currentUrl, 'https://flutter.dev/'); + expect(currentUrl, primaryUrl); }, skip: _skipDueToIssue86757); // TODO(bparrishMines): skipped due to https://github.com/flutter/flutter/issues/86757. @@ -53,7 +60,7 @@ void main() { textDirection: TextDirection.ltr, child: WebView( key: GlobalKey(), - initialUrl: 'https://flutter.dev/', + initialUrl: primaryUrl, onWebViewCreated: (WebViewController controller) { controllerCompleter.complete(controller); }, @@ -61,9 +68,9 @@ void main() { ), ); final WebViewController controller = await controllerCompleter.future; - await controller.loadUrl('https://www.example.com/'); + await controller.loadUrl(secondaryUrl); final String? currentUrl = await controller.currentUrl(); - expect(currentUrl, 'https://www.example.com/'); + expect(currentUrl, secondaryUrl); }, skip: _skipDueToIssue86757); testWidgets('loadUrl with headers', (WidgetTester tester) async { @@ -76,7 +83,7 @@ void main() { textDirection: TextDirection.ltr, child: WebView( key: GlobalKey(), - initialUrl: 'https://flutter.dev/', + initialUrl: primaryUrl, onWebViewCreated: (WebViewController controller) { controllerCompleter.complete(controller); }, @@ -292,7 +299,7 @@ void main() { textDirection: TextDirection.ltr, child: WebView( key: _globalKey, - initialUrl: 'https://flutter.dev/', + initialUrl: primaryUrl, javascriptMode: JavascriptMode.unrestricted, onWebViewCreated: (WebViewController controller) { controllerCompleter.complete(controller); @@ -912,12 +919,11 @@ void main() { await pageLoads.stream.first; // Wait for initial page load. final WebViewController controller = await controllerCompleter.future; - await controller - .evaluateJavascript('location.href = "https://www.example.com/"'); + await controller.evaluateJavascript('location.href = "$secondaryUrl"'); await pageLoads.stream.first; // Wait for the next page load. final String? currentUrl = await controller.currentUrl(); - expect(currentUrl, 'https://www.example.com/'); + expect(currentUrl, secondaryUrl); }); testWidgets('onWebResourceError', (WidgetTester tester) async { @@ -1084,12 +1090,11 @@ void main() { await pageLoads.stream.first; // Wait for initial page load. final WebViewController controller = await controllerCompleter.future; - await controller - .evaluateJavascript('location.href = "https://www.example.com"'); + await controller.evaluateJavascript('location.href = "$secondaryUrl"'); await pageLoads.stream.first; // Wait for second page to load. final String? currentUrl = await controller.currentUrl(); - expect(currentUrl, 'https://www.example.com/'); + expect(currentUrl, secondaryUrl); }); }); @@ -1105,7 +1110,7 @@ void main() { height: 300, child: WebView( key: GlobalKey(), - initialUrl: 'https://flutter.dev/', + initialUrl: primaryUrl, gestureNavigationEnabled: true, onWebViewCreated: (WebViewController controller) { controllerCompleter.complete(controller); @@ -1116,7 +1121,7 @@ void main() { ); final WebViewController controller = await controllerCompleter.future; final String? currentUrl = await controller.currentUrl(); - expect(currentUrl, 'https://flutter.dev/'); + expect(currentUrl, primaryUrl); }); testWidgets('target _blank opens in same window', @@ -1140,11 +1145,10 @@ void main() { ), ); final WebViewController controller = await controllerCompleter.future; - await controller - .evaluateJavascript('window.open("https://flutter.dev/", "_blank")'); + await controller.evaluateJavascript('window.open("$primaryUrl", "_blank")'); await pageLoaded.future; final String? currentUrl = await controller.currentUrl(); - expect(currentUrl, 'https://flutter.dev/'); + expect(currentUrl, primaryUrl); }); // TODO(bparrishMines): skipped due to https://github.com/flutter/flutter/issues/86757. @@ -1166,25 +1170,24 @@ void main() { onPageFinished: (String url) { pageLoaded.complete(); }, - initialUrl: 'https://flutter.dev', + initialUrl: primaryUrl, ), ), ); final WebViewController controller = await controllerCompleter.future; - expect(controller.currentUrl(), completion('https://flutter.dev/')); + expect(controller.currentUrl(), completion(primaryUrl)); await pageLoaded.future; pageLoaded = Completer(); - await controller - .evaluateJavascript('window.open("https://www.example.com/")'); + await controller.evaluateJavascript('window.open("$secondaryUrl")'); await pageLoaded.future; pageLoaded = Completer(); - expect(controller.currentUrl(), completion('https://www.example.com/')); + expect(controller.currentUrl(), completion(secondaryUrl)); expect(controller.canGoBack(), completion(true)); await controller.goBack(); await pageLoaded.future; - expect(controller.currentUrl(), completion('https://flutter.dev/')); + expect(controller.currentUrl(), completion(primaryUrl)); }, skip: _skipDueToIssue86757, );