/
Driver.java
497 lines (389 loc) · 12.5 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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
/*
* The MIT License
*
* Copyright 2022 Karate Labs 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 com.intuit.karate.core.AutoDef;
import com.intuit.karate.core.Plugin;
import com.intuit.karate.core.Config;
import com.intuit.karate.core.FeatureRuntime;
import com.intuit.karate.core.ScenarioEngine;
import com.intuit.karate.core.ScenarioRuntime;
import com.intuit.karate.core.StepResult;
import com.intuit.karate.http.HttpClientFactory;
import com.intuit.karate.http.ResourceType;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;
import java.util.function.Supplier;
/**
*
* @author pthomas3
*/
public interface Driver extends Plugin {
public static Driver start(String browserType) {
return start(Collections.singletonMap("type", browserType));
}
public static Driver start(Map<String, Object> options) {
ScenarioRuntime runtime = FeatureRuntime.forTempUse(HttpClientFactory.DEFAULT).scenarios.next();
ScenarioEngine.set(runtime.engine);
return DriverOptions.start(options, runtime);
}
@AutoDef
void activate();
@AutoDef
void refresh();
@AutoDef
void reload();
@AutoDef
void back();
@AutoDef
void forward();
@AutoDef
void maximize();
@AutoDef
void minimize();
@AutoDef
void fullscreen();
@AutoDef
void close();
@AutoDef
void quit();
@AutoDef
void switchPage(String titleOrUrl);
@AutoDef
void switchPage(int index);
@AutoDef
void switchFrame(int index);
@AutoDef
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 getDialogText(); // getter
@AutoDef
byte[] screenshot(boolean embed);
@AutoDef
default byte[] screenshot() {
return screenshot(true);
}
@AutoDef
Map<String, Object> cookie(String name);
@AutoDef
default void setCookies(List<Map<String, Object>> cookies) {
System.out.println("got this cookie: " + cookies);
cookies.forEach(c -> cookie(c));
}
@AutoDef
void cookie(Map<String, Object> cookie);
@AutoDef
void deleteCookie(String name);
@AutoDef
void clearCookies();
List<Map> getCookies(); // getter
@AutoDef
void dialog(boolean accept);
@AutoDef
void dialog(boolean accept, String input);
@AutoDef
Object script(String expression);
@AutoDef
boolean waitUntil(String expression);
@AutoDef
Driver submit();
@AutoDef
default Driver retry() {
return retry(null, null);
}
@AutoDef
default Driver retry(int count) {
return retry(count, null);
}
@AutoDef
default Driver retry(Integer count, Integer interval) {
getOptions().enableRetry(count, interval);
return this;
}
@AutoDef
default Driver delay(int millis) {
getOptions().sleep(millis);
return this;
}
@AutoDef
Driver timeout(Integer millis);
@AutoDef
Driver timeout();
// element actions =========================================================
//
@AutoDef
Element focus(String locator);
@AutoDef
Element clear(String locator);
@AutoDef
Element click(String locator);
@AutoDef
Element input(String locator, String value);
@AutoDef
default Element input(String locator, String[] values) {
return input(locator, values, 0);
}
@AutoDef
default Element input(String locator, String chars, int delay) {
String[] array = new String[chars.length()];
for (int i = 0; i < array.length; i++) {
array[i] = Character.toString(chars.charAt(i));
}
return input(locator, array, delay);
}
@AutoDef
default Element input(String locator, String[] values, int delay) {
Element element = DriverElement.locatorUnknown(this, locator);
for (String value : values) {
if (delay > 0) {
delay(delay);
}
element = input(locator, value);
}
return element;
}
@AutoDef
Element select(String locator, String text);
@AutoDef
Element select(String locator, int index);
@AutoDef
Element value(String locator, String value);
@AutoDef
default Element waitFor(String locator) {
return getOptions().waitForAny(this, locator);
}
@AutoDef
default String waitForUrl(String expected) {
return getOptions().waitForUrl(this, expected);
}
@AutoDef
default Element waitForText(String locator, String expected) {
return waitUntil(locator, "_.textContent.includes('" + expected + "')");
}
@AutoDef
default Element waitForEnabled(String locator) {
return waitUntil(locator, "!_.disabled");
}
@AutoDef
default List<Element> waitForResultCount(String locator, int count) {
return (List) waitUntil(() -> {
List<Element> list = locateAll(locator);
return list.size() == count ? list : null;
});
}
@AutoDef
default List waitForResultCount(String locator, int count, String expression) {
return (List) waitUntil(() -> {
List list = scriptAll(locator, expression);
return list.size() == count ? list : null;
});
}
@AutoDef
default Element waitForAny(String locator1, String locator2) {
return getOptions().waitForAny(this, new String[]{locator1, locator2});
}
@AutoDef
default Element waitForAny(String[] locators) {
return getOptions().waitForAny(this, locators);
}
@AutoDef
default Element waitUntil(String locator, String expression) {
return getOptions().waitUntil(this, locator, expression);
}
@AutoDef
default Object waitUntil(Supplier<Object> condition) {
return getOptions().retry(() -> condition.get(), o -> o != null, "waitUntil (function)", true);
}
@AutoDef
default Element locate(String locator) {
Element e = DriverElement.locatorUnknown(this, locator);
if (e.isPresent()) {
return e;
}
throw new RuntimeException("cannot find locator: " + locator);
}
@AutoDef
default List<Element> locateAll(String locator) {
return getOptions().findAll(this, locator);
}
@AutoDef
default List<Element> locateAll(String locator, Predicate predicate) {
List before = locateAll(locator);
List after = new ArrayList(before.size());
for (Object o : before) {
if (predicate.test(o)) {
after.add(o);
}
}
return after;
}
@AutoDef
default Element scroll(String locator) {
script(locator, DriverOptions.SCROLL_JS_FUNCTION);
return DriverElement.locatorExists(this, locator);
}
@AutoDef
default Element highlight(String locator) {
return highlight(locator, Config.DEFAULT_HIGHLIGHT_DURATION);
}
default Element highlight(String locator, int millis) {
script(getOptions().highlight(locator, millis));
delay(millis);
return DriverElement.locatorExists(this, locator);
}
@AutoDef
default void highlightAll(String locator) {
highlightAll(locator, Config.DEFAULT_HIGHLIGHT_DURATION);
}
default void highlightAll(String locator, int millis) {
script(getOptions().highlightAll(locator, millis));
delay(millis);
}
// friendly locators =======================================================
//
@AutoDef
default Finder rightOf(String locator) {
return new ElementFinder(this, locator, ElementFinder.Type.RIGHT);
}
@AutoDef
default Finder leftOf(String locator) {
return new ElementFinder(this, locator, ElementFinder.Type.LEFT);
}
@AutoDef
default Finder above(String locator) {
return new ElementFinder(this, locator, ElementFinder.Type.ABOVE);
}
@AutoDef
default Finder below(String locator) {
return new ElementFinder(this, locator, ElementFinder.Type.BELOW);
}
@AutoDef
default Finder near(String locator) {
return new ElementFinder(this, locator, ElementFinder.Type.NEAR);
}
// mouse and keys ==========================================================
//
@AutoDef
default Mouse mouse() {
return new DriverMouse(this);
}
@AutoDef
default Mouse mouse(String locator) {
return new DriverMouse(this).move(locator);
}
@AutoDef
default Mouse mouse(Number x, Number y) {
return new DriverMouse(this).move(x, y);
}
@AutoDef
default Keys keys() {
return new Keys(this);
}
@AutoDef
void actions(List<Map<String, Object>> actions);
// element state ===========================================================
//
@AutoDef
String html(String locator);
@AutoDef
String text(String locator);
@AutoDef
String value(String locator);
@AutoDef
String attribute(String locator, String name);
@AutoDef
String property(String locator, String name);
@AutoDef
boolean enabled(String locator);
@AutoDef
default boolean exists(String locator) {
return getOptions().optional(this, locator).isPresent();
}
@AutoDef
default Element optional(String locator) {
return getOptions().optional(this, locator);
}
@AutoDef
Map<String, Object> position(String locator);
@AutoDef
Map<String, Object> position(String locator, boolean relative);
@AutoDef
byte[] screenshot(String locator, boolean embed);
@AutoDef
default byte[] screenshot(String locator) {
return screenshot(locator, true);
}
@AutoDef
default Object script(String locator, String expression) {
String js = getOptions().scriptSelector(locator, expression);
return script(js);
}
@AutoDef
default List scriptAll(String locator, String expression) {
String js = getOptions().scriptAllSelector(locator, expression);
return (List) script(js);
}
@AutoDef
default List scriptAll(String locator, String expression, Predicate predicate) {
List before = scriptAll(locator, expression);
List after = new ArrayList(before.size());
for (Object o : before) {
if (predicate.test(o)) {
after.add(o);
}
}
return after;
}
@AutoDef
public byte[] pdf(Map<String, Object> options);
// for internal use ========================================================
//
boolean isTerminated();
DriverOptions getOptions();
Object elementId(String locator);
List elementIds(String locator);
static final List<String> METHOD_NAMES = Plugin.methodNames(Driver.class);
@Override
default List<String> methodNames() {
return METHOD_NAMES;
}
@Override
default Map<String, Object> afterScenario() {
return Collections.EMPTY_MAP; // TODO
}
@Override
public default void onFailure(StepResult stepResult) {
if (getOptions().screenshotOnFailure && !stepResult.isWithCallResults()) {
byte[] bytes = screenshot(false);
getRuntime().embed(bytes, ResourceType.PNG);
}
}
}