Skip to content

Commit e4dc3a4

Browse files
authored
Merge pull request #2420 from leancodepl/change-patrol-logs-in-finders
Wrap actions on finders with patrol logs.
2 parents bc76dda + 8813f55 commit e4dc3a4

File tree

4 files changed

+116
-47
lines changed

4 files changed

+116
-47
lines changed

packages/patrol_finders/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 2.4.0
2+
3+
- Wrap actions on finders with patrol logs.
4+
15
## 2.3.0
26

37
- Add option to disable printing patrol logs. Default value is disabled.

packages/patrol_finders/lib/src/custom_finders/patrol_finder.dart

Lines changed: 105 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import 'package:flutter_test/flutter_test.dart';
44
import 'package:meta/meta.dart';
55
import 'package:patrol_finders/patrol_finders.dart';
66
import 'package:patrol_finders/src/custom_finders/utils.dart';
7+
import 'package:patrol_log/patrol_log.dart';
78

89
/// Thrown when some [PatrolFinder]'s method fails.
910
class PatrolFinderException implements Exception {
@@ -174,6 +175,45 @@ class PatrolFinder implements MatchFinder {
174175
/// [PatrolTester] that this [PatrolFinder] wraps.
175176
final PatrolTester tester;
176177

178+
/// Wraps a function with a log entry for the start and end of the function.
179+
Future<T> wrapWithPatrolLog<T>({
180+
required String action,
181+
String? value,
182+
required String color,
183+
required Future<T> Function() function,
184+
bool enablePatrolLog = true,
185+
}) async {
186+
if (!(tester.config.printLogs && enablePatrolLog)) {
187+
return function();
188+
}
189+
190+
final finderText = finder
191+
.toString(describeSelf: true)
192+
.replaceAll('A finder that searches for', '')
193+
.replaceAll(' (considering only hit-testable ones)', '')
194+
.replaceAll(' (ignoring all but first)', '');
195+
196+
final valueText = value != null ? ' "$value"' : '';
197+
final text = '$color$action${AnsiCodes.reset}$valueText$finderText';
198+
tester.patrolLog
199+
.log(StepEntry(action: text, status: StepEntryStatus.start));
200+
try {
201+
final result = await function();
202+
tester.patrolLog
203+
.log(StepEntry(action: text, status: StepEntryStatus.success));
204+
return result;
205+
} catch (err) {
206+
tester.patrolLog.log(
207+
StepEntry(
208+
action: text,
209+
status: StepEntryStatus.failure,
210+
exception: err.toString(),
211+
),
212+
);
213+
rethrow;
214+
}
215+
}
216+
177217
/// Waits until this finder finds at least 1 visible widget and then taps on
178218
/// it.
179219
///
@@ -203,14 +243,18 @@ class PatrolFinder implements MatchFinder {
203243
SettlePolicy? settlePolicy,
204244
Duration? visibleTimeout,
205245
Duration? settleTimeout,
206-
}) async {
207-
await tester.tap(
208-
this,
209-
settlePolicy: settlePolicy,
210-
visibleTimeout: visibleTimeout,
211-
settleTimeout: settleTimeout,
212-
);
213-
}
246+
}) async =>
247+
wrapWithPatrolLog(
248+
action: 'tap',
249+
color: AnsiCodes.yellow,
250+
function: () => tester.tap(
251+
this,
252+
settlePolicy: settlePolicy,
253+
visibleTimeout: visibleTimeout,
254+
settleTimeout: settleTimeout,
255+
enablePatrolLog: false,
256+
),
257+
);
214258

215259
/// Waits until this finder finds at least 1 visible widget and then makes
216260
/// long press gesture on it.
@@ -241,14 +285,18 @@ class PatrolFinder implements MatchFinder {
241285
SettlePolicy? settlePolicy,
242286
Duration? visibleTimeout,
243287
Duration? settleTimeout,
244-
}) async {
245-
await tester.longPress(
246-
this,
247-
settlePolicy: settlePolicy,
248-
visibleTimeout: visibleTimeout,
249-
settleTimeout: settleTimeout,
250-
);
251-
}
288+
}) async =>
289+
wrapWithPatrolLog(
290+
action: 'longPress',
291+
color: AnsiCodes.yellow,
292+
function: () => tester.longPress(
293+
this,
294+
settlePolicy: settlePolicy,
295+
visibleTimeout: visibleTimeout,
296+
settleTimeout: settleTimeout,
297+
enablePatrolLog: false,
298+
),
299+
);
252300

253301
/// Waits until this finder finds at least 1 visible widget and then enters
254302
/// text into it.
@@ -280,15 +328,19 @@ class PatrolFinder implements MatchFinder {
280328
SettlePolicy? settlePolicy,
281329
Duration? visibleTimeout,
282330
Duration? settleTimeout,
283-
}) async {
284-
await tester.enterText(
285-
this,
286-
text,
287-
settlePolicy: settlePolicy,
288-
visibleTimeout: visibleTimeout,
289-
settleTimeout: settleTimeout,
290-
);
291-
}
331+
}) async =>
332+
wrapWithPatrolLog(
333+
action: 'enterText',
334+
color: AnsiCodes.magenta,
335+
function: () => tester.enterText(
336+
this,
337+
text,
338+
settlePolicy: settlePolicy,
339+
visibleTimeout: visibleTimeout,
340+
settleTimeout: settleTimeout,
341+
enablePatrolLog: false,
342+
),
343+
);
292344

293345
/// Shorthand for [PatrolTester.scrollUntilVisible].
294346
///
@@ -309,15 +361,22 @@ class PatrolFinder implements MatchFinder {
309361
Duration? dragDuration,
310362
SettlePolicy? settlePolicy,
311363
}) {
312-
return tester.scrollUntilVisible(
313-
finder: finder,
314-
view: view,
315-
delta: step,
316-
scrollDirection: scrollDirection,
317-
maxScrolls: maxScrolls,
318-
settleBetweenScrollsTimeout: settleBetweenScrollsTimeout,
319-
settlePolicy: settlePolicy,
320-
dragDuration: dragDuration,
364+
return wrapWithPatrolLog(
365+
action: 'scrollTo',
366+
color: AnsiCodes.green,
367+
function: () {
368+
return tester.scrollUntilVisible(
369+
finder: this,
370+
view: view,
371+
delta: step,
372+
scrollDirection: scrollDirection,
373+
maxScrolls: maxScrolls,
374+
settleBetweenScrollsTimeout: settleBetweenScrollsTimeout,
375+
settlePolicy: settlePolicy,
376+
dragDuration: dragDuration,
377+
enablePatrolLog: false,
378+
);
379+
},
321380
);
322381
}
323382

@@ -327,9 +386,12 @@ class PatrolFinder implements MatchFinder {
327386
///
328387
/// Timeout is globally set by [PatrolTester.config.visibleTimeout]. If you
329388
/// want to override this global setting, set [timeout].
330-
Future<PatrolFinder> waitUntilExists({Duration? timeout}) {
331-
return tester.waitUntilExists(this, timeout: timeout);
332-
}
389+
Future<PatrolFinder> waitUntilExists({Duration? timeout}) =>
390+
wrapWithPatrolLog(
391+
action: 'waitUntilExists',
392+
color: AnsiCodes.cyan,
393+
function: () => tester.waitUntilExists(this, timeout: timeout),
394+
);
333395

334396
/// Waits until this finder finds at least one visible widget.
335397
///
@@ -338,9 +400,12 @@ class PatrolFinder implements MatchFinder {
338400
///
339401
/// Timeout is globally set by [PatrolTester.config.visibleTimeout]. If you
340402
/// want to override this global setting, set [timeout].
341-
Future<PatrolFinder> waitUntilVisible({Duration? timeout}) {
342-
return tester.waitUntilVisible(this, timeout: timeout);
343-
}
403+
Future<PatrolFinder> waitUntilVisible({Duration? timeout}) =>
404+
wrapWithPatrolLog(
405+
action: 'waitUntilVisible',
406+
color: AnsiCodes.cyan,
407+
function: () => tester.waitUntilVisible(this, timeout: timeout),
408+
);
344409

345410
/// Returns a finder matching widget of type [T] which also fulfills
346411
/// [predicate].

packages/patrol_finders/lib/src/custom_finders/patrol_tester.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,13 @@ class PatrolTester {
129129
PatrolTester({
130130
required this.tester,
131131
required this.config,
132-
}) : _patrolLog = PatrolLogWriter();
132+
}) : patrolLog = PatrolLogWriter();
133133

134134
/// Global configuration of this tester.
135135
final PatrolTesterConfig config;
136136

137137
/// Logs a message to the patrol log.
138-
final PatrolLogWriter _patrolLog;
138+
final PatrolLogWriter patrolLog;
139139

140140
/// Flutter's widget tester that this [PatrolTester] wraps.
141141
final WidgetTester tester;
@@ -161,13 +161,13 @@ class PatrolTester {
161161
'';
162162
final valueText = value != null ? ' "$value"' : '';
163163
final text = '$color$action${AnsiCodes.reset}$valueText$finderText';
164-
_patrolLog.log(StepEntry(action: text, status: StepEntryStatus.start));
164+
patrolLog.log(StepEntry(action: text, status: StepEntryStatus.start));
165165
try {
166166
final result = await function();
167-
_patrolLog.log(StepEntry(action: text, status: StepEntryStatus.success));
167+
patrolLog.log(StepEntry(action: text, status: StepEntryStatus.success));
168168
return result;
169169
} catch (err) {
170-
_patrolLog.log(
170+
patrolLog.log(
171171
StepEntry(
172172
action: text,
173173
status: StepEntryStatus.failure,

packages/patrol_finders/pubspec.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: patrol_finders
22
description: Streamlined, high-level API on top of flutter_test.
3-
version: 2.3.0
3+
version: 2.4.0
44
homepage: https://patrol.leancode.co
55
repository: https://github.com/leancodepl/patrol/tree/master/packages/patrol_finders
66
issue_tracker: https://github.com/leancodepl/patrol/issues?q=is%3Aopen+is%3Aissue+label%3Apackage%3Apatrol_finders
@@ -20,7 +20,7 @@ dependencies:
2020
flutter_test:
2121
sdk: flutter
2222
meta: ^1.10.0
23-
patrol_log: ^0.0.1+2
23+
patrol_log: ^0.1.0
2424

2525
dev_dependencies:
2626
leancode_lint: ^14.2.0

0 commit comments

Comments
 (0)