forked from appium/java-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAndroidFunctionTest.java
146 lines (125 loc) · 5.33 KB
/
AndroidFunctionTest.java
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package io.appium.java_client.android;
import static java.time.Duration.ofMillis;
import static java.time.Duration.ofSeconds;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.StringContains.containsString;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import io.appium.java_client.functions.AppiumFunction;
import io.appium.java_client.functions.ExpectedCondition;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.TimeoutException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.FluentWait;
import org.openqa.selenium.support.ui.Wait;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class AndroidFunctionTest extends BaseAndroidTest {
private final AppiumFunction<WebDriver, List<WebElement>> searchingFunction = input -> {
List<WebElement> result = input.findElements(By.tagName("a"));
if (result.size() > 0) {
return result;
}
return null;
};
private final AppiumFunction<Pattern, WebDriver> contextFunction = input -> {
Set<String> contexts = driver.getContextHandles();
String current = driver.getContext();
contexts.forEach(context -> {
Matcher m = input.matcher(context);
if (m.find()) {
driver.context(context);
}
});
if (!current.equals(driver.getContext())) {
return driver;
}
return null;
};
private final AppiumFunction<List<WebElement>, List<WebElement>> filteringFunction = input -> {
final List<WebElement> result = new ArrayList<>();
input.forEach(element -> {
if (element.getText().equals("Hello World! - 1")) {
result.add(element);
}
});
if (result.size() > 0) {
return result;
}
return null;
};
@BeforeAll
public static void startWebViewActivity() {
if (driver != null) {
Activity activity = new Activity("io.appium.android.apis", ".view.WebView1");
driver.startActivity(activity);
}
}
@BeforeEach
public void setUp() {
driver.context("NATIVE_APP");
}
@Test
public void complexWaitingTestWithPreCondition() {
AppiumFunction<Pattern, List<WebElement>> compositeFunction =
searchingFunction.compose(contextFunction);
Wait<Pattern> wait = new FluentWait<>(Pattern.compile("WEBVIEW"))
.withTimeout(ofSeconds(30));
List<WebElement> elements = wait.until(compositeFunction);
assertThat("Element size should be 1", elements.size(), is(1));
assertThat("WebView is expected", driver.getContext(), containsString("WEBVIEW"));
}
@Test public void complexWaitingTestWithPostConditions() {
final List<Boolean> calls = new ArrayList<>();
AppiumFunction<Pattern, WebDriver> waitingForContext = input -> {
WebDriver result = contextFunction.apply(input);
if (result != null) {
calls.add(true);
}
return result;
};
AppiumFunction<Pattern, List<WebElement>> compositeFunction = waitingForContext
.andThen((ExpectedCondition<List<WebElement>>) input -> {
List<WebElement> result = searchingFunction.apply(input);
if (result != null) {
calls.add(true);
}
return result;
})
.andThen((AppiumFunction<List<WebElement>, List<WebElement>>) input -> {
List<WebElement> result = filteringFunction.apply(input);
if (result != null) {
calls.add(true);
}
return result;
});
Wait<Pattern> wait = new FluentWait<>(Pattern.compile("WEBVIEW"))
.withTimeout(ofSeconds(30));
List<WebElement> elements = wait.until(compositeFunction);
assertThat("Element size should be 1", elements.size(), is(1));
assertThat("WebView is expected", driver.getContext(), containsString("WEBVIEW"));
assertThat("There should be 3 calls", calls.size(), is(3));
}
@Test
public void nullPointerExceptionSafetyTestWithPrecondition() {
Wait<Pattern> wait = new FluentWait<>(Pattern.compile("Fake_context"))
.withTimeout(ofSeconds(30)).pollingEvery(ofMillis(500));
assertThrows(TimeoutException.class,
() -> assertTrue(wait.until(searchingFunction.compose(contextFunction)).size() > 0));
}
@Test
public void nullPointerExceptionSafetyTestWithPostConditions() {
Wait<Pattern> wait = new FluentWait<>(Pattern.compile("Fake_context"))
.withTimeout(ofSeconds(30)).pollingEvery(ofMillis(500));
assertThrows(TimeoutException.class,
() -> assertTrue(wait.until(contextFunction.andThen(searchingFunction).andThen(filteringFunction)).size() > 0));
}
}