forked from appium/java-client
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestUtils.java
More file actions
Latest commit
81 lines (73 loc) · 2.86 KB
/
Copy pathTestUtils.java
File metadata and controls
81 lines (73 loc) · 2.86 KB
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
packageio.appium.java_client;
importorg.openqa.selenium.Dimension;
importorg.openqa.selenium.Point;
importorg.openqa.selenium.TimeoutException;
importorg.openqa.selenium.WebElement;
importjavax.annotation.Nullable;
importjava.net.DatagramSocket;
importjava.net.InetAddress;
importjava.net.SocketException;
importjava.net.URISyntaxException;
importjava.net.URL;
importjava.net.UnknownHostException;
importjava.nio.file.Path;
importjava.nio.file.Paths;
importjava.time.Duration;
importjava.util.function.Supplier;
publicclassTestUtils {
privateTestUtils() {
}
publicstaticStringgetLocalIp4Address() throwsSocketException, UnknownHostException {
// https://stackoverflow.com/questions/9481865/getting-the-ip-address-of-the-current-machine-using-java
try (finalDatagramSocketsocket = newDatagramSocket()) {
socket.connect(InetAddress.getByName("8.8.8.8"), 10002);
returnsocket.getLocalAddress().getHostAddress();
}
}
publicstaticPathresourcePathToAbsolutePath(StringresourcePath) {
URLurl = ClassLoader.getSystemResource(resourcePath);
if (url == null) {
thrownewIllegalArgumentException(String.format("Cannot find the '%s' resource", resourcePath));
}
try {
returnPaths.get(url.toURI()).toAbsolutePath();
} catch (URISyntaxExceptione) {
thrownewIllegalArgumentException(e);
}
}
publicstaticvoidwaitUntilTrue(Supplier<Boolean> func, Durationtimeout, Durationinterval) {
longstarted = System.currentTimeMillis();
RuntimeExceptionlastError = null;
while (System.currentTimeMillis() - started < timeout.toMillis()) {
lastError = null;
try {
Booleanresult = func.get();
if (result != null && result) {
return;
}
//noinspection BusyWait
Thread.sleep(interval.toMillis());
} catch (RuntimeException | InterruptedExceptione) {
if (einstanceofInterruptedException) {
thrownewRuntimeException(e);
} else {
lastError = (RuntimeException) e;
}
}
}
if (lastError != null) {
throwlastError;
}
thrownewTimeoutException(String.format("Condition unmet after %sms timeout", timeout.toMillis()));
}
publicstaticPointgetCenter(WebElementwebElement) {
returngetCenter(webElement, null);
}
publicstaticPointgetCenter(WebElementwebElement, @NullablePointlocation) {
Dimensiondim = webElement.getSize();
if (location == null) {
location = webElement.getLocation();
}
returnnewPoint(location.x + dim.width / 2, location.y + dim.height / 2);
}
}