From a351866646da05a1bf2d278d4a102731b09fa797 Mon Sep 17 00:00:00 2001 From: Jon Samwell Date: Tue, 24 Sep 2019 09:49:00 +1000 Subject: [PATCH] feat(reporters): added flutter driver reporter chore(docs): updated readme and changelog fix(world): added missing super class to flutter world dispose method --- CHANGELOG.md | 4 +++ README.md | 1 + example/test_driver/app_test.dart | 3 +- lib/flutter_gherkin.dart | 3 ++ lib/src/flutter/flutter_world.dart | 1 + .../reporters/flutter_driver_reporter.dart | 35 +++++++++++++++++++ pubspec.yaml | 2 +- 7 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 lib/src/flutter/reporters/flutter_driver_reporter.dart diff --git a/CHANGELOG.md b/CHANGELOG.md index c0b0e6f..1ecc950 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## [1.1.3] - 24/09/2019 +* Added Flutter driver reporter - the driver logs all messages (even non-error ones) to stderr and will cause the process to be marked as failed by a CI server becuase of this. So this reporter redirect the messages to the appropiate output stream (stdout / stderr). +* FlutterWorld - added missing `super.dispose()` call + ## [1.1.2] - 22/09/2019 * Fixed lint warnings diff --git a/README.md b/README.md index 40dc385..d74f4eb 100644 --- a/README.md +++ b/README.md @@ -868,6 +868,7 @@ A reporter is a class that is able to report on the progress of the test run. In - `ProgressReporter` - prints the result of each scenario and step to the console - colours the output. - `TestRunSummaryReporter` - prints the results and duration of the test run once the run has completed - colours the output. - `JsonReporter` - creates a JSON file with the results of the test run which can then be used by 'https://www.npmjs.com/package/cucumber-html-reporter.' to create a HTML report. You can pass in the file path of the json file to be created. +- `FlutterDriverReporter` - prints the output from Flutter Driver. Flutter driver logs all messages to the stderr stream by default so most CI servers would mark the process as failed if anything is logged to the stderr stream (even if the Flutter driver logs are only info messages). This reporter ensures the log messages are output to the most appropiate stream depending on their log level. You can create your own custom reporter by inheriting from the base `Reporter` class and overriding the one or many of the methods to direct the output message. The `Reporter` defines the following methods that can be overridden. All methods must return a `Future` and can be async. diff --git a/example/test_driver/app_test.dart b/example/test_driver/app_test.dart index 2792a78..8fda7c0 100644 --- a/example/test_driver/app_test.dart +++ b/example/test_driver/app_test.dart @@ -13,7 +13,8 @@ Future main() { ..reporters = [ ProgressReporter(), TestRunSummaryReporter(), - JsonReporter(path: './report.json') + JsonReporter(path: './report.json'), + FlutterDriverReporter() // include this reporter if running on a CI server as Flutter driver logs all output to stderr ] // you can include the "StdoutReporter()" without the message level parameter for verbose log information ..hooks = [ HookExample() diff --git a/lib/flutter_gherkin.dart b/lib/flutter_gherkin.dart index b030b63..540c225 100644 --- a/lib/flutter_gherkin.dart +++ b/lib/flutter_gherkin.dart @@ -15,3 +15,6 @@ export 'src/flutter/steps/restart_app_step.dart'; // Hooks export 'src/flutter/hooks/attach_screenshot_on_failed_step_hook.dart'; + +// Reporters +export 'src/flutter/reporters/flutter_driver_reporter.dart'; diff --git a/lib/src/flutter/flutter_world.dart b/lib/src/flutter/flutter_world.dart index 1db1e65..4375053 100644 --- a/lib/src/flutter/flutter_world.dart +++ b/lib/src/flutter/flutter_world.dart @@ -35,6 +35,7 @@ class FlutterWorld extends World { @override void dispose() async { + super.dispose(); _flutterRunProcessHandler = null; await _driver?.close(); } diff --git a/lib/src/flutter/reporters/flutter_driver_reporter.dart b/lib/src/flutter/reporters/flutter_driver_reporter.dart new file mode 100644 index 0000000..41a15f3 --- /dev/null +++ b/lib/src/flutter/reporters/flutter_driver_reporter.dart @@ -0,0 +1,35 @@ +import 'dart:async'; +import 'dart:io'; + +import 'package:gherkin/gherkin.dart'; +import 'package:flutter_driver/flutter_driver.dart'; + +/// The Flutter driver helpfully logs ALL messages to the stderr output +/// useless something is listening to the messages. +/// This reporter listens to the messages from the driver so nothing is logged +/// to the stderr stream unless it is actually an error. +/// This can cause problems with CI servers for example as they will mark a process as failed if it logs to the +/// stderr stream. So Flutter driver will log a normal info message to the stderr and thus make +/// the process fail from the perspective of a CI server. +class FlutterDriverReporter extends Reporter { + List subscriptions = List(); + + Future onTestRunStarted() async { + subscriptions.add( + flutterDriverLog.where((log) => !isErrorLog(log.level)).listen((log) { + stdout.writeln(log.toString()); + })); + subscriptions.add( + flutterDriverLog.where((log) => isErrorLog(log.level)).listen((log) { + stderr.writeln(log.toString()); + })); + } + + Future dispose() async { + subscriptions.forEach((s) => s.cancel()); + subscriptions.clear(); + } + + bool isErrorLog(LogLevel level) => + level == LogLevel.error || level == LogLevel.critical; +} diff --git a/pubspec.yaml b/pubspec.yaml index 88a40a9..2472e78 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: flutter_gherkin description: A Gherkin / Cucumber parser and test runner for Dart and Flutter -version: 1.1.2 +version: 1.1.3 author: Jon Samwell homepage: https://github.com/jonsamwell/flutter_gherkin