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