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 org.junit.Assert;
19 import org.kuali.rice.testtools.common.JiraAwareFailable;
20 import org.kuali.rice.testtools.common.JiraAwareFailureUtils;
21 import org.openqa.selenium.By;
22 import org.openqa.selenium.WebDriver;
23 import org.openqa.selenium.WebElement;
24
25 import java.util.List;
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41 public abstract class JiraAwareAftBase extends AutomatedFunctionalTestBase implements JiraAwareFailable {
42
43
44
45
46 private boolean passed = false;
47
48
49
50
51
52
53
54
55 protected abstract void checkForIncidentReport(String locator, String message);
56
57
58
59
60
61
62 protected abstract WebDriver getDriver();
63
64
65
66
67
68
69 protected void assertButtonDisabledByText(String buttonText) {
70 JiraAwareWebDriverUtils.assertButtonDisabledByText(getDriver(), buttonText, this);
71 }
72
73
74
75
76
77
78 protected void assertButtonEnabledByText(String buttonText) {
79 JiraAwareWebDriverUtils.assertButtonEnabledByText(getDriver(), buttonText, this);
80 }
81
82 protected void assertDataTableContains(String[][] data) throws InterruptedException {
83 boolean dataPresent = true;
84 String missingMessage = "";
85 String dataTableRow;
86 for (int i = 0, s = data.length; i < s; i++) {
87 dataTableRow = findDataTableRow(data[i][0]).getText();
88 for (int j = 1, t = data[i].length; j < t; j++) {
89 if (!dataTableRow.contains(data[i][j])) {
90 dataPresent = false;
91 missingMessage += data[i][j] + " not present in data table row containing " + data[i][0] + ". ";
92 }
93 }
94 WebDriverUtils.jGrowl(getDriver(), "Assert DataTable Row", false, "Assert datatable row '" + dataTableRow
95 + "' contains '" + data[i] + "' " + dataPresent);
96 }
97 if (!dataPresent) {
98 jiraAwareFail(missingMessage);
99 }
100 }
101
102 protected void assertDataTableContains(String[][] data, String tableClass) throws InterruptedException {
103 boolean dataPresent = true;
104 String missingMessage = "";
105 String dataTableRow;
106 for (int i = 0, s = data.length; i < s; i++) {
107 dataTableRow = findDataTableRow(data[i][0], tableClass).getText();
108 for (int j = 1, t = data[i].length; j < t; j++) {
109 if (!dataTableRow.contains(data[i][j])) {
110 dataPresent = false;
111 missingMessage += data[i][j] + " not present in data table row containing " + data[i][0] + ". ";
112 }
113 }
114 }
115 if (!dataPresent) {
116 jiraAwareFail(missingMessage);
117 }
118 }
119
120 protected void assertElementPresentByName(String name) {
121 assertElementPresentByName(name, this.getClass().toString());
122 }
123
124 protected void assertElementPresentByName(String name, String message) {
125 try {
126 findElement(By.name(name));
127 } catch (Exception e) {
128 jiraAwareFail(name + " not present " + message);
129 }
130 }
131
132 protected void assertElementPresentByXpath(String locator) {
133 assertElementPresentByXpath(locator, this.getClass().toString());
134 }
135
136 protected void assertElementPresentByXpath(String locator, String message) {
137 try {
138 findElement(By.xpath(locator));
139 } catch (Exception e) {
140 jiraAwareFail(By.xpath(locator), message, e);
141 }
142 }
143
144 protected void assertElementPresentByLinkText(String linkText) {
145 try {
146 findElement(By.linkText(linkText));
147 } catch (Exception e) {
148 jiraAwareFail(By.cssSelector(linkText), this.getClass().toString(), e);
149 }
150
151 }
152
153 protected void assertElementPresent(String locator) {
154 try {
155 findElement(By.cssSelector(locator));
156 } catch (Exception e) {
157 jiraAwareFail(By.cssSelector(locator), this.getClass().toString(), e);
158 }
159 }
160
161 protected void assertEquals(boolean expected, boolean actual) {
162 if (expected != actual) {
163 jiraAwareFail("Expected \"" + expected + "\" but saw \"" + actual + "\" instead");
164 }
165 }
166
167 protected void assertEquals(int expected, int actual) {
168 if (expected != actual) {
169 jiraAwareFail("Expected \"" + expected + "\" but saw \"" + actual + "\" instead");
170 }
171 }
172
173 protected void assertEquals(String message, int expected, int actual) {
174 if (expected != actual) {
175 jiraAwareFail("Expected \"" + expected + "\" but saw \"" + actual + "\" instead " + message);
176 }
177 }
178
179
180 protected void assertEquals(String expected, String actual) {
181 if (!expected.equals(actual)) {
182 jiraAwareFail("Expected \"" + expected + "\" but saw \"" + actual + "\" instead");
183 }
184 }
185
186
187
188
189
190
191 protected void assertFalse(boolean booleanToAssertFalse) {
192 JiraAwareWebDriverUtils.assertFalse(booleanToAssertFalse, this);
193 }
194
195
196
197
198
199
200
201 protected void assertFalse(String message, boolean booleanToAssertFalse) {
202 JiraAwareWebDriverUtils.assertFalse(message, booleanToAssertFalse, this);
203 }
204
205 protected void assertIsVisible(String locator) {
206 if (!isVisible(locator)) {
207 jiraAwareFail(locator + " is not visible and should be");
208 }
209 }
210
211 protected void assertIsVisible(By by, String message) {
212 if (!isVisible(by)) {
213 jiraAwareFail(by + " not visible " + message);
214 }
215 }
216
217 protected void assertIsVisibleById(String id) {
218 if (!isVisibleById(id)) {
219 jiraAwareFail(id + " is not visible and should be");
220 }
221 }
222
223 protected void assertIsVisibleByXpath(String xpath, String message) {
224 if (!isVisibleByXpath(xpath)) {
225 jiraAwareFail(xpath + " not visible " + message);
226 }
227 }
228
229 protected void assertIsNotVisible(By by) {
230 assertIsNotVisible(by, this.getClass().toString());
231 }
232
233 protected void assertIsNotVisible(By by, String message) {
234 if (isVisible(by)) {
235 jiraAwareFail(by + " is visible and should not be " + message);
236 }
237 }
238
239 protected void assertIsNotVisible(String locator) {
240 if (isVisible(locator)) {
241 jiraAwareFail(locator + " is visible and should not be");
242 }
243 }
244
245 protected void assertIsNotVisibleByXpath(String xpath) {
246 if (isVisible(By.xpath(xpath))) {
247 jiraAwareFail(xpath + " is visible and should not be");
248 }
249 }
250
251 protected void assertIsNotVisibleByXpath(String xpath, String message) {
252 if (isVisibleByXpath(xpath)) {
253 jiraAwareFail(xpath + " visible and should not be " + message);
254 }
255 }
256
257 protected void assertLabeledTextNotPresent(String[][] labeledText) {
258 boolean allLabeledTextNotPresent = true;
259 String missingMessage = "";
260 for (int i = 0, s = labeledText.length; i < s; i++) {
261 if (isLabeledTextPresent(labeledText[i][0], labeledText[i][1])) {
262 allLabeledTextNotPresent = false;
263 missingMessage += "Text: " + labeledText[i][1] + " labeled by: " + labeledText[i][0] + " present. ";
264 }
265 }
266 if (!allLabeledTextNotPresent) {
267 jiraAwareFail(missingMessage);
268 }
269 }
270
271 protected void assertLabeledTextPresent(String[][] labeledText) {
272 boolean allLabeledTextPresent = true;
273 String missingMessage = "";
274 for (int i = 0, s = labeledText.length; i < s; i++) {
275 if (!isLabeledTextPresent(labeledText[i][0], labeledText[i][1])) {
276 allLabeledTextPresent = false;
277 missingMessage += "Text: " + labeledText[i][1] + " labeled by: " + labeledText[i][0] + " not present. ";
278 }
279 }
280 if (!allLabeledTextPresent) {
281 jiraAwareFail(missingMessage);
282 }
283 }
284
285 protected void assertLabeledTextPresent(String label, String text) {
286 if (!isLabeledTextPresent(label, text)) {
287 jiraAwareFail("Text: " + text + " labeled by: " + label + " not present");
288 }
289 }
290
291 protected void assertResultCount(String count) throws InterruptedException {
292 jiraAwareWaitFor(By.cssSelector("li.uif-infoMessageItem"), "result count for " + this.getClass().toString());
293 assertTextPresent(count + " items retrieved, displaying all items.", "li.uif-infoMessageItem", this.getClass().toString());
294 }
295
296
297
298
299
300
301
302 protected void assertTextPresent(String text) {
303 assertTextPresent(text, this.getClass().toString());
304 }
305
306
307
308
309
310 protected void assertTextPresent(String text, String message) {
311 WebDriverUtils.jGrowl(getDriver(), "Assert Text Present", false, "Assert text '" + text + "' is present.");
312 String pageSource = getDriver().getPageSource();
313 if (!pageSource.contains(text)) {
314 jiraAwareFail(text + " not present " + message);
315 }
316 WebDriverUtils.highlightElement(getDriver(), By.xpath("//*[contains(text(), '" + text + "')]"));
317 }
318
319
320
321
322 protected void assertTextPresent(String text, String cssSelector, String message){
323 WebElement element = findElement(By.cssSelector(cssSelector));
324 if (!element.getText().contains(text)){
325 jiraAwareFail(text + " for " + cssSelector + " not present " + message);
326 }
327 }
328
329
330
331
332
333
334 protected void assertTextNotPresent(String text) {
335 assertTextNotPresent(text, this.getClass().toString());
336 }
337
338
339
340
341
342
343 protected void assertTextNotPresent(String text, String message) {
344 String contents = getDriver().getPageSource();
345 if (contents.contains(text)) {
346 jiraAwareFail(text + " is present and should not be " + message);
347 }
348 }
349
350
351
352
353 protected void assertTextNotPresent(String text, String cssSelector, String message){
354 WebElement element = findElement(By.cssSelector(cssSelector));
355 if (element.getText().contains(text)){
356 jiraAwareFail(text + " for " + cssSelector + " is present and shouldn't be " + message);
357 }
358 }
359
360
361
362
363
364
365 protected void assertTrue(boolean booleanToAssertTrue) {
366 JiraAwareWebDriverUtils.assertTrue(booleanToAssertTrue, this);
367 }
368
369
370
371
372
373
374
375 protected void assertTrue(String message, boolean booleanToAssertTrue) {
376 JiraAwareWebDriverUtils.assertTrue(message, booleanToAssertTrue, this);
377 }
378
379
380
381
382
383
384
385
386 @Override
387 public void fail(String message) {
388 passed = false;
389 WebDriverUtils.jGrowl(getDriver(), "Failure " + getClass().getSimpleName(), true, message);
390 Assert.fail(message);
391 }
392
393 protected WebElement findDataTableRow(String keyText) throws InterruptedException {
394 return findDataTableRow(keyText, "dataTable");
395 }
396
397 protected WebElement findDataTableRow(String keyText, String className) throws InterruptedException {
398 jiraAwareWaitFor(By.className(className));
399 WebElement element = findElement(By.className(className));
400 return findElement(By.xpath("./*/tr//*[contains(text(), '" + keyText + "')]/ancestor::tr"), element);
401 }
402
403
404
405
406
407
408
409 protected WebElement findElement(By by) {
410 try {
411 return WebDriverUtils.findElement(getDriver(), by);
412 } catch (Exception e) {
413 jiraAwareFail(by.toString(), e.getMessage(), e);
414 }
415 return null;
416 }
417
418 protected WebElement findElement(By by, WebElement elementToFindOn) {
419 try {
420 WebElement found = elementToFindOn.findElement(by);
421 WebDriverUtils.highlightElement(getDriver(), found);
422 return found;
423 } catch (Exception e) {
424 jiraAwareFail(by.toString(), e.getMessage() + " " + this.getClass().toString(), e);
425 }
426 return null;
427 }
428
429 protected boolean isLabeledTextPresent(String label, String text) {
430 WebElement element = findElement(By.xpath("//tr/th/*/label[contains(text(), '" + label + "')]/ancestor::tr/td"));
431 String labeledText = element.getText().trim();
432 WebDriverUtils.jGrowl(getDriver(), "Is Labeled Text Present", false, "Is text '" + text + "' present for label '" + label + "'? " + labeledText.contains(text));
433 return labeledText.contains(text);
434 }
435
436 protected boolean isVisible(String locator) {
437 return isVisible(By.cssSelector(locator));
438 }
439
440 protected boolean isVisible(By by) {
441 List<WebElement> elements = getDriver().findElements(by);
442 for (WebElement element: elements) {
443 if (element.isDisplayed()) {
444 return true;
445 }
446 }
447 return false;
448 }
449
450 protected boolean isVisibleById(String id) {
451 return isVisible(By.id(id));
452 }
453
454 protected boolean isVisibleByXpath(String locator) {
455 return isVisible(By.xpath(locator));
456 }
457
458
459
460
461
462
463
464 @Override
465 public void jiraAwareFail(String message) {
466 checkForIncidentReport("", message);
467 JiraAwareFailureUtils.fail(message, this);
468 }
469
470
471
472
473
474
475
476
477 @Override
478 public void jiraAwareFail(String contents, String message) {
479 checkForIncidentReport(contents, message);
480 JiraAwareFailureUtils.fail(contents, message, this);
481 }
482
483
484
485
486
487
488
489
490 protected void jiraAwareFail(By by, String message, Throwable throwable) {
491 jiraAwareFail(by.toString(), message, throwable);
492 }
493
494
495
496
497
498
499
500
501
502 @Override
503 public void jiraAwareFail(String contents, String message, Throwable throwable) {
504 checkForIncidentReport(contents, message);
505 JiraAwareFailureUtils.fail(contents, message, throwable, this);
506 }
507
508
509
510
511
512
513
514
515
516 protected void jiraAwareFail(String contents, String message, Throwable throwable, JiraAwareFailable failable) {
517 checkForIncidentReport(contents, message);
518 JiraAwareFailureUtils.fail(contents, message, throwable, failable);
519 }
520
521
522
523
524
525
526
527
528 protected void jiraAwareWaitAndClick(By by, String message) throws InterruptedException {
529 jiraAwareWaitAndClick(by, message, this);
530 }
531
532
533
534
535
536
537
538
539
540 protected void jiraAwareWaitAndClick(By by, String message, JiraAwareFailable failable) throws InterruptedException {
541 try {
542 jiraAwareWaitFor(by, message, failable);
543 WebElement element = findElement(by);
544
545
546
547
548
549 element.click();
550 } catch (Exception e) {
551 failable.jiraAwareFail(by.toString(), message, e);
552 }
553 }
554
555
556
557
558
559
560
561
562 protected WebElement jiraAwareWaitFor(By by) throws InterruptedException {
563 return jiraAwareWaitFor(by, this.getClass().toString());
564 }
565
566
567
568
569
570
571
572
573
574 protected WebElement jiraAwareWaitFor(By by, String message) throws InterruptedException {
575 try {
576 return WebDriverUtils.waitFor(getDriver(), WebDriverUtils.configuredImplicityWait(), by, message);
577 } catch (Throwable t) {
578 jiraAwareFail(by, message + " " + this.getClass().toString(), t);
579 }
580 return null;
581 }
582
583
584
585
586
587
588
589
590 protected void jiraAwareWaitFors(By by, String message) throws InterruptedException {
591 try {
592 WebDriverUtils.waitFors(getDriver(), WebDriverUtils.configuredImplicityWait(), by, message);
593 } catch (Throwable t) {
594 jiraAwareFail(by, message, t);
595 }
596 }
597
598
599
600
601
602
603
604
605
606 protected void jiraAwareWaitFor(By by, String message, JiraAwareFailable failable) throws InterruptedException {
607 try {
608 WebDriverUtils.waitFor(getDriver(), WebDriverUtils.configuredImplicityWait(), by, message);
609 } catch (Throwable t) {
610 jiraAwareFail(by.toString(), message, t, failable);
611 }
612 }
613
614
615
616
617
618
619
620
621
622
623 protected WebElement jiraAwareWaitFor(By by, int seconds, String message) throws InterruptedException {
624 try {
625 return WebDriverUtils.waitFor(getDriver(), seconds, by, message);
626 } catch (Throwable t) {
627 jiraAwareFail(by, message, t);
628 }
629 return null;
630 }
631
632
633
634
635 public boolean isPassed() {
636 return passed;
637 }
638
639 protected void selectOptionByName(String name, String optionValue) throws InterruptedException {
640 selectOption(By.name(name), optionValue);
641 }
642
643 protected void selectOptionByXpath(String locator, String optionValue) throws InterruptedException {
644 selectOption(By.name(locator), optionValue);
645 }
646
647
648
649
650
651
652
653 protected void selectOption(By by, String optionValue) throws InterruptedException {
654 WebElement select1 = findElement(by);
655 List<WebElement> options = select1.findElements(By.tagName("option"));
656
657 String name = select1.getAttribute("name");
658
659 if (options == null || options.size() == 0) {
660 jiraAwareFail("No options for select "
661 + select1.toString()
662 + " was looking for value "
663 + optionValue
664 + " using "
665 + by.toString());
666 }
667
668 for (WebElement option : options) {
669 if (option.getAttribute("value").equals(optionValue)) {
670 WebDriverUtils.jGrowl(getDriver(), "Select " + option.getText(), false, "Select " + option.getText() + " from " + name);
671 option.click();
672 break;
673 }
674 }
675 }
676
677
678
679
680
681
682
683 protected void passed() {
684 passed = true;
685 WebDriverUtils.jGrowl(getDriver(), "Success " + getClass().getSimpleName(), true, "Passed");
686 }
687
688 protected WebElement waitAndType(By by, String text, String message) throws InterruptedException {
689 try {
690 jiraAwareWaitFor(by, message);
691 WebElement element = findElement(by);
692 String name = element.getAttribute("name");
693 WebDriverUtils.jGrowl(getDriver(), "Type", false, "Type into " + name + " the text: " + text);
694 WebDriverUtils.highlightElement(getDriver(), element);
695 element.sendKeys(text);
696 return element;
697 } catch (Exception e) {
698 JiraAwareFailureUtils.failOnMatchedJira(by.toString(), message, this);
699 jiraAwareFail(e.getMessage()
700 + " "
701 + by.toString()
702 + " unable to type text '"
703 + text
704 + "' "
705 + message
706 + " current url "
707 + getDriver().getCurrentUrl()
708 + "\n"
709 + AutomatedFunctionalTestUtils.deLinespace(getDriver().getPageSource()));
710 }
711 return null;
712 }
713 }