@@ -4,6 +4,7 @@ import 'package:flutter_test/flutter_test.dart';
4
4
import 'package:meta/meta.dart' ;
5
5
import 'package:patrol_finders/patrol_finders.dart' ;
6
6
import 'package:patrol_finders/src/custom_finders/utils.dart' ;
7
+ import 'package:patrol_log/patrol_log.dart' ;
7
8
8
9
/// Thrown when some [PatrolFinder] 's method fails.
9
10
class PatrolFinderException implements Exception {
@@ -174,6 +175,45 @@ class PatrolFinder implements MatchFinder {
174
175
/// [PatrolTester] that this [PatrolFinder] wraps.
175
176
final PatrolTester tester;
176
177
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
+
177
217
/// Waits until this finder finds at least 1 visible widget and then taps on
178
218
/// it.
179
219
///
@@ -203,14 +243,18 @@ class PatrolFinder implements MatchFinder {
203
243
SettlePolicy ? settlePolicy,
204
244
Duration ? visibleTimeout,
205
245
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
+ );
214
258
215
259
/// Waits until this finder finds at least 1 visible widget and then makes
216
260
/// long press gesture on it.
@@ -241,14 +285,18 @@ class PatrolFinder implements MatchFinder {
241
285
SettlePolicy ? settlePolicy,
242
286
Duration ? visibleTimeout,
243
287
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
+ );
252
300
253
301
/// Waits until this finder finds at least 1 visible widget and then enters
254
302
/// text into it.
@@ -280,15 +328,19 @@ class PatrolFinder implements MatchFinder {
280
328
SettlePolicy ? settlePolicy,
281
329
Duration ? visibleTimeout,
282
330
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
+ );
292
344
293
345
/// Shorthand for [PatrolTester.scrollUntilVisible] .
294
346
///
@@ -309,15 +361,22 @@ class PatrolFinder implements MatchFinder {
309
361
Duration ? dragDuration,
310
362
SettlePolicy ? settlePolicy,
311
363
}) {
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
+ },
321
380
);
322
381
}
323
382
@@ -327,9 +386,12 @@ class PatrolFinder implements MatchFinder {
327
386
///
328
387
/// Timeout is globally set by [PatrolTester.config.visibleTimeout] . If you
329
388
/// 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
+ );
333
395
334
396
/// Waits until this finder finds at least one visible widget.
335
397
///
@@ -338,9 +400,12 @@ class PatrolFinder implements MatchFinder {
338
400
///
339
401
/// Timeout is globally set by [PatrolTester.config.visibleTimeout] . If you
340
402
/// 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
+ );
344
409
345
410
/// Returns a finder matching widget of type [T] which also fulfills
346
411
/// [predicate] .
0 commit comments