1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package edu.samplu.common;
17
18 import org.junit.After;
19 import org.junit.Assert;
20 import org.junit.Before;
21 import org.junit.BeforeClass;
22 import org.junit.Rule;
23 import org.junit.rules.TestName;
24 import org.openqa.selenium.By;
25 import org.openqa.selenium.JavascriptExecutor;
26 import org.openqa.selenium.NoSuchFrameException;
27 import org.openqa.selenium.WebDriver;
28 import org.openqa.selenium.WebElement;
29 import org.openqa.selenium.chrome.ChromeDriverService;
30 import org.openqa.selenium.interactions.Actions;
31 import org.openqa.selenium.remote.RemoteWebDriver;
32
33 import java.io.BufferedReader;
34 import java.io.InputStreamReader;
35 import java.net.HttpURLConnection;
36 import java.net.URL;
37 import java.util.List;
38 import java.util.Set;
39 import java.util.concurrent.TimeUnit;
40
41 import static com.thoughtworks.selenium.SeleneseTestBase.fail;
42 import static org.junit.Assert.assertEquals;
43
44
45
46
47
48
49 public abstract class WebDriverLegacyITBase {
50
51 public static final int DEFAULT_WAIT_SEC = 60;
52 public static final String REMOTE_PUBLIC_USERPOOL_PROPERTY = "remote.public.userpool";
53 public static final String REMOTE_PUBLIC_USER_PROPERTY = "remote.public.user";
54
55 public abstract String getTestUrl();
56
57 protected WebDriver driver;
58 protected String user = "admin";
59 protected boolean passed = false;
60 static ChromeDriverService chromeDriverService;
61
62 public @Rule TestName testName= new TestName();
63
64 String sessionId = null;
65
66 public String getSessionId() {
67 return sessionId;
68 }
69
70 @BeforeClass
71 public static void createAndStartService() throws Exception {
72 chromeDriverService = WebDriverUtil.createAndStartService();
73 if (chromeDriverService != null) chromeDriverService.start();
74 }
75
76
77
78
79
80
81 @Before
82 public void setUp() throws Exception {
83
84 try {
85 if (System.getProperty(REMOTE_PUBLIC_USER_PROPERTY) != null) {
86 user = System.getProperty(REMOTE_PUBLIC_USER_PROPERTY);
87 } else if (System.getProperty(REMOTE_PUBLIC_USERPOOL_PROPERTY) != null) {
88 String userResponse = getHTML(ITUtil.prettyHttp(System.getProperty(REMOTE_PUBLIC_USERPOOL_PROPERTY) + "?test=" + this.toString().trim()));
89 user = userResponse.substring(userResponse.lastIndexOf(":" ) + 2, userResponse.lastIndexOf("\""));
90 }
91 driver = WebDriverUtil.setUp(getUserName(), ITUtil.getBaseUrlString() + getTestUrl(), getClass().getSimpleName(), testName);
92 this.sessionId = ((RemoteWebDriver)driver).getSessionId().toString();
93 } catch (Exception e) {
94 fail("Exception in setUp " + e.getMessage());
95 e.printStackTrace();
96 }
97 ITUtil.login(driver, user);
98 }
99
100 @After
101 public void tearDown() throws Exception {
102 try {
103
104
105
106 if (System.getProperty(REMOTE_PUBLIC_USERPOOL_PROPERTY) != null) {
107 getHTML(ITUtil.prettyHttp(System.getProperty(REMOTE_PUBLIC_USERPOOL_PROPERTY) + "?test=" + this.toString() + "&user=" + user));
108 }
109 } catch (Exception e) {
110 System.out.println("Exception in tearDown " + e.getMessage());
111 e.printStackTrace();
112 } finally {
113 if (driver != null) {
114 if (ITUtil.dontTearDownPropertyNotSet()) {
115 driver.close();
116 driver.quit();
117 }
118 } else {
119 System.out.println("WebDriver is null, if using saucelabs, has sauceleabs been uncommented in WebDriverUtil.java? If using a remote hub did you include the port?");
120 }
121 }
122 }
123
124 protected String getHTML(String urlToRead) {
125 URL url;
126 HttpURLConnection conn;
127 BufferedReader rd;
128 String line;
129 String result = "";
130 try {
131 url = new URL(urlToRead);
132 conn = (HttpURLConnection) url.openConnection();
133 conn.setRequestMethod("GET");
134 rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
135 while ((line = rd.readLine()) != null) {
136 result += line;
137 }
138 rd.close();
139 } catch (Exception e) {
140 e.printStackTrace();
141 }
142 return result;
143 }
144
145 protected void passed() {
146 passed = true;
147 }
148
149 protected void assertElementPresentByName(String name) {
150 driver.findElement(By.name(name));
151 }
152
153 protected void assertElementPresentByName(String name,String message) {
154 try{
155 driver.findElement(By.name(name));
156 }catch(Exception e){
157 Assert.fail(name+ " not present "+ message);
158 }
159 }
160
161 protected void assertElementPresentByXpath(String locator) {
162 driver.findElement(By.xpath(locator));
163 }
164
165 protected void assertElementPresentByXpath(String locator,String message) {
166 try{
167 driver.findElement(By.xpath(locator));
168 }catch(Exception e){
169 Assert.fail(locator+ " not present "+ message);
170 }
171 }
172
173 protected void assertElementPresent(String locator) {
174 driver.findElement(By.cssSelector(locator));
175 }
176
177 protected void assertTextPresent(String text) {
178 assertTextPresent(text, "");
179 }
180
181 protected void assertTextPresent(String text, String message) {
182 if (!driver.getPageSource().contains(text)) {
183 Assert.fail(text + " not present " + message);
184 }
185 }
186
187 protected void blanketApproveTest() throws InterruptedException {
188 ITUtil.checkForIncidentReport(driver.getPageSource(), "methodToCall.blanketApprove", "");
189 waitAndClickByName("methodToCall.blanketApprove", "No blanket approve button does the user " + getUserName() + " have permission?");
190 Thread.sleep(2000);
191
192 if (driver.findElements(By.xpath(ITUtil.DIV_ERROR_LOCATOR)).size()>0) {
193 String errorText = driver.findElement(By.xpath(ITUtil.DIV_ERROR_LOCATOR)).getText();
194 if (errorText != null && errorText.contains("error(s) found on page.")) {
195 errorText = ITUtil.blanketApprovalCleanUpErrorText(errorText);
196 if (driver.findElements(By.xpath(ITUtil.DIV_EXCOL_LOCATOR)).size()>0) {
197 errorText = ITUtil.blanketApprovalCleanUpErrorText(driver.findElement(By.xpath(ITUtil.DIV_EXCOL_LOCATOR)).getText());
198 }
199
200
201
202
203 Assert.fail(errorText);
204 }
205 }
206 ITUtil.checkForIncidentReport(driver.getPageSource(), "//img[@alt='doc search']", "Blanket Approve failure");
207 waitAndClickByXpath("//img[@alt='doc search']");
208 assertEquals("Kuali Portal Index", driver.getTitle());
209 selectFrame("iframeportlet");
210 waitAndClickByXpath("//input[@name='methodToCall.search' and @value='search']");
211 }
212
213 protected void checkForIncidentReport() {
214 checkForIncidentReport("", "");
215 }
216
217 protected void checkForIncidentReport(String locator) {
218 checkForIncidentReport(locator, "");
219 }
220
221 protected void checkForIncidentReport(String locator, String message) {
222 WebDriverUtil.checkForIncidentReport(driver, locator, message);
223 }
224
225 protected void clearText(By by) throws InterruptedException {
226 driver.findElement(by).clear();
227 }
228
229 protected void clearTextByName(String name) throws InterruptedException {
230 clearText(By.name(name));
231 }
232
233 protected void clearTextByXpath(String locator) throws InterruptedException {
234 clearText(By.xpath(locator));
235 }
236
237 protected String getAttribute(By by, String attribute) throws InterruptedException {
238 waitFor(by);
239 return driver.findElement(by).getAttribute(attribute);
240 }
241
242
243
244
245
246
247
248 protected String getAttributeByName(String name,String attribute) throws InterruptedException {
249 return getAttribute(By.name(name),attribute);
250 }
251
252
253
254
255
256
257
258 protected String getAttributeByXpath(String locator,String attribute) throws InterruptedException {
259 return getAttribute(By.xpath(locator),attribute);
260 }
261
262 protected String getBaseUrlString() {
263 return ITUtil.getBaseUrlString();
264 }
265
266 protected String getText(By by) throws InterruptedException {
267 return driver.findElement(by).getText();
268 }
269
270 protected String getTextByName(String name) throws InterruptedException {
271 return getText(By.name(name));
272 }
273
274 protected String getText(String locator) throws InterruptedException {
275 return getText(By.cssSelector(locator));
276 }
277
278 protected String getTextByXpath(String locator) throws InterruptedException {
279 return getText(By.xpath(locator));
280 }
281
282 protected String getTitle() {
283 return driver.getTitle();
284 }
285
286
287
288
289
290 public String getUserName() {
291 return user;
292 }
293
294 protected boolean isElementPresent(By by) {
295 return (driver.findElements(by)).size()>0;
296 }
297
298 protected boolean isElementPresent(String locator) {
299 return (driver.findElements(By.cssSelector(locator))).size()>0;
300 }
301
302 protected boolean isElementPresentByName(String name) {
303 return isElementPresent(By.name(name));
304 }
305
306 protected boolean isElementPresentByXpath(String locator) {
307 return isElementPresent(By.xpath(locator));
308 }
309
310 protected void open(String url) {
311 driver.get(url);
312 }
313
314 protected void selectFrame(String locator) {
315 try {
316 driver.switchTo().frame(locator);
317 } catch (NoSuchFrameException nsfe) {
318
319 }
320 }
321
322 protected void selectTopFrame() {
323 driver.switchTo().defaultContent();
324 }
325
326 protected void selectWindow(String locator) {
327 driver.switchTo().window(locator);
328 }
329
330 protected void testCancelConfirmation() throws InterruptedException {
331 waitAndCancelConfirmation();
332 passed();
333 }
334
335 protected void testCreateNewSearchReturnValueCancelConfirmation() throws InterruptedException, Exception {
336 selectFrame("iframeportlet");
337 waitAndCreateNew();
338 waitAndSearch();
339 waitAndReturnValue();
340 waitAndCancelConfirmation();
341 passed();
342 }
343
344 protected void testSearchEditCancel() throws InterruptedException {
345 selectFrame("iframeportlet");
346 waitAndSearch();
347 waitAndEdit();
348 testCancelConfirmation();
349 }
350
351 protected void testVerifyAddDeleteFiscalOfficerLegacy() throws Exception {
352 selectFrame("iframeportlet");
353 waitAndTypeByName("document.documentHeader.documentDescription", ITUtil.DTS_TWO);
354 waitAndTypeByName("newCollectionLines['document.newMaintainableObject.dataObject.fiscalOfficer.accounts'].number", "1234567890");
355 waitAndTypeByName("newCollectionLines['document.newMaintainableObject.dataObject.fiscalOfficer.accounts'].foId", "2");
356
357 waitAndClickByXpath("//button[@data-loadingmessage='Adding Line...']");
358
359 assertElementPresentByName("document.newMaintainableObject.dataObject.fiscalOfficer.accounts[0].number", "https://jira.kuali.org/browse/KULRICE-8564");
360
361 assertEquals("1234567890", getAttributeByName("document.newMaintainableObject.dataObject.fiscalOfficer.accounts[0].number","value"));
362 assertEquals("2", getAttributeByName("document.newMaintainableObject.dataObject.fiscalOfficer.accounts[0].foId","value"));
363
364 waitAndClickByXpath("//button[@data-loadingmessage='Deleting Line...']");
365
366 assertElementPresentByName("document.newMaintainableObject.dataObject.fiscalOfficer.accounts[0].number");
367 passed();
368 }
369
370 protected void waitAndCancelConfirmation() throws InterruptedException {
371 waitAndClickByName("methodToCall.cancel");
372 waitAndClickByName("methodToCall.processAnswer.button0");
373 }
374
375 protected void waitAndCreateNew() throws InterruptedException {
376 waitAndClickByXpath("//img[@alt='create new']");
377
378 }
379
380 protected void waitAndEdit() throws InterruptedException {
381 waitAndClickByLinkText("edit");
382 }
383
384 protected void waitAndReturnValue() throws InterruptedException {
385 waitAndClickByLinkText("return value");
386 }
387
388 protected void waitAndSearch() throws InterruptedException {
389 waitAndClickByXpath("//input[@value='search']");
390
391
392
393 }
394
395 protected String waitForDocId() throws InterruptedException {
396 waitForElementPresentByXpath("//div[@id='headerarea']/div/table/tbody/tr[1]/td[1]");
397 return driver.findElement(By.xpath("//div[@id='headerarea']/div/table/tbody/tr[1]/td[1]")).getText();
398 }
399
400 protected void waitForElementPresent(String locator) throws InterruptedException {
401 waitFor(By.cssSelector(locator));
402 }
403
404 protected void waitForElementPresentByXpath(String locator) throws InterruptedException {
405 waitFor(By.xpath(locator));
406 }
407
408 protected void waitForElementPresentByName(String name) throws InterruptedException {
409 waitFor(By.name(name));
410 }
411
412 protected void waitForTitleToEqualKualiPortalIndex() throws InterruptedException {
413 waitForTitleToEqualKualiPortalIndex("");
414 }
415
416 protected void waitForTitleToEqualKualiPortalIndex(String message) throws InterruptedException {
417 Thread.sleep(2000);
418
419
420
421
422
423
424
425
426
427 }
428
429 protected void waitAndClick(String locator) throws InterruptedException {
430 waitAndClick(locator, "");
431 }
432
433 protected void waitForPageToLoad() {
434
435 }
436
437 protected void waitFor(By by) throws InterruptedException {
438 waitFor(by, "");
439 }
440
441 protected void waitFor(By by, String message) throws InterruptedException {
442
443 Thread.sleep(1000);
444 driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
445
446
447 try { driver.findElement(by);
448
449 } catch (Exception e) {}
450 driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
451
452 }
453
454 protected void waitAndClick(By by) throws InterruptedException {
455 waitAndClick(by, "");
456 }
457
458 protected void waitAndClick(By by, String message) throws InterruptedException {
459 waitFor(by, message);
460 try {
461 (driver.findElement(by)).click();
462 } catch (Exception e) {
463 fail(e.getMessage() + " " + by.toString() + " " + message + " " + driver.getCurrentUrl() );
464 e.printStackTrace();
465 }
466 }
467
468 protected void waitAndClick(String locator, String message) throws InterruptedException {
469 waitAndClick(By.cssSelector(locator), message);
470 }
471
472 protected void waitAndClickByLinkText(String text) throws InterruptedException {
473 waitAndClick(By.linkText(text),"");
474 }
475
476 protected void waitAndClickByLinkText(String text, String message) throws InterruptedException {
477 waitAndClick(By.linkText(text), message);
478 }
479
480 protected void waitAndClickByName(String name) throws InterruptedException {
481 waitAndClick(By.name(name), "");
482 }
483
484 protected void waitAndClickByXpath(String xpath) throws InterruptedException {
485 waitAndClick(By.xpath(xpath));
486 }
487
488 protected void waitAndClickByName(String name, String message) throws InterruptedException {
489 waitAndClick(By.name(name), message);
490 }
491
492 protected void waitAndClickByXpath(String xpath, String message) throws InterruptedException {
493 waitAndClick(By.xpath(xpath), message);
494 }
495
496 protected void waitAndType(By by, String text) throws InterruptedException {
497 waitFor(by, "");
498 try {
499 (driver.findElement(by)).sendKeys(text);
500 } catch (Exception e) {
501 fail(e.getMessage() + " " + by.toString() + " " + text + " " + driver.getCurrentUrl());
502 e.printStackTrace();
503 }
504 }
505
506 protected void waitAndType(By by, String text, String message) throws InterruptedException {
507 waitFor(by, "");
508 try {
509 (driver.findElement(by)).sendKeys(text);
510 } catch (Exception e) {
511 fail(e.getMessage() + " " + by.toString() + " " + text + " " + message + " " + driver.getCurrentUrl());
512 e.printStackTrace();
513 }
514 }
515
516 protected void waitAndTypeByXpath(String locator, String text) throws InterruptedException {
517 waitAndType(By.xpath(locator), text);
518 }
519
520 protected void waitAndTypeByXpath(String locator, String text, String message) throws InterruptedException {
521 waitAndType(By.xpath(locator), text, message);
522 }
523
524 protected void waitAndTypeByName(String name, String text) throws InterruptedException {
525 waitAndType(By.name(name), text);
526 }
527
528 protected void selectByXpath(String locator, String selectText) throws InterruptedException {
529 select(By.xpath(locator), selectText);
530 }
531
532 protected void selectByName(String name, String selectText) throws InterruptedException {
533 select(By.name(name), selectText);
534 }
535
536 protected void select(By by, String selectText) throws InterruptedException {
537 WebElement select1 = driver.findElement(by);
538 List<WebElement> options = select1.findElements(By.tagName("option"));
539 for(WebElement option : options){
540 if(option.getText().equals(selectText)){
541 option.click();
542 break;
543 }
544 }
545 }
546
547 protected void selectOptionByName(String name, String optionValue) throws InterruptedException {
548 selectOption(By.name(name), optionValue);
549 }
550
551 protected void selectOptionByXpath(String locator, String optionValue) throws InterruptedException {
552 selectOption(By.name(locator), optionValue);
553 }
554
555 protected void selectOption(By by, String optionValue) throws InterruptedException {
556 WebElement select1 = driver.findElement(by);
557 List<WebElement> options = select1.findElements(By.tagName("option"));
558 for(WebElement option : options){
559 if(option.getAttribute("value").equals(optionValue)){
560 option.click();
561 break;
562 }
563 }
564 }
565
566 protected String[] getSelectOptions(By by) throws InterruptedException {
567 WebElement select1 = driver.findElement(by);
568 List<WebElement> options = select1.findElements(By.tagName("option"));
569 String[] optionValues = new String[options.size()];
570 int counter=0;
571 for(WebElement option : options){
572 optionValues[counter] = option.getAttribute("value");
573 counter++;
574 }
575 return optionValues;
576 }
577
578 protected String[] getSelectOptionsByName(String name) throws InterruptedException {
579 return getSelectOptions(By.name(name));
580 }
581
582 protected String[] getSelectOptionsByXpath(String locator) throws InterruptedException {
583 return getSelectOptions(By.xpath(locator));
584 }
585
586 protected int getCssCount(String selector) {
587 return getCssCount(By.cssSelector(selector));
588 }
589
590 protected int getCssCount(By by) {
591 return (driver.findElements(by)).size();
592 }
593
594 protected void checkErrorMessageItem(String message)
595 {
596 final String error_locator = "//li[@class='uif-errorMessageItem']";
597 assertElementPresentByXpath(error_locator);
598 String errorText=null;
599 try {
600 errorText = getTextByXpath(error_locator);
601 } catch (InterruptedException e) {
602 e.printStackTrace();
603 }
604 if (errorText != null && errorText.contains("errors")) {
605 Assert.fail(errorText + message);
606 }
607
608 }
609
610 protected boolean isVisible(String locator) {
611 return driver.findElement(By.cssSelector(locator)).isDisplayed();
612 }
613
614 protected boolean isVisible(By by) {
615 return driver.findElement(by).isDisplayed();
616 }
617
618 protected boolean isVisibleByXpath(String locator) {
619 return isVisible(By.xpath(locator));
620 }
621
622 protected void waitNotVisible(By by) throws InterruptedException {
623 for (int second = 0;; second++) {
624 if (second >= 60) {
625 Assert.fail("timeout");
626 }
627
628 if (!isVisible(by)) {
629 break;
630 }
631
632 Thread.sleep(1000);
633 }
634 }
635
636 protected void waitNotVisibleByXpath(String locator) throws InterruptedException {
637 waitNotVisible(By.xpath(locator));
638 }
639
640 protected void waitIsVisible(By by) throws InterruptedException {
641 for (int second = 0;; second++) {
642 if (second >= 60) {
643 Assert.fail("timeout");
644 }
645 if (isVisible(by)) {
646 break;
647 }
648 Thread.sleep(1000);
649 }
650 }
651
652 protected void waitForElementVisible(String elementLocator, String message) throws InterruptedException {
653 boolean failed = false;
654 for (int second = 0;; second++) {
655 if (second >= 60) failed = true;
656 try { if (failed || (driver.findElements(By.cssSelector(elementLocator))).size()>0) break; } catch (Exception e) {}
657 Thread.sleep(1000);
658 }
659 checkForIncidentReport(elementLocator);
660 if (failed) fail("timeout of 60 seconds waiting for " + elementLocator + " " + message + " " + driver.getCurrentUrl());
661 }
662
663 protected void waitIsVisible(String locator) throws InterruptedException {
664 waitIsVisible(By.cssSelector(locator));
665 }
666
667 protected void waitIsVisibleByXpath(String locator) throws InterruptedException {
668 waitIsVisible(By.xpath(locator));
669 }
670
671 protected void colapseExpandByXpath(String clickLocator, String visibleLocator) throws InterruptedException {
672 waitAndClickByXpath(clickLocator);
673 waitNotVisibleByXpath(visibleLocator);
674
675 waitAndClickByXpath(clickLocator);
676 waitIsVisibleByXpath(visibleLocator);
677 }
678
679 protected void expandColapseByXpath(String clickLocator, String visibleLocator) throws InterruptedException {
680 waitAndClickByXpath(clickLocator);
681 waitIsVisibleByXpath(visibleLocator);
682
683 waitAndClickByXpath(clickLocator);
684 waitNotVisibleByXpath(visibleLocator);
685 }
686
687 public void switchToWindow(String title) {
688 Set<String> windows = driver.getWindowHandles();
689
690 for (String window : windows) {
691 driver.switchTo().window(window);
692 if (driver.getTitle().contains(title)) {
693 return;
694 }
695 }
696 }
697
698 protected void check(By by) throws InterruptedException {
699 WebElement element =driver.findElement(by);
700 if(!element.isSelected()){
701 element.click();
702 }
703 }
704
705 protected void checkByName(String name) throws InterruptedException {
706 check(By.name(name));
707 }
708
709 protected void checkByXpath(String locator) throws InterruptedException {
710 check(By.xpath(locator));
711 }
712
713 protected void uncheck(By by) throws InterruptedException {
714 WebElement element =driver.findElement(by);
715 if(element.isSelected()){
716 element.click();
717 }
718 }
719
720 protected void uncheckByName(String name) throws InterruptedException {
721 uncheck(By.name(name));
722 }
723
724 protected void uncheckByXpath(String locator) throws InterruptedException {
725 uncheck(By.xpath(locator));
726 }
727
728 protected void fireEvent(String name, String event) {
729 ((JavascriptExecutor)driver).executeScript(
730 "var elements=document.getElementsByName(\""+name+"\");"+
731 "for (var i = 0; i < elements.length; i++){"+
732 "elements[i]."+event+"();}"
733 );
734 }
735
736 protected void fireEvent(String name, String value, String event) {
737 ((JavascriptExecutor)driver).executeScript(
738 "var elements=document.getElementsByName(\""+name+"\");"+
739 "for (var i = 0; i < elements.length; i++){"+
740 "if(elements[i].value=='"+value+"')"+
741 "elements[i]."+event+"();}"
742 );
743 }
744
745 public void fireMouseOverEventByName(String name) {
746 this.fireMouseOverEvent(By.name(name));
747 }
748
749 public void fireMouseOverEventByXpath(String locator) {
750 this.fireMouseOverEvent(By.xpath(locator));
751 }
752
753 public void fireMouseOverEvent(By by) {
754 Actions builder = new Actions(driver);
755 Actions hover = builder.moveToElement(driver.findElement(by));
756 hover.perform();
757
758 }
759 }