flutter_flutter/packages/integration_test/lib/integration_test_driver_extended.dart
Kate Lovett 9d96df2364
Modernize framework lints (#179089)
WIP

Commits separated as follows:
- Update lints in analysis_options files
- Run `dart fix --apply`
- Clean up leftover analysis issues 
- Run `dart format .` in the right places.

Local analysis and testing passes. Checking CI now.

Part of https://github.com/flutter/flutter/issues/178827
- Adoption of flutter_lints in examples/api coming in a separate change
(cc @loic-sharma)

## Pre-launch Checklist

- [ ] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [ ] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [ ] I read and followed the [Flutter Style Guide], including [Features
we expect every widget to implement].
- [ ] I signed the [CLA].
- [ ] I listed at least one issue that this PR fixes in the description
above.
- [ ] I updated/added relevant documentation (doc comments with `///`).
- [ ] I added new tests to check the change I am making, or this PR is
[test-exempt].
- [ ] I followed the [breaking change policy] and added [Data Driven
Fixes] where supported.
- [ ] All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel
on [Discord].

**Note**: The Flutter team is currently trialing the use of [Gemini Code
Assist for
GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code).
Comments from the `gemini-code-assist` bot should not be taken as
authoritative feedback from the Flutter team. If you find its comments
useful you can update your code accordingly, but if you are unsure or
disagree with the feedback, please feel free to wait for a Flutter team
member's review for guidance on which automated comments should be
addressed.

<!-- Links -->
[Contributor Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview
[Tree Hygiene]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md
[test-exempt]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests
[Flutter Style Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md
[Features we expect every widget to implement]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement
[CLA]: https://cla.developers.google.com/
[flutter/tests]: https://github.com/flutter/tests
[breaking change policy]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes
[Discord]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md
[Data Driven Fixes]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
2025-11-26 01:10:39 +00:00

186 lines
6.8 KiB
Dart

// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// This is a CLI library; we use prints as part of the interface.
// ignore_for_file: avoid_print
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'package:flutter_driver/flutter_driver.dart';
import 'package:path/path.dart' as path;
import 'common.dart';
export 'package:flutter_driver/flutter_driver.dart' show testOutputsDirectory;
/// The callback type to handle [Response.data] after the test
/// succeeds.
typedef ResponseDataCallback = FutureOr<void> Function(Map<String, dynamic>?);
/// Writes a json-serializable data to
/// [testOutputsDirectory]/`testOutputFilename.json`.
///
/// This is the default `responseDataCallback` in [integrationDriver].
Future<void> writeResponseData(
Map<String, dynamic>? data, {
String testOutputFilename = 'integration_response_data',
String? destinationDirectory,
}) async {
destinationDirectory ??= testOutputsDirectory;
await fs.directory(destinationDirectory).create(recursive: true);
final File file = fs.file(path.join(destinationDirectory, '$testOutputFilename.json'));
final String resultString = _encodeJson(data, true);
await file.writeAsString(resultString);
}
/// Adaptor to run an integration test using `flutter drive`.
///
/// To an integration test `<test_name>.dart` using `flutter drive`, put a file named
/// `<test_name>_test.dart` in the app's `test_driver` directory:
///
/// ```dart
/// import 'dart:async';
///
/// import 'package:flutter_driver/flutter_driver.dart';
/// import 'package:integration_test/integration_test_driver_extended.dart';
///
/// Future<void> main() async {
/// final FlutterDriver driver = await FlutterDriver.connect();
/// await integrationDriver(
/// driver: driver,
/// onScreenshot: (String name, List<int> image, [Map<String, Object?>? args]) async {
/// return true;
/// },
/// );
/// }
/// ```
///
/// ## Parameters:
///
/// `driver` A custom driver. Defaults to `FlutterDriver.connect()`.
///
/// `onScreenshot` can be used to process the screenshots taken during the test.
/// An example could be that this callback compares the byte array against a baseline image,
/// and it returns `true` if both images are equal.
///
/// As a result, returning `false` from `onScreenshot` will make the test fail.
///
/// `responseDataCallback` is the handler for processing [Response.data].
/// The default value is `writeResponseData`.
///
/// `writeResponseOnFailure` determines whether the `responseDataCallback`
/// function will be called to process the [Response.data] when a test fails.
/// The default value is `false`.
Future<void> integrationDriver({
FlutterDriver? driver,
ScreenshotCallback? onScreenshot,
ResponseDataCallback? responseDataCallback = writeResponseData,
bool writeResponseOnFailure = false,
}) async {
driver ??= await FlutterDriver.connect();
// Test states that it's waiting on web driver commands.
// [DriverTestMessage] is converted to string since json format causes an
// error if it's used as a message for requestData.
String jsonResponse = await driver.requestData(DriverTestMessage.pending().toString());
final onScreenshotResults = <String, bool>{};
Response response = Response.fromJson(jsonResponse);
// Until `integration_test` returns a [WebDriverCommandType.noop], keep
// executing WebDriver commands.
while (response.data != null &&
response.data!['web_driver_command'] != null &&
response.data!['web_driver_command'] != '${WebDriverCommandType.noop}') {
final webDriverCommand = response.data!['web_driver_command'] as String?;
if (webDriverCommand == '${WebDriverCommandType.screenshot}') {
assert(onScreenshot != null, 'screenshot command requires an onScreenshot callback');
// Use `driver.screenshot()` method to get a screenshot of the web page.
final List<int> screenshotImage = await driver.screenshot();
final screenshotName = response.data!['screenshot_name']! as String;
final Map<String, Object?>? args = (response.data!['args'] as Map<String, Object?>?)
?.cast<String, Object?>();
final bool screenshotSuccess = await onScreenshot!(screenshotName, screenshotImage, args);
onScreenshotResults[screenshotName] = screenshotSuccess;
if (screenshotSuccess) {
jsonResponse = await driver.requestData(DriverTestMessage.complete().toString());
} else {
jsonResponse = await driver.requestData(DriverTestMessage.error().toString());
}
response = Response.fromJson(jsonResponse);
} else if (webDriverCommand == '${WebDriverCommandType.ack}') {
// Previous command completed ask for a new one.
jsonResponse = await driver.requestData(DriverTestMessage.pending().toString());
response = Response.fromJson(jsonResponse);
} else {
break;
}
}
// If No-op command is sent, ask for the result of all tests.
if (response.data != null &&
response.data!['web_driver_command'] != null &&
response.data!['web_driver_command'] == '${WebDriverCommandType.noop}') {
jsonResponse = await driver.requestData(null);
response = Response.fromJson(jsonResponse);
print('result $jsonResponse');
}
if (response.data != null && response.data!['screenshots'] != null && onScreenshot != null) {
final screenshots = response.data!['screenshots'] as List<dynamic>;
final failures = <String>[];
for (final dynamic screenshot in screenshots) {
final data = screenshot as Map<String, dynamic>;
final screenshotBytes = data['bytes'] as List<dynamic>;
final screenshotName = data['screenshotName'] as String;
var ok = false;
try {
ok =
onScreenshotResults[screenshotName] ??
await onScreenshot(screenshotName, screenshotBytes.cast<int>());
} catch (exception) {
throw StateError(
'Screenshot failure:\n'
'onScreenshot("$screenshotName", <bytes>) threw an exception: $exception',
);
}
if (!ok) {
failures.add(screenshotName);
}
}
if (failures.isNotEmpty) {
throw StateError('The following screenshot tests failed: ${failures.join(', ')}');
}
}
await driver.close();
if (response.allTestsPassed) {
print('All tests passed.');
if (responseDataCallback != null) {
await responseDataCallback(response.data);
}
exit(0);
} else {
print('Failure Details:\n${response.formattedFailureDetails}');
if (responseDataCallback != null && writeResponseOnFailure) {
await responseDataCallback(response.data);
}
exit(1);
}
}
const JsonEncoder _prettyEncoder = JsonEncoder.withIndent(' ');
String _encodeJson(Map<String, dynamic>? jsonObject, bool pretty) {
return pretty ? _prettyEncoder.convert(jsonObject) : json.encode(jsonObject);
}