forked from karatelabs/karate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Driver.java
276 lines (193 loc) · 7.18 KB
/
Driver.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
* The MIT License
*
* Copyright 2018 Intuit Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.intuit.karate.driver;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;
/**
*
* @author pthomas3
*/
public interface Driver {
void activate();
void refresh();
void reload();
void back();
void forward();
void maximize();
void minimize();
void fullscreen();
void close();
void quit();
void switchPage(String titleOrUrl);
void switchFrame(int index);
void switchFrame(String locator);
String getUrl(); // getter
void setUrl(String url); // setter
Map<String, Object> getDimensions(); // getter
void setDimensions(Map<String, Object> map); // setter
String getTitle(); // getter
List<String> getPages(); // getter
String getDialog(); // getter
byte[] screenshot(boolean embed);
default byte[] screenshot() {
return screenshot(true);
}
Map<String, Object> cookie(String name);
void deleteCookie(String name);
void clearCookies();
List<Map> getCookies(); // getter
void setCookie(Map<String, Object> cookie); // setter
void dialog(boolean accept);
void dialog(boolean accept, String input);
Object script(String expression);
boolean waitUntil(String expression);
Driver submit();
default Driver retry() {
return retry(null, null);
}
default Driver retry(int count) {
return retry(count, null);
}
default Driver retry(Integer count, Integer interval) {
getOptions().enableRetry(count, interval);
return this;
}
default Driver delay(int millis) {
getOptions().sleep(millis);
return this;
}
// element actions =========================================================
//
Element focus(String locator);
Element clear(String locator);
Element click(String locator);
Element input(String locator, String value);
default Element input(String locator, String[] values) {
Element element = DriverElement.locatorUnknown(this, locator);
for (String value : values) {
element = input(locator, value);
}
return element;
}
Element select(String locator, String text);
Element select(String locator, int index);
Element value(String locator, String value);
default Element waitFor(String locator) {
return getOptions().waitForAny(this, locator);
}
default String waitForUrl(String expected) {
return getOptions().waitForUrl(this, expected);
}
default Element waitForText(String locator, String expected) {
return waitUntil(locator, "_.textContent.includes('" + expected + "')");
}
default Element waitForEnabled(String locator) {
return waitUntil(locator, "!_.disabled");
}
default Element waitForAny(String locator1, String locator2) {
return getOptions().waitForAny(this, new String[]{locator1, locator2});
}
default Element waitForAny(String[] locators) {
return getOptions().waitForAny(this, locators);
}
default Element waitUntil(String locator, String expression) {
return getOptions().waitUntil(this, locator, expression);
}
default Object waitUntil(Supplier<Object> condition) {
return getOptions().retry(() -> condition.get(), o -> o != null, "waitUntil (function)");
}
default List<Element> findAll(String locator) {
return getOptions().findAll(this, locator);
}
default Element scroll(String locator) {
script(locator, DriverOptions.SCROLL_JS_FUNCTION);
return DriverElement.locatorExists(this, locator);
}
default Element highlight(String locator) {
script(getOptions().highlighter(locator));
return DriverElement.locatorExists(this, locator);
}
// friendly locators =======================================================
//
default Finder rightOf(String locator) {
return new ElementFinder(this, locator, ElementFinder.Type.RIGHT);
}
default Finder leftOf(String locator) {
return new ElementFinder(this, locator, ElementFinder.Type.LEFT);
}
default Finder above(String locator) {
return new ElementFinder(this, locator, ElementFinder.Type.ABOVE);
}
default Finder below(String locator) {
return new ElementFinder(this, locator, ElementFinder.Type.BELOW);
}
default Finder near(String locator) {
return new ElementFinder(this, locator, ElementFinder.Type.NEAR);
}
// mouse and keys ==========================================================
//
default Mouse mouse() {
return new DriverMouse(this);
}
default Mouse mouse(String locator) {
return new DriverMouse(this).move(locator);
}
default Mouse mouse(int x, int y) {
return new DriverMouse(this).move(x, y);
}
default Keys keys() {
return new Keys(this);
}
void actions(List<Map<String, Object>> actions);
// element state ===========================================================
//
String html(String locator);
String text(String locator);
String value(String locator);
String attribute(String locator, String name);
String property(String locator, String name);
boolean enabled(String locator);
default Element exists(String locator) {
return getOptions().exists(this, locator);
}
Map<String, Object> position(String locator);
byte[] screenshot(String locator, boolean embed);
default byte[] screenshot(String locator) {
return screenshot(locator, true);
}
default Object script(String locator, String expression) {
String js = getOptions().selectorScript(locator, expression);
return script(js);
}
default List scripts(String locator, String expression) {
String js = getOptions().selectorAllScript(locator, expression);
return (List) script(js);
}
// for internal use ========================================================
//
DriverOptions getOptions();
Object elementId(String locator);
List elementIds(String locator);
}