e2e_test.dart 1.71 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
// 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.

import 'dart:async';
import 'dart:convert';
import 'dart:io';

import 'package:e2e/common.dart' as e2e;
import 'package:flutter_driver/flutter_driver.dart';

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

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

/// 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';

String testOutputFilename = 'e2e_perf_summary';

Future<void> main() async {
  final FlutterDriver driver = await FlutterDriver.connect();
  final String jsonResult =
      await driver.requestData(null, timeout: const Duration(minutes: 1));
  final e2e.Response response = e2e.Response.fromJson(jsonResult);
  await driver.close();

  if (response.allTestsPassed) {
    print('All tests passed.');

    await fs.directory(testOutputsDirectory).create(recursive: true);
    final File file = fs.file(path.join(
      testOutputsDirectory,
      '$testOutputFilename.json'
    ));
    final String resultString = _encodeJson(
      response.data['performance'] as Map<String, dynamic>,
      true,
    );
    await file.writeAsString(resultString);

    exit(0);
  } else {
    print('Failure Details:\n${response.formattedFailureDetails}');
    exit(1);
  }
}

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