diff --git a/dev/bots/test.dart b/dev/bots/test.dart index 68d1b583c4..e6919b72ab 100644 --- a/dev/bots/test.dart +++ b/dev/bots/test.dart @@ -1138,7 +1138,7 @@ Future _runWebUnitTests(String webRenderer) async { /// Coarse-grained integration tests running on the Web. Future _runWebLongRunningTests() async { final List tests = [ - for (String buildMode in _kAllBuildModes) + for (String buildMode in _kAllBuildModes) ...[ () => _runFlutterDriverWebTest( testAppDirectory: path.join('packages', 'integration_test', 'example'), target: path.join('test_driver', 'failure.dart'), @@ -1148,6 +1148,21 @@ Future _runWebLongRunningTests() async { // logs. To avoid confusion, silence browser output. silenceBrowserOutput: true, ), + () => _runFlutterDriverWebTest( + testAppDirectory: path.join('packages', 'integration_test', 'example'), + target: path.join('integration_test', 'example_test.dart'), + driver: path.join('test_driver', 'integration_test.dart'), + buildMode: buildMode, + renderer: 'canvaskit', + ), + () => _runFlutterDriverWebTest( + testAppDirectory: path.join('packages', 'integration_test', 'example'), + target: path.join('integration_test', 'extended_test.dart'), + driver: path.join('test_driver', 'extended_integration_test.dart'), + buildMode: buildMode, + renderer: 'canvaskit', + ), + ], // This test specifically tests how images are loaded in HTML mode, so we don't run it in CanvasKit mode. () => _runWebE2eTest('image_loading_integration', buildMode: 'debug', renderer: 'html'), @@ -1281,6 +1296,7 @@ Future _runFlutterDriverWebTest({ required String buildMode, required String renderer, required String testAppDirectory, + String? driver, bool expectFailure = false, bool silenceBrowserOutput = false, }) async { @@ -1295,6 +1311,7 @@ Future _runFlutterDriverWebTest({ [ ...flutterTestArgs, 'drive', + if (driver != null) '--driver=$driver', '--target=$target', '--browser-name=chrome', '--no-sound-null-safety', diff --git a/dev/integration_tests/web_e2e_tests/lib/screenshot_support.dart b/dev/integration_tests/web_e2e_tests/lib/screenshot_support.dart index def467130d..5a46b8e398 100644 --- a/dev/integration_tests/web_e2e_tests/lib/screenshot_support.dart +++ b/dev/integration_tests/web_e2e_tests/lib/screenshot_support.dart @@ -25,7 +25,7 @@ Future runTestWithScreenshots({ test.integrationDriver( driver: driver, - onScreenshot: (String screenshotName, List screenshotBytes) async { + onScreenshot: (String screenshotName, List screenshotBytes, [Map? args]) async { // TODO(yjbanov): implement, see https://github.com/flutter/flutter/issues/86120 return true; }, diff --git a/packages/integration_test/example/integration_test/_extended_test_web.dart b/packages/integration_test/example/integration_test/_extended_test_web.dart index b95b157073..5868e5c6a8 100644 --- a/packages/integration_test/example/integration_test/_extended_test_web.dart +++ b/packages/integration_test/example/integration_test/_extended_test_web.dart @@ -17,7 +17,8 @@ import 'package:integration_test/integration_test.dart'; import 'package:integration_test_example/main.dart' as app; void main() { - final IntegrationTestWidgetsFlutterBinding binding = IntegrationTestWidgetsFlutterBinding.ensureInitialized(); + final IntegrationTestWidgetsFlutterBinding binding = + IntegrationTestWidgetsFlutterBinding.ensureInitialized(); testWidgets('verify text', (WidgetTester tester) async { // Build our app and trigger a frame. @@ -27,7 +28,18 @@ void main() { await tester.pumpAndSettle(); // Take a screenshot. - await binding.takeScreenshot('platform_name'); + await binding.takeScreenshot( + 'platform_name', + // The optional parameter 'args' can be used to pass values to the + // [integrationDriver.onScreenshot] handler + // (see test_driver/extended_integration_test.dart). For example, you + // could look up environment variables in this test that were passed to + // the run command via `--dart-define=`, and then pass the values to the + // [integrationDriver.onScreenshot] handler through this 'args' map. + { + 'someArgumentKey': 'someArgumentValue', + }, + ); // Verify that platform is retrieved. expect( @@ -49,7 +61,20 @@ void main() { await tester.pumpAndSettle(); // Multiple methods can take screenshots. Screenshots are taken with the - // same order the methods run. - await binding.takeScreenshot('platform_name_2'); + // same order the methods run. We pass an argument that can be looked up + // from the [onScreenshot] handler in + // [test_driver/extended_integration_test.dart]. + await binding.takeScreenshot( + 'platform_name_2', + // The optional parameter 'args' can be used to pass values to the + // [integrationDriver.onScreenshot] handler + // (see test_driver/extended_integration_test.dart). For example, you + // could look up environment variables in this test that were passed to + // the run command via `--dart-define=`, and then pass the values to the + // [integrationDriver.onScreenshot] handler through this 'args' map. + { + 'someArgumentKey': 'someArgumentValue', + }, + ); }); } diff --git a/packages/integration_test/example/test_driver/extended_integration_test.dart b/packages/integration_test/example/test_driver/extended_integration_test.dart index 7595392f04..e2dabae450 100644 --- a/packages/integration_test/example/test_driver/extended_integration_test.dart +++ b/packages/integration_test/example/test_driver/extended_integration_test.dart @@ -9,8 +9,20 @@ Future main() async { final FlutterDriver driver = await FlutterDriver.connect(); await integrationDriver( driver: driver, - onScreenshot: (String screenshotName, List screenshotBytes) async { + onScreenshot: ( + String screenshotName, + List screenshotBytes, [ + Map? args, + ]) async { // Return false if the screenshot is invalid. + // TODO(yjbanov): implement, see https://github.com/flutter/flutter/issues/86120 + + // Here is an example of using an argument that was passed in via the + // optional 'args' Map. + if (args != null) { + final String? someArgumentValue = args['someArgumentKey'] as String?; + return someArgumentValue != null; + } return true; }, ); diff --git a/packages/integration_test/lib/_callback_io.dart b/packages/integration_test/lib/_callback_io.dart index 662b6af227..b7efa20557 100644 --- a/packages/integration_test/lib/_callback_io.dart +++ b/packages/integration_test/lib/_callback_io.dart @@ -86,7 +86,8 @@ class IOCallbackManager implements CallbackManager { } @override - Future> takeScreenshot(String screenshot) async { + Future> takeScreenshot(String screenshot, [Map? args]) async { + assert(args == null, '[args] handling has not been implemented for this platform'); if (Platform.isAndroid && !_isSurfaceRendered) { throw StateError('Call convertFlutterSurfaceToImage() before taking a screenshot'); } diff --git a/packages/integration_test/lib/_callback_web.dart b/packages/integration_test/lib/_callback_web.dart index fa9a0bd8c9..3f588b8cab 100644 --- a/packages/integration_test/lib/_callback_web.dart +++ b/packages/integration_test/lib/_callback_web.dart @@ -44,10 +44,13 @@ class WebCallbackManager implements CallbackManager { /// /// See: https://www.w3.org/TR/webdriver/#screen-capture. @override - Future> takeScreenshot(String screenshotName) async { - await _sendWebDriverCommand(WebDriverCommand.screenshot(screenshotName)); - // Flutter Web doesn't provide the bytes. - return const {'bytes': []}; + Future> takeScreenshot(String screenshotName, [Map? args]) async { + await _sendWebDriverCommand(WebDriverCommand.screenshot(screenshotName, args)); + return { + 'screenshotName': screenshotName, + // Flutter Web doesn't provide the bytes. + 'bytes': [] + }; } @override diff --git a/packages/integration_test/lib/common.dart b/packages/integration_test/lib/common.dart index 428b52aa83..81558e104c 100644 --- a/packages/integration_test/lib/common.dart +++ b/packages/integration_test/lib/common.dart @@ -7,17 +7,23 @@ import 'dart:convert'; /// A callback to use with [integrationDriver]. /// -/// The callback receives the name of screenshot passed to `binding.takeScreenshot()` and -/// a PNG byte buffer. +/// The callback receives the name of screenshot passed to `binding.takeScreenshot()`, +/// a PNG byte buffer representing the screenshot, and an optional `Map` of arguments. /// /// The callback returns `true` if the test passes or `false` otherwise. /// /// You can use this callback to store the bytes locally in a file or upload them to a service /// that compares the image against a gold or baseline version. /// +/// The optional `Map` of arguments can be passed from the +/// `binding.takeScreenshot(, )` callsite in the integration test, +/// and then the arguments can be used in the `onScreenshot` handler that is defined by +/// the Flutter driver. This `Map` should only contain values that can be serialized +/// to JSON. +/// /// Since the function is executed on the host driving the test, you can access any environment /// variable from it. -typedef ScreenshotCallback = Future Function(String name, List image); +typedef ScreenshotCallback = Future Function(String name, List image, [Map? args]); /// Classes shared between `integration_test.dart` and `flutter drive` based /// adoptor (ex: `integration_test_driver.dart`). @@ -247,9 +253,12 @@ class WebDriverCommand { values = {}; /// Constructor for [WebDriverCommandType.noop] screenshot. - WebDriverCommand.screenshot(String screenshotName) + WebDriverCommand.screenshot(String screenshotName, [Map? args]) : type = WebDriverCommandType.screenshot, - values = {'screenshot_name': screenshotName}; + values = { + 'screenshot_name': screenshotName, + if (args != null) 'args': args, + }; /// Type of the [WebDriverCommand]. /// @@ -286,7 +295,7 @@ abstract class CallbackManager { /// Takes a screenshot of the application. /// Returns the data that is sent back to the host. - Future> takeScreenshot(String screenshot); + Future> takeScreenshot(String screenshot, [Map? args]); /// Android only. Converts the Flutter surface to an image view. Future convertFlutterSurfaceToImage(); diff --git a/packages/integration_test/lib/integration_test.dart b/packages/integration_test/lib/integration_test.dart index 816158c857..51517a5280 100644 --- a/packages/integration_test/lib/integration_test.dart +++ b/packages/integration_test/lib/integration_test.dart @@ -184,10 +184,10 @@ https://flutter.dev/docs/testing/integration-tests#testing-on-firebase-test-lab /// /// On Android, you need to call `convertFlutterSurfaceToImage()`, and /// pump a frame before taking a screenshot. - Future> takeScreenshot(String screenshotName) async { + Future> takeScreenshot(String screenshotName, [Map? args]) async { reportData ??= {}; reportData!['screenshots'] ??= []; - final Map data = await callbackManager.takeScreenshot(screenshotName); + final Map data = await callbackManager.takeScreenshot(screenshotName, args); assert(data.containsKey('bytes')); (reportData!['screenshots']! as List).add(data); diff --git a/packages/integration_test/lib/integration_test_driver_extended.dart b/packages/integration_test/lib/integration_test_driver_extended.dart index 6ded1b03c8..d449d7df98 100644 --- a/packages/integration_test/lib/integration_test_driver_extended.dart +++ b/packages/integration_test/lib/integration_test_driver_extended.dart @@ -50,6 +50,8 @@ Future integrationDriver( // error if it's used as a message for requestData. String jsonResponse = await driver.requestData(DriverTestMessage.pending().toString()); + final Map onScreenshotResults = {}; + Response response = Response.fromJson(jsonResponse); // Until `integration_test` returns a [WebDriverCommandType.noop], keep @@ -63,8 +65,10 @@ Future integrationDriver( // Use `driver.screenshot()` method to get a screenshot of the web page. final List screenshotImage = await driver.screenshot(); final String screenshotName = response.data!['screenshot_name']! as String; + final Map? args = (response.data!['args'] as Map?)?.cast(); - final bool screenshotSuccess = await onScreenshot!(screenshotName, screenshotImage); + final bool screenshotSuccess = await onScreenshot!(screenshotName, screenshotImage, args); + onScreenshotResults[screenshotName] = screenshotSuccess; if (screenshotSuccess) { jsonResponse = await driver.requestData(DriverTestMessage.complete().toString()); } else { @@ -104,7 +108,8 @@ Future integrationDriver( bool ok = false; try { - ok = await onScreenshot(screenshotName, screenshotBytes.cast()); + ok = onScreenshotResults[screenshotName] ?? + await onScreenshot(screenshotName, screenshotBytes.cast()); } catch (exception) { throw StateError( 'Screenshot failure:\n'