Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit fbd6510

Browse files
authored
[e2e] add support to report extra information (#2873)
* add extra info report
1 parent 5971e2f commit fbd6510

File tree

6 files changed

+144
-31
lines changed

6 files changed

+144
-31
lines changed

packages/e2e/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.6.1
2+
3+
* Added `data` in the reported json.
4+
15
## 0.6.0
26

37
* **Breaking change** `E2EPlugin` exports a `Future` for `testResults`.

packages/e2e/lib/common.dart

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,17 @@ class Response {
1111

1212
final bool _allTestsPassed;
1313

14+
/// The extra information to be added along side the test result.
15+
Map<String, dynamic> data;
16+
1417
/// Constructor to use for positive response.
15-
Response.allTestsPassed()
18+
Response.allTestsPassed({this.data})
1619
: this._allTestsPassed = true,
1720
this._failureDetails = null;
1821

1922
/// Constructor for failure response.
20-
Response.someTestsFailed(this._failureDetails) : this._allTestsPassed = false;
23+
Response.someTestsFailed(this._failureDetails, {this.data})
24+
: this._allTestsPassed = false;
2125

2226
/// Whether the test ran successfully or not.
2327
bool get allTestsPassed => _allTestsPassed;
@@ -33,16 +37,19 @@ class Response {
3337
String toJson() => json.encode(<String, dynamic>{
3438
'result': allTestsPassed.toString(),
3539
'failureDetails': _failureDetailsAsString(),
40+
if (data != null) 'data': data
3641
});
3742

3843
/// Deserializes the result from JSON.
3944
static Response fromJson(String source) {
40-
Map<String, dynamic> responseJson = json.decode(source);
41-
if (responseJson['result'] == 'true') {
42-
return Response.allTestsPassed();
45+
final Map<String, dynamic> responseJson = json.decode(source);
46+
if (responseJson['result'] as String == 'true') {
47+
return Response.allTestsPassed(data: responseJson['data']);
4348
} else {
4449
return Response.someTestsFailed(
45-
_failureDetailsFromJson(responseJson['failureDetails']));
50+
_failureDetailsFromJson(responseJson['failureDetails']),
51+
data: responseJson['data'],
52+
);
4653
}
4754
}
4855

packages/e2e/lib/e2e.dart

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -78,33 +78,47 @@ class E2EWidgetsFlutterBinding extends LiveTestWidgetsFlutterBinding {
7878

7979
static Map<String, String> _results = <String, String>{};
8080

81+
/// The extra data for the reported result.
82+
///
83+
/// The values in `reportData` must be json-serializable objects or `null`.
84+
/// If it's `null`, no extra data is attached to the result.
85+
///
86+
/// The default value is `null`.
87+
Map<String, dynamic> reportData;
88+
89+
/// the callback function to response the driver side input.
90+
@visibleForTesting
91+
Future<Map<String, dynamic>> callback(Map<String, String> params) async {
92+
final String command = params['command'];
93+
Map<String, String> response;
94+
switch (command) {
95+
case 'request_data':
96+
final bool allTestsPassed = await _allTestsPassed.future;
97+
response = <String, String>{
98+
'message': allTestsPassed
99+
? Response.allTestsPassed(data: reportData).toJson()
100+
: Response.someTestsFailed(
101+
_failureMethodsDetails,
102+
data: reportData,
103+
).toJson(),
104+
};
105+
break;
106+
case 'get_health':
107+
response = <String, String>{'status': 'ok'};
108+
break;
109+
default:
110+
throw UnimplementedError('$command is not implemented');
111+
}
112+
return <String, dynamic>{
113+
'isError': false,
114+
'response': response,
115+
};
116+
}
117+
81118
// Emulates the Flutter driver extension, returning 'pass' or 'fail'.
82119
@override
83120
void initServiceExtensions() {
84121
super.initServiceExtensions();
85-
Future<Map<String, dynamic>> callback(Map<String, String> params) async {
86-
final String command = params['command'];
87-
Map<String, String> response;
88-
switch (command) {
89-
case 'request_data':
90-
final bool allTestsPassed = await _allTestsPassed.future;
91-
response = <String, String>{
92-
'message': allTestsPassed
93-
? Response.allTestsPassed().toJson()
94-
: Response.someTestsFailed(_failureMethodsDetails).toJson(),
95-
};
96-
break;
97-
case 'get_health':
98-
response = <String, String>{'status': 'ok'};
99-
break;
100-
default:
101-
throw UnimplementedError('$command is not implemented');
102-
}
103-
return <String, dynamic>{
104-
'isError': false,
105-
'response': response,
106-
};
107-
}
108122

109123
if (kIsWeb) {
110124
registerWebServiceExtension(callback);

packages/e2e/pubspec.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
name: e2e
22
description: Runs tests that use the flutter_test API as integration tests.
3-
version: 0.6.0
3+
version: 0.6.1
44
homepage: https://github.com/flutter/plugins/tree/master/packages/e2e
55

66
environment:
7-
sdk: ">=2.1.0 <3.0.0"
7+
sdk: ">=2.2.2 <3.0.0"
88
flutter: ">=1.12.13+hotfix.5 <2.0.0"
99

1010
dependencies:

packages/e2e/test/binding_test.dart

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import 'package:flutter/material.dart';
2+
3+
import 'package:e2e/e2e.dart';
4+
import 'package:e2e/common.dart';
5+
import 'package:flutter_test/flutter_test.dart';
6+
7+
void main() async {
8+
Future<Map<String, dynamic>> request;
9+
10+
group('Test E2E binding', () {
11+
final WidgetsBinding binding = E2EWidgetsFlutterBinding.ensureInitialized();
12+
assert(binding is E2EWidgetsFlutterBinding);
13+
final E2EWidgetsFlutterBinding e2ebinding =
14+
binding as E2EWidgetsFlutterBinding;
15+
16+
setUp(() {
17+
request = e2ebinding.callback(<String, String>{
18+
'command': 'request_data',
19+
});
20+
});
21+
22+
testWidgets('Run E2E app', (WidgetTester tester) async {
23+
runApp(MaterialApp(
24+
home: Text('Test'),
25+
));
26+
expect(tester.binding, e2ebinding);
27+
e2ebinding.reportData = <String, dynamic>{'answer': 42};
28+
});
29+
});
30+
31+
tearDownAll(() async {
32+
// This part is outside the group so that `request` has been compeleted as
33+
// part of the `tearDownAll` registerred in the group during
34+
// `E2EWidgetsFlutterBinding` initialization.
35+
final Map<String, dynamic> response =
36+
(await request)['response'] as Map<String, dynamic>;
37+
final String message = response['message'] as String;
38+
Response result = Response.fromJson(message);
39+
assert(result.data['answer'] == 42);
40+
});
41+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import 'package:flutter_test/flutter_test.dart';
2+
3+
import 'package:e2e/common.dart';
4+
5+
void main() {
6+
test('Serialize and deserialize Failure', () {
7+
Failure fail = Failure('what a name', 'no detail');
8+
Failure restored = Failure.fromJsonString(fail.toString());
9+
expect(restored.methodName, fail.methodName);
10+
expect(restored.details, fail.details);
11+
});
12+
13+
test('Serialize and deserialize Response', () {
14+
Response response, restored;
15+
String jsonString;
16+
17+
response = Response.allTestsPassed();
18+
jsonString = response.toJson();
19+
expect(jsonString, '{"result":"true","failureDetails":[]}');
20+
restored = Response.fromJson(jsonString);
21+
expect(restored.allTestsPassed, response.allTestsPassed);
22+
expect(restored.data, null);
23+
expect(restored.formattedFailureDetails, '');
24+
25+
final Failure fail = Failure('what a name', 'no detail');
26+
final Failure fail2 = Failure('what a name2', 'no detail2');
27+
response = Response.someTestsFailed([fail, fail2]);
28+
jsonString = response.toJson();
29+
restored = Response.fromJson(jsonString);
30+
expect(restored.allTestsPassed, response.allTestsPassed);
31+
expect(restored.data, null);
32+
expect(restored.formattedFailureDetails, response.formattedFailureDetails);
33+
34+
Map<String, dynamic> data = <String, dynamic>{'aaa': 'bbb'};
35+
response = Response.allTestsPassed(data: data);
36+
jsonString = response.toJson();
37+
restored = Response.fromJson(jsonString);
38+
expect(restored.data.keys, ['aaa']);
39+
expect(restored.data.values, ['bbb']);
40+
41+
response = Response.someTestsFailed([fail, fail2], data: data);
42+
jsonString = response.toJson();
43+
restored = Response.fromJson(jsonString);
44+
expect(restored.data.keys, ['aaa']);
45+
expect(restored.data.values, ['bbb']);
46+
});
47+
}

0 commit comments

Comments
 (0)