1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package us.johnmeyer.sitetest.credentials;
22
23 import us.johnmeyer.utilities.UrlWrapper;
24 import org.openqa.selenium.WebDriver;
25
26 import org.openqa.selenium.By;
27 import org.openqa.selenium.WebElement;
28 import org.openqa.selenium.remote.DesiredCapabilities;
29 import org.openqa.selenium.remote.RemoteWebDriver;
30 import java.util.Formatter;
31 import java.util.List;
32 import static java.time.LocalDateTime.now;
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 public final class WebDriverWrapper {
63
64
65
66
67
68 public enum Browser {
69
70
71 FIREFOX ("Firefox"),
72
73
74 SAFARI ("Safari"),
75
76
77 INTERNET_EXPLORER ("IE"),
78
79
80 CHROME ("Chrome"),
81
82
83 OPERA ("Opera"),
84
85
86 EDGE ("Edge");
87
88
89 private String key;
90
91
92
93
94 Browser(final String capabilityKeyParam) {
95 key = capabilityKeyParam;
96 }
97
98
99
100
101
102 public String getKey() {
103 return (key);
104 }
105
106
107 }
108
109
110
111
112
113
114 private static final String REINSTANTIATION_EXCEPTION_CODE = "E001";
115
116
117 private static final String BROWSER_CAPABILITY_KEY = "browser";
118
119
120 private static final String BROWSERSTACK_DEBUG_CAPABILITY_KEY = "browserstack.debug";
121
122
123 private static final String BROWSERSTACK_BUILD_NAME_CAPABILITY_KEY = "build";
124
125
126 private static final Browser BROWSER_SELECTION = Browser.CHROME;
127
128
129 private static final String BROWSER_DEBUG_FLAG_DESIRED_CAPABILITY_KEY_VALUE = "true";
130
131
132 private static final String BROWSERSTACK_BUILD_NAME_DESIRED_CAPABILITY_KEY_VALUE = "defaultBuildName";
133
134
135
136
137
138
139
140
141 private static WebDriverWrapper activeWrapper;
142
143
144
145
146 private String buildName = null;
147
148
149 private WebDriver webDriver = null;
150
151
152 private String systemUrl = null;
153
154
155 private String elementTimeout = "Browserstack default";
156
157
158
159
160
161
162
163
164 public static boolean isInitialized() {
165 return (getPreviousWrapper() != null);
166 }
167
168
169
170
171
172
173 public static String getBrowserCapabilityKey() {
174 return BROWSER_CAPABILITY_KEY;
175 }
176
177
178
179
180
181 public static String getBrowserstackDebugCapabilityKey() {
182 return BROWSERSTACK_DEBUG_CAPABILITY_KEY;
183 }
184
185
186
187
188
189 public static String getBrowserstackBuildNameCapabilityKey() {
190 return BROWSERSTACK_BUILD_NAME_CAPABILITY_KEY;
191 }
192
193
194
195
196
197 public static Browser getBrowserSelection() {
198 return BROWSER_SELECTION;
199 }
200
201
202
203
204
205 public static String getBrowserDebugFlagDesiredCapabilityKeyValue() {
206 return BROWSER_DEBUG_FLAG_DESIRED_CAPABILITY_KEY_VALUE;
207 }
208
209
210
211
212
213 public static String getBrowserstackBuildNameDesiredCapabilityKeyValue() {
214 return BROWSERSTACK_BUILD_NAME_DESIRED_CAPABILITY_KEY_VALUE;
215
216 }
217
218
219
220
221
222
223
224
225
226 public static WebDriverWrapper newWebDriverWrapper(final String buildNameParam) {
227
228
229 WebDriverWrapper wrapper = WebDriverWrapper.getPreviousWrapper();
230 WebDriver driver = null;
231
232
233 log("newWebDriverWrapper1(" + buildNameParam + ")");
234
235
236 if (wrapper != null) {
237
238
239 String oldBuildName = wrapper.getBuildName();
240
241 if (oldBuildName.equals(buildNameParam)) {
242 log("Recycling wrapper.");
243 return wrapper;
244 } else {
245 log("Insantiating new wrapper because build name "
246 + oldBuildName + " != " + buildNameParam);
247 }
248 } else {
249 log("No previous wrapper found.");
250 }
251 return newWebDriverWrapper(buildNameParam, driver);
252
253 }
254
255
256
257
258
259
260
261
262
263
264
265
266 public static WebDriverWrapper newWebDriverWrapper(final String buildNameParam,
267 final WebDriver webDriverParam) {
268
269 WebDriverWrapper webDriverWrapperLocal;
270 boolean doReinitialize = true;
271
272 log("newWebDriverWrapper2(" + buildNameParam + ", [webDriverParam])");
273
274 if (buildNameParam == null) {
275
276 throw new NullPointerException("Submit build name.");
277 }
278
279
280 webDriverWrapperLocal = WebDriverWrapper.getPreviousWrapper();
281
282 if (webDriverWrapperLocal != null
283 && webDriverParam != null) {
284
285 String prevBuildName = webDriverWrapperLocal.getBuildName();
286
287 if (prevBuildName == null) {
288
289 doReinitialize = false;
290
291 } else if (isInitialized() && !(prevBuildName.equals(buildNameParam))) {
292
293 System.out.println("Recycling WebDriverWrapper.");
294 webDriverWrapperLocal = WebDriverWrapper.getPreviousWrapper();
295 doReinitialize = false;
296
297 }
298
299
300 }
301
302 if (doReinitialize) {
303
304 log("Initializing new WebDriver.");
305 WebDriver webDriverLocal = newWebDriver(buildNameParam);
306 log("Initializing new wrapper.");
307 webDriverWrapperLocal = new WebDriverWrapper(webDriverLocal,
308 buildNameParam);
309
310 }
311
312 return webDriverWrapperLocal;
313
314 }
315
316
317
318
319
320
321 public static WebDriverWrapper newWebDriverWrapper() {
322
323 String keyValue
324 = getBrowserstackBuildNameDesiredCapabilityKeyValue();
325
326 log("newWebDriverWrapper()");
327
328 if (keyValue == null) {
329 throw new NullPointerException("Received null key value from"
330 + " getBrowserstackBuildNameDesiredCapabilityKeyValue");
331 }
332
333 WebDriverWrapper webDriverWrapperLocal
334 = newWebDriverWrapper(keyValue);
335
336
337 return webDriverWrapperLocal;
338 }
339
340
341
342
343
344
345
346
347
348
349
350
351
352 public static DesiredCapabilities
353 getDesiredCapabilities(final String browserKeyParam,
354 final String debugKeyParam,
355 final String buildNameKeyParam,
356 final Browser browserValueParam,
357 final String debugValueParam,
358 final String buildNameValueParam) {
359
360 DesiredCapabilities desiredCapabilities
361 = new DesiredCapabilities();
362
363 desiredCapabilities.setCapability(browserKeyParam,
364 browserValueParam.getKey());
365
366 desiredCapabilities
367 .setCapability(debugKeyParam,
368 debugValueParam);
369
370 desiredCapabilities
371 .setCapability(buildNameKeyParam,
372 buildNameValueParam);
373
374 return desiredCapabilities;
375
376 }
377
378
379
380
381
382
383 public static void log(final String logEntryParam) {
384
385 String entry;
386
387 entry = now().toString() + " DriverWrapper "
388 + ": " + logEntryParam;
389
390 System.out.println(entry);
391
392 }
393
394
395
396
397
398
399
400
401 private static WebDriverWrapper getPreviousWrapper() {
402 return activeWrapper;
403 }
404
405
406
407
408
409
410 public static void setActiveWrapper(final WebDriverWrapper wrapperParam) {
411
412 activeWrapper = wrapperParam;
413 }
414
415
416
417
418
419
420
421 private static WebDriver newWebDriver(final String buildNameParam) {
422
423
424
425 WebDriver webDriverLocal;
426
427
428 log("newWebDriver(" + buildNameParam + ")");
429
430
431
432 try {
433
434 BrowserstackCredential.newBrowserstackCredential();
435
436 } catch (BrowserstackCredential.UsingRecycledCredentialInstance r) {
437
438 System.out.println("Recycling credential: "
439 + r.toString());
440
441 } catch (RuntimeException r) {
442
443 throw r;
444
445 } catch (BrowserstackAutomateKeyException e) {
446
447 throw new RuntimeException(e.getMessage());
448 }
449
450 DesiredCapabilities desiredCapabilities
451 = getDesiredCapabilities(getBrowserCapabilityKey(),
452 getBrowserstackDebugCapabilityKey(),
453 getBrowserstackBuildNameCapabilityKey(),
454 getBrowserSelection(),
455 getBrowserDebugFlagDesiredCapabilityKeyValue(),
456 buildNameParam);
457
458 UrlWrapper urlWrapper = BrowserstackCredential.getBrowserstackAutomateUrl();
459 webDriverLocal
460 = new RemoteWebDriver(urlWrapper.getUrl(),
461 desiredCapabilities);
462
463 return webDriverLocal;
464
465 }
466
467
468
469
470
471
472
473
474
475
476
477 private WebDriverWrapper(final WebDriver webDriverParam,
478 final String buildNameParam) {
479
480 webDriver = webDriverParam;
481 WebDriverWrapper.setActiveWrapper(this);
482 this.buildName = buildNameParam;
483 log("Wrapping ["
484 + webDriverParam.toString()
485 + "] for build "
486 + buildNameParam);
487
488 }
489
490
491
492
493
494
495
496
497
498
499
500 private boolean isBuildName(final String buildNameParam) {
501
502 if (buildNameParam == null) {
503 throw new NullPointerException("Pass not a null build name.");
504 }
505
506 if (buildName == null) {
507 return false;
508 }
509
510 return (buildName.equals(buildNameParam));
511
512 }
513
514
515
516
517
518 public String getBuildName() {
519
520 if (buildName == null) {
521 throw new NullPointerException("Found null instead of internal buildName value.");
522 }
523
524 return buildName;
525
526 }
527
528
529
530
531
532
533
534
535
536
537 public void waitSeconds(final long secondsParam) {
538
539 String secondsParamString
540 = new Long(secondsParam).toString();
541
542 String oldTimeout = elementTimeout;
543
544 java.util.concurrent.TimeUnit unit
545 = java.util.concurrent.TimeUnit.SECONDS;
546
547 WebDriver webDriverLocal = getWebDriver();
548 webDriverLocal.manage().timeouts().implicitlyWait(secondsParam, unit);
549
550 elementTimeout = secondsParamString;
551
552 if (!oldTimeout.equals(elementTimeout)) {
553
554 log("Element timeout changed from " + oldTimeout
555 + " to " + secondsParamString
556 + " seconds.");
557 }
558
559 }
560
561
562
563
564
565 public String getPageSource() {
566 String pageSource = getWebDriver().getPageSource();
567 return pageSource;
568 }
569
570
571
572
573
574 public String getBodyText() {
575 WebElement webElement = findElement(By.tagName("body"));
576 return webElement.getText();
577 }
578
579
580
581
582
583
584 public boolean isInactive() {
585
586 WebDriverWrapper previousWrapper;
587
588
589
590 if (!WebDriverWrapper.isInitialized()) {
591 return true;
592 }
593
594
595
596
597 previousWrapper = WebDriverWrapper.getPreviousWrapper();
598
599 if (previousWrapper != null) {
600
601 if (previousWrapper != this) {
602
603 return true;
604 }
605
606 }
607
608 return false;
609
610 }
611
612
613
614
615
616
617
618
619 public String toString() {
620
621 String objectId, isPrevious;
622 WebDriver webDriverLocal = getWebDriver();
623
624 if (getPreviousWrapper() != null) {
625 isPrevious = "true";
626 } else {
627 isPrevious = "false";
628 }
629
630 if (webDriverLocal != null) {
631 objectId = webDriverLocal.toString();
632 } else {
633 objectId = null;
634 }
635
636 String format = "# WebDriverWrapper #\n---\n"
637 + " WebDriver Object: %s\nBuild Name: %s\n"
638 + " Element timeout: %s\n"
639 + " Obfuscated Test System URL: %s\n"
640 + " Previous Wrapper = %s";
641
642 Formatter formatter = new Formatter();
643
644 formatter.format(format, objectId, buildName, elementTimeout,
645 systemUrl, isPrevious);
646
647 return formatter.toString();
648 }
649
650
651
652
653
654
655 public void get(final String targetUrlString) {
656 getWebDriver().get(targetUrlString);
657
658 }
659
660
661
662
663
664
665
666
667 public WebElement findElement(final By byParam) {
668
669 return getWebDriver().findElement(byParam);
670
671 }
672
673
674
675
676
677
678
679
680
681
682 public List<WebElement> findElements(final By byParam) {
683
684 return getWebDriver().findElements(byParam);
685
686 }
687
688
689
690
691 public void quit() {
692 getWebDriver().quit();
693 setActiveWrapper(null);
694 log("quit()");
695
696 }
697
698
699
700
701
702
703
704 public String getWebDriverToString() {
705
706 if (!isInitialized()) {
707 return null;
708 } else {
709 return webDriver.toString();
710 }
711 }
712
713
714
715
716
717
718
719
720
721
722
723 private WebDriver getWebDriver() {
724
725 if (!isInitialized()) {
726 throw new IllegalStateException("Wrapper not initialized or"
727 + " previous driver not quit. Check code as init flag is set in constructor.");
728 }
729
730 return webDriver;
731
732 }
733
734 }
735
736