integration_test_driver.dart 3.45 KB
Newer Older
1 2 3 4
// 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.

5 6 7
// This is a CLI library; we use prints as part of the interface.
// ignore_for_file: avoid_print

8 9 10 11 12 13 14
import 'dart:async';
import 'dart:convert';
import 'dart:io';

import 'package:flutter_driver/flutter_driver.dart';
import 'package:path/path.dart' as path;

15 16
import 'common.dart';

17 18 19 20 21 22 23 24 25 26
/// Flutter Driver test output directory.
///
/// Tests should write any output files to this directory. Defaults to the path
/// set in the FLUTTER_TEST_OUTPUTS_DIR environment variable, or `build` if
/// unset.
String testOutputsDirectory =
    Platform.environment['FLUTTER_TEST_OUTPUTS_DIR'] ?? 'build';

/// The callback type to handle [Response.data] after the test
/// succeeds.
27
typedef ResponseDataCallback = FutureOr<void> Function(Map<String, dynamic>?);
28

nt4f04uNd's avatar
nt4f04uNd committed
29
/// Writes a json-serializable data to
30
/// [testOutputsDirectory]/`testOutputFilename.json`.
31 32 33
///
/// This is the default `responseDataCallback` in [integrationDriver].
Future<void> writeResponseData(
34
  Map<String, dynamic>? data, {
35
  String testOutputFilename = 'integration_response_data',
36
  String? destinationDirectory,
37 38 39
}) async {
  destinationDirectory ??= testOutputsDirectory;
  await fs.directory(destinationDirectory).create(recursive: true);
40 41 42 43 44 45
  final File file = fs.file(path.join(
    destinationDirectory,
    '$testOutputFilename.json',
  ));
  final String resultString = _encodeJson(data, true);
  await file.writeAsString(resultString);
46 47 48 49
}

/// Adaptor to run an integration test using `flutter drive`.
///
50
/// To run an integration test `<test_name>.dart` using `flutter drive`, put a file named
51 52 53 54 55 56 57 58 59 60
/// `<test_name>_test.dart` in the app's `test_driver` directory:
///
/// ```dart
/// import 'dart:async';
///
/// import 'package:integration_test/integration_test_driver.dart';
///
/// Future<void> main() async => integrationDriver();
///
/// ```
61 62 63 64 65 66 67 68 69
///
/// ## Parameters:
///
/// `timeout` controls the longest time waited before the test ends.
/// It is not necessarily the execution time for the test app: the test may
/// finish sooner than the `timeout`.
///
/// `responseDataCallback` is the handler for processing [Response.data].
/// The default value is `writeResponseData`.
70 71 72 73
///
/// `writeResponseOnFailure` determines whether the `responseDataCallback`
/// function will be called to process the [Response.data] when a test fails.
/// The default value is `false`.
74
Future<void> integrationDriver({
75
  Duration timeout = const Duration(minutes: 20),
76
  ResponseDataCallback? responseDataCallback = writeResponseData,
77
  bool writeResponseOnFailure = false,
78 79 80 81
}) async {
  final FlutterDriver driver = await FlutterDriver.connect();
  final String jsonResult = await driver.requestData(null, timeout: timeout);
  final Response response = Response.fromJson(jsonResult);
82

83 84 85 86 87 88 89 90 91 92
  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}');
93 94 95
    if (responseDataCallback != null && writeResponseOnFailure) {
      await responseDataCallback(response.data);
    }
96 97 98 99 100 101
    exit(1);
  }
}

const JsonEncoder _prettyEncoder = JsonEncoder.withIndent('  ');

102
String _encodeJson(Map<String, dynamic>? jsonObject, bool pretty) {
103 104
  return pretty ? _prettyEncoder.convert(jsonObject) : json.encode(jsonObject);
}