forked from jonsamwell/flutter_gherkin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
driver_utils.dart
109 lines (99 loc) · 3.02 KB
/
driver_utils.dart
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import 'dart:async';
import 'package:flutter_driver/flutter_driver.dart';
class FlutterDriverUtils {
static Future<bool> isPresent(
FlutterDriver? driver,
SerializableFinder finder, {
Duration timeout = const Duration(seconds: 1),
}) async {
try {
await driver?.waitFor(finder, timeout: timeout);
return true;
} catch (_) {
return false;
}
}
static Future<bool> isAbsent(
FlutterDriver? driver,
SerializableFinder finder, {
Duration timeout = const Duration(seconds: 30),
}) async {
try {
await driver?.waitForAbsent(finder, timeout: timeout);
return true;
} catch (_) {
return false;
}
}
static Future<bool> waitForFlutter(
FlutterDriver? driver, {
Duration timeout = const Duration(seconds: 30),
}) async {
try {
await driver?.waitUntilNoTransientCallbacks(timeout: timeout);
return true;
} catch (_) {
return false;
}
}
static Future<void> enterText(
FlutterDriver? driver,
SerializableFinder finder,
String text, {
Duration timeout = const Duration(seconds: 30),
}) async {
await FlutterDriverUtils.tap(driver, finder, timeout: timeout);
await driver?.enterText(text, timeout: timeout);
}
static Future<String> getText(
FlutterDriver driver,
SerializableFinder finder, {
Duration timeout = const Duration(seconds: 30),
}) async {
await FlutterDriverUtils.waitForFlutter(driver, timeout: timeout);
return await driver.getText(finder, timeout: timeout);
}
static Future<void> tap(
FlutterDriver? driver,
SerializableFinder finder, {
Duration? timeout,
}) async {
timeout ??= const Duration(seconds: 30);
await driver?.tap(finder, timeout: timeout);
await FlutterDriverUtils.waitForFlutter(driver, timeout: timeout);
}
static Future<void> longPress(
FlutterDriver driver,
SerializableFinder finder, {
Duration pressDuration = const Duration(milliseconds: 500),
Duration timeout = const Duration(seconds: 30),
}) async {
await driver.scroll(finder, 0, 0, pressDuration, timeout: timeout);
await FlutterDriverUtils.waitForFlutter(driver, timeout: timeout);
}
/// Waits until the [condition] returns true
/// Will raise a complete with a [TimeoutException] if the
/// condition does not return true with the timeout period.
static Future<void> waitUntil(
FlutterDriver? driver,
Future<bool> Function() condition, {
Duration timeout = const Duration(seconds: 10),
Duration pollInterval = const Duration(milliseconds: 500),
}) async {
return Future.microtask(() async {
final completer = Completer<void>();
var maxAttempts =
(timeout.inMilliseconds / pollInterval.inMilliseconds).round();
var attempts = 0;
while (attempts < maxAttempts) {
final result = await condition();
if (result) {
completer.complete();
break;
} else {
await Future.delayed(pollInterval);
}
}
}).timeout(timeout);
}
}