1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.testtools.selenium;
17
18 import com.thoughtworks.selenium.SeleneseTestBase;
19 import org.apache.commons.lang.StringUtils;
20 import org.apache.commons.lang3.exception.ExceptionUtils;
21 import org.openqa.selenium.Alert;
22 import org.openqa.selenium.By;
23 import org.openqa.selenium.Dimension;
24 import org.openqa.selenium.JavascriptExecutor;
25 import org.openqa.selenium.NoSuchFrameException;
26 import org.openqa.selenium.Proxy;
27 import org.openqa.selenium.WebDriver;
28 import org.openqa.selenium.WebElement;
29 import org.openqa.selenium.chrome.ChromeDriver;
30 import org.openqa.selenium.chrome.ChromeDriverService;
31 import org.openqa.selenium.firefox.FirefoxDriver;
32 import org.openqa.selenium.firefox.FirefoxProfile;
33 import org.openqa.selenium.remote.CapabilityType;
34 import org.openqa.selenium.remote.DesiredCapabilities;
35 import org.openqa.selenium.remote.RemoteWebDriver;
36 import org.openqa.selenium.safari.SafariDriver;
37
38 import java.io.BufferedReader;
39 import java.io.File;
40 import java.io.InputStream;
41 import java.io.InputStreamReader;
42 import java.net.MalformedURLException;
43 import java.net.URL;
44 import java.text.SimpleDateFormat;
45 import java.util.Calendar;
46 import java.util.Date;
47 import java.util.LinkedList;
48 import java.util.List;
49 import java.util.concurrent.TimeUnit;
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64 public class WebDriverUtils {
65
66 protected static SauceLabsWebDriverHelper saucelabs;
67
68 public static boolean jGrowlEnabled = false;
69
70 public static boolean jsHighlightEnabled = false;
71
72
73
74
75 public static final String DEFAULT_BASE_URL = "http://localhost:8080/kr-dev";
76
77
78
79
80 public static final String DEFAULT_BASE_URL_KRAD = "http://localhost:8080/krad-dev";
81
82
83
84
85
86
87
88
89 public static final String DONT_TEAR_DOWN_PROPERTY = "remote.driver.dontTearDown";
90
91
92
93
94
95
96
97
98 public static final String DONT_TEAR_DOWN_ON_FAILURE_PROPERTY = "remote.driver.dontTearDownOnFailure";
99
100
101
102
103 public static final String HUB_DRIVER_PROPERTY = "remote.public.driver";
104
105
106
107
108
109 public static final String HUB_PROPERTY = "remote.public.hub";
110
111
112
113
114 public static final String HUB_URL_PROPERTY = "http://localhost:4444/wd/hub";
115
116
117
118
119
120 public static int IMPLICIT_WAIT_TIME_LOOP_MS = 1000;
121
122
123
124
125
126
127
128
129 public static int IMPLICIT_WAIT_TIME_SECONDS_DEFAULT = 30;
130
131
132
133
134
135 public static final boolean JGROWL_ERROR_FAILURE = false;
136
137
138
139
140 public static final String JS_HIGHLIGHT_BACKGROUND = "#66FF33";
141
142
143
144
145 public static final String JS_HIGHLIGHT_BOARDER = "#66FF33";
146
147
148
149
150 public static final int JS_HIGHLIGHT_MS = 400;
151
152
153
154
155
156
157
158
159 public static final String JS_HIGHLIGHT_MS_PROPERTY = "remote.driver.highlight.ms";
160
161
162
163
164
165
166
167
168 public static final String JS_HIGHLIGHT_PROPERTY = "remote.driver.highlight";
169
170
171
172
173
174
175 public static final String JS_HIGHLIGHT_INPUT_PROPERTY = "remote.driver.highlight.input";
176
177
178
179
180
181
182
183
184
185
186 public static final String PROXY_HOST_PROPERTY = "remote.public.proxy";
187
188
189
190
191
192
193
194
195 public static final String REMOTE_AUTOLOGIN_PROPERTY = "remote.autologin";
196
197
198
199
200
201
202
203
204
205
206 public static final String REMOTE_JGROWL_ENABLED = "remote.jgrowl.enabled";
207
208
209
210
211 public static final String REMOTE_LOGIN_UIF = "remote.login.uif";
212
213
214
215
216 public static final String REMOTE_PROPERTIES_PROPERTY = "remote.property.file";
217
218
219
220
221 public static final String REMOTE_PUBLIC_CHROME = "remote.public.chrome";
222
223
224
225
226 public static final String REMOTE_PUBLIC_URL_PROPERTY = "remote.public.url";
227
228
229
230
231
232
233
234
235 public static final String REMOTE_PUBLIC_WAIT_SECONDS_PROPERTY = "remote.public.wait.seconds";
236
237
238
239
240 public static final String REMOTE_PUBLIC_USER_PROPERTY = "remote.public.user";
241
242
243
244
245 public static final String REMOTE_PUBLIC_USERPOOL_PROPERTY = "remote.public.userpool";
246
247
248
249
250
251
252
253
254
255
256 public static final int SETUP_URL_LOAD_WAIT_SECONDS = 120;
257
258
259
260
261 public static final String WEBDRIVER_CHROME_DRIVER = "webdriver.chrome.driver";
262
263
264
265
266
267
268
269
270
271 public static WebDriver setUp(String username, String url) throws Exception {
272 return setUp(username, url, null, null);
273 }
274
275
276
277
278
279
280
281
282
283
284
285 public static WebDriver setUp(String username, String url, String className, String testName) throws Exception {
286 if ("true".equals(System.getProperty(REMOTE_JGROWL_ENABLED, "false"))) {
287 jGrowlEnabled = true;
288 }
289
290 if ("true".equals(System.getProperty(JS_HIGHLIGHT_PROPERTY, "false"))) {
291 jsHighlightEnabled = true;
292 if (System.getProperty(JS_HIGHLIGHT_INPUT_PROPERTY) != null) {
293 InputStream in = WebDriverUtils.class.getResourceAsStream(System.getProperty(JS_HIGHLIGHT_INPUT_PROPERTY));
294 BufferedReader reader = new BufferedReader(new InputStreamReader(in));
295 String line = null;
296 List<String> lines = new LinkedList<String>();
297 while ((line = reader.readLine()) != null) {
298 lines.add(line);
299 }
300 }
301 }
302
303 WebDriver driver = null;
304 if (System.getProperty(SauceLabsWebDriverHelper.REMOTE_DRIVER_SAUCELABS_PROPERTY) == null) {
305 driver = getWebDriver();
306 } else {
307 saucelabs = new SauceLabsWebDriverHelper();
308 saucelabs.setUp(className, testName);
309 driver = saucelabs.getDriver();
310 }
311
312 driver.manage().timeouts().implicitlyWait(SETUP_URL_LOAD_WAIT_SECONDS, TimeUnit.SECONDS);
313
314 if (!System.getProperty(SauceLabsWebDriverHelper.SAUCE_BROWSER_PROPERTY,"ff").equals("opera")) {
315 driver.manage().window().maximize();
316
317 }
318
319
320
321 if (!url.startsWith("http")) {
322 url = getBaseUrlString() + url;
323 }
324
325 driver.get(url);
326 driver.manage().timeouts().implicitlyWait(configuredImplicityWait(), TimeUnit.SECONDS);
327 return driver;
328 }
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344 public static void tearDown(boolean passed, String sessionId, String poolParamTest, String poolParamUser, String className, String testName) throws Exception {
345
346 if (passed) {
347 System.out.println("Registering session passed " + sessionId);
348 } else {
349 System.out.println("Registering session failed " + sessionId);
350 }
351
352 if (System.getProperty(SauceLabsWebDriverHelper.REMOTE_DRIVER_SAUCELABS_PROPERTY) != null) {
353 saucelabs.tearDown(passed, sessionId, className, testName);
354 }
355
356 if (System.getProperty(REMOTE_PUBLIC_USERPOOL_PROPERTY) != null) {
357 AutomatedFunctionalTestUtils.getHTML(AutomatedFunctionalTestUtils.prettyHttp(System.getProperty(
358 REMOTE_PUBLIC_USERPOOL_PROPERTY) + "?test=" + poolParamTest + "&user=" + poolParamUser));
359 }
360 }
361
362
363
364
365
366
367
368
369 public static void acceptAlertIfPresent(WebDriver driver) {
370 if (WebDriverUtils.isAlertPresent(driver)) {
371 System.out.println("Alert present " + WebDriverUtils.alertText(driver));
372 alertAccept(driver);
373 }
374 }
375
376
377
378
379
380
381
382
383 public static void alertAccept(WebDriver driver) {
384 Alert alert = driver.switchTo().alert();
385 jGrowl(driver, "AFT Step", false, "AFT Step: Accept Alert " + WebDriverUtils.alertText(driver));
386 alert.accept();
387 }
388
389
390
391
392
393
394
395
396 public static void alertDismiss(WebDriver driver) {
397 Alert alert = driver.switchTo().alert();
398 jGrowl(driver, "AFT Step", false, "AFT Step: Dismiss Alert " + WebDriverUtils.alertText(driver));
399 alert.dismiss();
400 }
401
402
403
404
405
406
407
408
409
410 public static String alertText(WebDriver driver) {
411 return driver.switchTo().alert().getText();
412 }
413
414
415
416
417
418
419
420
421
422 public static ChromeDriverService chromeDriverCreateCheck() {
423 String driverParam = System.getProperty(HUB_DRIVER_PROPERTY);
424
425 if (driverParam != null && "chrome".equals(driverParam.toLowerCase())) {
426 if (System.getProperty(WEBDRIVER_CHROME_DRIVER) == null) {
427 if (System.getProperty(REMOTE_PUBLIC_CHROME) != null) {
428 System.setProperty(WEBDRIVER_CHROME_DRIVER, System.getProperty(REMOTE_PUBLIC_CHROME));
429 }
430 }
431 try {
432 ChromeDriverService chromeDriverService = new ChromeDriverService.Builder()
433 .usingDriverExecutable(new File(System.getProperty(WEBDRIVER_CHROME_DRIVER)))
434 .usingAnyFreePort()
435 .build();
436 return chromeDriverService;
437 } catch (Throwable t) {
438 throw new RuntimeException("Exception starting chrome driver service, is chromedriver ( http://code.google.com/p/chromedriver/downloads/list ) installed? You can include the path to it using -Dremote.public.chrome", t) ;
439 }
440 }
441 return null;
442 }
443
444
445
446
447
448
449
450
451 public static int configuredImplicityWait() {
452 return Integer.parseInt(System.getProperty(REMOTE_PUBLIC_WAIT_SECONDS_PROPERTY, IMPLICIT_WAIT_TIME_SECONDS_DEFAULT + ""));
453 }
454
455
456
457
458
459
460
461
462
463 public static String deLinespace(String contents) {
464 while (contents.contains("\n\n")) {
465 contents = contents.replaceAll("\n\n", "\n");
466 }
467
468 return contents;
469 }
470
471
472
473
474
475
476
477
478
479
480 public static String determineUser(String testParam) {
481 String user = null;
482
483 if (System.getProperty(REMOTE_PUBLIC_USER_PROPERTY) != null) {
484 return System.getProperty(REMOTE_PUBLIC_USER_PROPERTY);
485 } else if (System.getProperty(REMOTE_PUBLIC_USERPOOL_PROPERTY) != null) {
486 String userResponse = AutomatedFunctionalTestUtils.getHTML(AutomatedFunctionalTestUtils.prettyHttp(
487 System.getProperty(REMOTE_PUBLIC_USERPOOL_PROPERTY) + "?test=" + testParam.trim()));
488 return userResponse.substring(userResponse.lastIndexOf(":") + 2, userResponse.lastIndexOf("\""));
489 }
490
491 return user;
492 }
493
494
495
496
497
498
499
500
501
502
503
504
505 public static boolean dontTearDownPropertyNotSet() {
506 return System.getProperty(DONT_TEAR_DOWN_PROPERTY) == null ||
507 "f".startsWith(System.getProperty(DONT_TEAR_DOWN_PROPERTY).toLowerCase()) ||
508 "n".startsWith(System.getProperty(DONT_TEAR_DOWN_PROPERTY).toLowerCase());
509 }
510
511
512
513
514
515
516
517
518 public static boolean dontTearDownOnFailure(boolean passed) {
519 if (!"n".equalsIgnoreCase(System.getProperty(DONT_TEAR_DOWN_ON_FAILURE_PROPERTY, "n"))) {
520 return passed;
521 }
522 return true;
523 }
524
525
526
527
528
529
530
531
532
533
534 public static WebElement findButtonByText(WebDriver driver, String buttonText) {
535 return findElement(driver, By.xpath("//button[contains(text(), '" + buttonText + "')]"));
536 }
537
538
539
540
541
542
543
544
545
546
547 public static WebElement findElement(WebDriver driver, By by) {
548 WebElement found = driver.findElement(by);
549 WebDriverUtils.highlightElement(driver, found);
550 return found;
551 }
552
553
554
555
556
557
558
559
560
561
562 public static String getBaseUrlString() {
563 String baseUrl = System.getProperty(REMOTE_PUBLIC_URL_PROPERTY);
564 if (baseUrl == null) {
565 baseUrl = DEFAULT_BASE_URL;
566 }
567 baseUrl = AutomatedFunctionalTestUtils.prettyHttp(baseUrl);
568 return baseUrl;
569 }
570
571
572
573
574
575
576 public static String getDateTimeStampFormatted() {
577 Date now = Calendar.getInstance().getTime();
578 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
579 return sdf.format(now);
580 }
581
582
583
584
585
586
587
588
589
590
591
592 public static WebElement getElementByAttributeValue(WebDriver driver, String attributeName, String value){
593 return findElement(driver, By.cssSelector("[" + attributeName + "='" + value +"']"));
594 }
595
596
597
598
599
600
601
602
603
604
605 public static String getHubUrlString() {
606 String hubUrl = System.getProperty(HUB_PROPERTY);
607 if (hubUrl == null) {
608 hubUrl = HUB_URL_PROPERTY;
609 }
610 hubUrl = AutomatedFunctionalTestUtils.prettyHttp(hubUrl);
611 if (!hubUrl.endsWith("/wd/hub")) {
612 hubUrl = hubUrl + "/wd/hub";
613 }
614 return hubUrl;
615 }
616
617
618
619
620
621
622
623
624
625
626
627 public static WebDriver getWebDriver() {
628 String driverParam = System.getProperty(HUB_DRIVER_PROPERTY);
629 String hubParam = System.getProperty(HUB_PROPERTY);
630 String proxyParam = System.getProperty(PROXY_HOST_PROPERTY);
631
632
633 DesiredCapabilities capabilities = new DesiredCapabilities();
634 WebDriver webDriver = null;
635 if (StringUtils.isNotEmpty(proxyParam)) {
636 capabilities.setCapability(CapabilityType.PROXY, new Proxy().setHttpProxy(proxyParam));
637 }
638
639 if (hubParam == null) {
640 if (driverParam == null || "firefox".equalsIgnoreCase(driverParam)) {
641 FirefoxProfile profile = new FirefoxProfile();
642 profile.setEnableNativeEvents(false);
643 capabilities.setCapability(FirefoxDriver.PROFILE, profile);
644 return new FirefoxDriver(capabilities);
645 } else if ("chrome".equalsIgnoreCase(driverParam)) {
646 return new ChromeDriver(capabilities);
647 } else if ("safari".equals(driverParam)) {
648 System.out.println("SafariDriver probably won't work, if it does please contact Erik M.");
649 return new SafariDriver(capabilities);
650 }
651 } else {
652 try {
653 if (driverParam == null || "firefox".equalsIgnoreCase(driverParam)) {
654 return new RemoteWebDriver(new URL(getHubUrlString()), DesiredCapabilities.firefox());
655 } else if ("chrome".equalsIgnoreCase(driverParam)) {
656 return new RemoteWebDriver(new URL(getHubUrlString()), DesiredCapabilities.chrome());
657 }
658 } catch (MalformedURLException mue) {
659 System.out.println(getHubUrlString() + " " + mue.getMessage());
660 mue.printStackTrace();
661 }
662 }
663 return null;
664 }
665
666 public static void highlightElement(WebDriver webDriver, By by) {
667 List<WebElement> elements = webDriver.findElements(by);
668 for (WebElement element : elements) {
669 WebDriverUtils.highlightElement(webDriver, element);
670 }
671 }
672
673
674 public static void highlightElements(WebDriver webDriver, List<WebElement> webElements) {
675 for (WebElement webElement: webElements) {
676 highlightElement(webDriver, webElement);
677 }
678 }
679
680
681
682
683
684
685
686
687
688 public static void highlightElement(WebDriver webDriver, WebElement webElement) {
689 if (jsHighlightEnabled && webElement != null) {
690 try {
691
692 JavascriptExecutor js = (JavascriptExecutor) webDriver;
693 String jsHighlight = "element = arguments[0];\n"
694 + "originalStyle = element.getAttribute('style');\n"
695 + "element.setAttribute('style', originalStyle + \"; background: "
696 + JS_HIGHLIGHT_BACKGROUND + "; border: 2px solid " + JS_HIGHLIGHT_BOARDER + ";\");\n"
697 + "setTimeout(function(){\n"
698 + " element.setAttribute('style', originalStyle);\n"
699 + "}, " + System.getProperty(JS_HIGHLIGHT_MS_PROPERTY, JS_HIGHLIGHT_MS + "") + ");";
700 js.executeScript(jsHighlight, webElement);
701 } catch (Throwable t) {
702 System.out.println("Throwable during javascript highlight element");
703 t.printStackTrace();
704 }
705 }
706 }
707
708
709
710
711
712
713
714
715
716 public static boolean isAlertPresent(WebDriver driver) {
717 try {
718 driver.switchTo().alert();
719 return true;
720 } catch (Exception e) {
721 return false;
722 }
723 }
724
725 public static Boolean isTextPresent(WebDriver driver, String pageText, String text) {
726 boolean textPresent = Boolean.FALSE;
727 if (pageText.contains(text)) {
728 WebDriverUtils.highlightElement(driver, By.xpath("//*[contains(text(), '" + text + "')]"));
729 textPresent = Boolean.TRUE;
730 }
731 WebDriverUtils.jGrowl(driver, "Is Text Present?", false, "Is text '" + text + " present?" + " " + textPresent);
732 return textPresent;
733 }
734
735
736
737
738
739
740
741
742
743
744
745
746 public static void jGrowl(WebDriver driver, String jGrowlHeader, boolean sticky, String message, Throwable throwable) {
747 if (jGrowlEnabled) {
748 jGrowl(driver, jGrowlHeader, sticky, message + " " + throwable.getMessage() + "\n" + ExceptionUtils.getStackTrace(throwable));
749 }
750 }
751
752
753
754
755
756
757
758
759
760
761
762 public static void jGrowl(WebDriver driver, String jGrowlHeader, boolean sticky, String message) {
763 stepMessage(message);
764 if (jGrowlEnabled) {
765 try {
766 String javascript="jQuery.jGrowl('" + message + "' , {sticky: " + sticky + ", header : '" + jGrowlHeader + "'});";
767 ((JavascriptExecutor) driver).executeScript(javascript);
768 } catch (Throwable t) {
769 jGrowlException(t);
770 }
771 }
772 }
773
774
775
776
777
778
779
780
781 public static void jGrowlException(Throwable throwable) {
782 String failMessage = throwable.getMessage() + "\n" + ExceptionUtils.getStackTrace(throwable);
783 System.out.println("jGrowl failure " + failMessage);
784 if (JGROWL_ERROR_FAILURE) {
785 SeleneseTestBase.fail(failMessage);
786 }
787 }
788
789
790
791
792
793
794
795
796
797 public static void selectFrameSafe(WebDriver driver, String locator) {
798 try {
799 driver.switchTo().frame(locator);
800 } catch (NoSuchFrameException nsfe) {
801
802 }
803 }
804
805 public static void stepMessage(String message) {
806 System.out.println("AFT Step: " + message);
807 }
808
809
810
811
812
813
814
815
816
817
818
819
820
821 public static WebElement waitAndGetElementByAttributeValue(WebDriver driver, String attribute, String attributeValue, int waitSeconds) throws InterruptedException {
822
823
824
825
826 driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
827
828 boolean failed = false;
829
830 for (int second = 0;; second++) {
831 Thread.sleep(1000);
832 if (second >= waitSeconds) {
833 failed = true;
834 }
835 try {
836 if (failed || (getElementByAttributeValue(driver, attribute, attributeValue) != null)) {
837 break;
838 }
839 } catch (Exception e) {}
840 }
841
842 WebElement element = getElementByAttributeValue(driver, attribute, attributeValue);
843 driver.manage().timeouts().implicitlyWait(WebDriverUtils.configuredImplicityWait(), TimeUnit.SECONDS);
844 return element;
845 }
846
847 public static void waitToAcceptAlert(WebDriver driver, int waitSeconds, String message) throws InterruptedException {
848 driver.manage().timeouts().implicitlyWait(IMPLICIT_WAIT_TIME_LOOP_MS, TimeUnit.MILLISECONDS);
849
850 boolean failed = false;
851
852 for (int second = 0;; second++) {
853 Thread.sleep(1000);
854 if (second >= waitSeconds) {
855 failed = true;
856 }
857 try {
858 if (failed) {
859 break;
860 } else if (isAlertPresent(driver)) {
861 acceptAlertIfPresent(driver);
862 break;
863 }
864 } catch (Exception e) {}
865 }
866
867 driver.manage().timeouts().implicitlyWait(configuredImplicityWait(), TimeUnit.SECONDS);
868 }
869
870
871
872
873
874
875
876
877
878
879
880
881
882 public static WebElement waitFor(WebDriver driver, int waitSeconds, By by, String message) throws InterruptedException {
883
884
885
886
887 driver.manage().timeouts().implicitlyWait(IMPLICIT_WAIT_TIME_LOOP_MS, TimeUnit.MILLISECONDS);
888
889 boolean failed = false;
890 WebElement element = null;
891
892 for (int second = 0;; second++) {
893 Thread.sleep(1000);
894 if (second >= waitSeconds) {
895 failed = true;
896 }
897 try {
898 if (failed) {
899 break;
900 } else if ((driver.findElements(by)).size() > 0) {
901 element = findElement(driver, by);
902 highlightElement(driver, element);
903 break;
904 }
905 } catch (Exception e) {}
906 }
907
908 driver.manage().timeouts().implicitlyWait(configuredImplicityWait(), TimeUnit.SECONDS);
909 return element;
910 }
911
912
913
914
915
916
917
918
919
920
921
922 public static List<WebElement> waitFors(WebDriver driver, By by) throws InterruptedException {
923 return waitFors(driver, configuredImplicityWait(), by, "");
924 }
925
926
927
928
929
930
931
932
933
934
935
936
937 public static List<WebElement> waitFors(WebDriver driver, By by, String message) throws InterruptedException {
938 return waitFors(driver, configuredImplicityWait(), by, message);
939 }
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954 public static List<WebElement> waitFors(WebDriver driver, int waitSeconds, By by, String message) throws InterruptedException {
955
956
957
958
959 driver.manage().timeouts().implicitlyWait(IMPLICIT_WAIT_TIME_LOOP_MS, TimeUnit.MILLISECONDS);
960
961 boolean failed = false;
962
963 for (int second = 0;; second++) {
964 Thread.sleep(1000);
965 if (second >= waitSeconds) {
966 failed = true;
967 }
968 try {
969 if (failed || (driver.findElements(by)).size() > 0) {
970 break;
971 }
972 } catch (Exception e) {}
973 }
974
975 driver.manage().timeouts().implicitlyWait(configuredImplicityWait(), TimeUnit.SECONDS);
976 return driver.findElements(by);
977 }
978 }