1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package edu.samplu.common;
17
18 import com.thoughtworks.selenium.Selenium;
19 import org.apache.commons.lang.RandomStringUtils;
20 import org.junit.Assert;
21 import org.openqa.selenium.By;
22 import org.openqa.selenium.WebDriver;
23 import org.openqa.selenium.chrome.ChromeDriver;
24 import org.openqa.selenium.firefox.FirefoxDriver;
25 import org.openqa.selenium.firefox.FirefoxProfile;
26 import org.openqa.selenium.remote.DesiredCapabilities;
27 import org.openqa.selenium.remote.RemoteWebDriver;
28
29 import java.io.PrintWriter;
30 import java.io.StringWriter;
31 import java.net.MalformedURLException;
32 import java.net.URL;
33 import java.net.URLEncoder;
34 import java.util.Calendar;
35 import java.util.HashMap;
36 import java.util.Iterator;
37 import java.util.Map;
38
39 import static com.thoughtworks.selenium.SeleneseTestBase.fail;
40 import static org.junit.Assert.assertEquals;
41
42
43
44
45
46
47 public class ITUtil {
48
49 public static final String KUALI_PORTAL_TITLE = "Kuali Portal Index";
50 public static final String DEFAULT_BASE_URL = "http://localhost:8080/kr-dev";
51 public final static String PORTAL = "/portal.do";
52 public final static String PORTAL_URL = ITUtil.getBaseUrlString() + ITUtil.PORTAL;
53 public final static String PORTAL_URL_ENCODED = URLEncoder.encode(PORTAL_URL);
54 public static final String DTS = Calendar.getInstance().getTimeInMillis() + "";
55 public static final String DTS_TWO = Calendar.getInstance().getTimeInMillis() + "" + RandomStringUtils.randomAlphabetic(2).toLowerCase();
56 public static String WAIT_TO_END_TEST = "5000";
57 public static final String DIV_ERROR_LOCATOR = "//div[@class='error']";
58 public static final String DIV_EXCOL_LOCATOR = "//div[@class='msg-excol']";
59 public static final int WAIT_DEFAULT_SECONDS = 60;
60 public static final String DEFAULT_WAIT_FOR_PAGE_TO_LOAD_TIMEOUT = "30000";
61 static Map<String, String> jiraMatches;
62 public static final String REMOTE_PUBLIC_URL_PROPERTY = "remote.public.url";
63 public static final String REMOTE_AUTOLOGIN_PROPERTY = "remote.autologin";
64 public static final String HUB_PROPERTY = "remote.public.hub";
65 public static final String HUB_DRIVER_PROPERTY = "remote.public.driver";
66 public static final String HUB_URL_PROPERTY = "http://localhost:4444/wd/hub";
67 public static final String DONT_TEAR_DOWN_PROPERTY = "remote.driver.dontTearDown";
68
69 static {
70 jiraMatches = new HashMap<String, String>();
71 jiraMatches.put("Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'refreshWhenChanged' of bean class [org.kuali.rice.krad.uif.element.Action]: Bean property 'refreshWhenChanged' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?",
72 "KULRICE-8137 Agenda Rule edit Incident report Invalid property 'refreshWhenChanged'");
73
74 jiraMatches.put("org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase.processAddCollectionLineBusinessRules(MaintenanceDocumentRuleBase.",
75 "KULRICE-8142 NPE in MaintenanceDocumentRuleBase.processAddCollectionLineBusinessRules");
76
77 jiraMatches.put("at org.kuali.rice.krad.rules.DocumentRuleBase.isDocumentOverviewValid(DocumentRuleBase.",
78 "KULRICE-8134 NPE in DocumentRuleBase.isDocumentOverviewValid(DocumentRuleBase");
79
80 jiraMatches.put("org.kuali.rice.krad.uif.layout.TableLayoutManager.buildLine(TableLayoutManager.",
81 "KULRICE-8160 NPE at TableLayoutManager.buildLine(TableLayoutManager");
82
83 jiraMatches.put("Bean property 'configFileLocations' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?",
84 "KULRICE-8173 Bean property 'configFileLocations' is not writable or has an invalid setter method");
85
86 jiraMatches.put("Bean property 'componentSecurity' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?",
87 "KULRICE-8182 JDK7 Bean property 'componentSecurity' is not readable...");
88
89 jiraMatches.put("java.sql.SQLSyntaxErrorException: ORA-00904: \"ROUTEHEADERID\": invalid identifier",
90 "KULRICE-8277 Several ITs fail with OJB operation; bad SQL grammar []; nested exception is java.sql.SQLException: ORA-00904: \"ROUTEHEADERID\": invalid identifier");
91
92
93
94 }
95
96
97
98
99
100
101 public static void assertDocFinal(Selenium selenium, String docId) {
102 docId= "link=" + docId;
103 if(selenium.isElementPresent(docId)){
104 assertEquals("FINAL", selenium.getText("//table[@id='row']/tbody/tr[1]/td[4]"));
105 }else{
106 assertEquals(docId, selenium.getText("//table[@id='row']/tbody/tr[1]/td[1]"));
107 assertEquals("FINAL", selenium.getText("//table[@id='row']/tbody/tr[1]/td[4]"));
108 }
109 }
110
111 protected static String blanketApprovalCleanUpErrorText(String errorText) {
112 errorText = errorText.replace("* required field", "").replace("\n", " ").trim();
113 return errorText;
114 }
115
116
117
118
119
120
121 public static void blanketApprove(Selenium selenium) throws InterruptedException {
122 ITUtil.checkForIncidentReport(selenium, "methodToCall.blanketApprove");
123 ITUtil.waitAndClick(selenium, "methodToCall.blanketApprove");
124 selenium.waitForPageToLoad(DEFAULT_WAIT_FOR_PAGE_TO_LOAD_TIMEOUT);
125 Thread.sleep(2000);
126
127 if (selenium.isElementPresent(DIV_ERROR_LOCATOR)) {
128 String errorText = selenium.getText(DIV_ERROR_LOCATOR);
129 if (errorText != null && errorText.contains("error(s) found on page.")) {
130 errorText = blanketApprovalCleanUpErrorText(errorText);
131 if (selenium.isElementPresent(DIV_EXCOL_LOCATOR)) {
132 errorText = blanketApprovalCleanUpErrorText(selenium.getText(DIV_EXCOL_LOCATOR));
133 }
134 if (selenium.isElementPresent("//div[@class='left-errmsg-tab']/div/div")) {
135 errorText = errorText + blanketApprovalCleanUpErrorText(selenium.getText("//div[@class='left-errmsg-tab']/div/div"));
136 }
137
138
139
140
141 Assert.fail(errorText);
142 }
143 }
144 ITUtil.checkForIncidentReport(selenium, "//img[@alt='doc search']");
145 waitAndClick(selenium, "//img[@alt='doc search']");
146 selenium.waitForPageToLoad(DEFAULT_WAIT_FOR_PAGE_TO_LOAD_TIMEOUT);
147 assertEquals("Kuali Portal Index", selenium.getTitle());
148 selenium.selectFrame("iframeportlet");
149 selenium.click("//input[@name='methodToCall.search' and @value='search']");
150 selenium.waitForPageToLoad(DEFAULT_WAIT_FOR_PAGE_TO_LOAD_TIMEOUT);
151 }
152
153
154
155
156
157
158 public static void checkErrorMessageItem(Selenium selenium, String message) {
159 final String error_locator = "//li[@class='uif-errorMessageItem']";
160 if (selenium.isElementPresent(error_locator)) {
161 String errorText = selenium.getText(error_locator);
162 if (errorText != null && errorText.contains("errors")) {
163 Assert.fail(errorText + message);
164 }
165 }
166 }
167
168
169
170
171
172
173
174 public static String getBaseUrlString() {
175 String baseUrl = System.getProperty(REMOTE_PUBLIC_URL_PROPERTY);
176 if (baseUrl == null) {
177 baseUrl = DEFAULT_BASE_URL;
178 }
179 baseUrl = prettyHttp(baseUrl);
180 return baseUrl;
181 }
182
183
184
185
186
187
188 public static String prettyHttp(String baseUrl) {
189 if (baseUrl.endsWith("/")) {
190 baseUrl = baseUrl.substring(0, baseUrl.length() - 1);
191 }
192 if (!baseUrl.startsWith("http")) {
193 baseUrl = "http://" + baseUrl;
194 }
195 return baseUrl;
196 }
197
198
199
200
201
202
203 public static String getHubUrlString() {
204 String hubUrl = System.getProperty(HUB_PROPERTY);
205 if (hubUrl == null) {
206 hubUrl = HUB_URL_PROPERTY;
207 }
208 hubUrl = prettyHttp(hubUrl);
209 if (!hubUrl.endsWith("/wd/hub")) {
210 hubUrl = hubUrl + "/wd/hub";
211 }
212 return hubUrl;
213 }
214
215
216
217
218
219
220 public static WebDriver getWebDriver() {
221 String driverParam = System.getProperty(HUB_DRIVER_PROPERTY);
222 String hubParam = System.getProperty(HUB_PROPERTY);
223 if (hubParam == null) {
224 if (driverParam == null || "firefox".equalsIgnoreCase(driverParam)) {
225 FirefoxProfile profile = new FirefoxProfile();
226 profile.setEnableNativeEvents(false);
227 return new FirefoxDriver(profile);
228 } else if ("chrome".equalsIgnoreCase(driverParam)) {
229 return new ChromeDriver();
230 }
231 } else {
232 try {
233 if (driverParam == null || "firefox".equalsIgnoreCase(driverParam)) {
234 return new RemoteWebDriver(new URL(ITUtil.getHubUrlString()), DesiredCapabilities.firefox());
235 } else if ("chrome".equalsIgnoreCase(driverParam)) {
236 return new RemoteWebDriver(new URL(ITUtil.getHubUrlString()), DesiredCapabilities.chrome());
237 }
238 } catch (MalformedURLException mue) {
239 System.out.println(ITUtil.getHubUrlString() + " " + mue.getMessage());
240 mue.printStackTrace();
241 }
242 }
243 return null;
244 }
245
246
247
248
249
250 public static void loginSe(Selenium selenium) {
251 loginSe(selenium, "admin");
252 }
253
254
255
256
257
258
259
260 public static void login(WebDriver driver, String userName) throws InterruptedException {
261 if (System.getProperty(REMOTE_AUTOLOGIN_PROPERTY) == null) {
262 driver.findElement(By.name("__login_user")).clear();
263 driver.findElement(By.name("__login_user")).sendKeys(userName);
264 driver.findElement(By.cssSelector("input[type=\"submit\"]")).click();
265 Thread.sleep(1000);
266 String contents = driver.getPageSource();
267 checkForInvalidUserName(userName, contents);
268 }
269 }
270
271 private static void checkForInvalidUserName(String userName, String contents) {
272 if (contents.indexOf("Invalid username") > -1) {
273 Assert.fail("Invalid username " + userName);
274 }
275 }
276
277
278
279
280
281 public static void loginSe(Selenium selenium, String user) {
282 if (System.getProperty(REMOTE_AUTOLOGIN_PROPERTY) == null) {
283 try {
284 selenium.waitForPageToLoad(DEFAULT_WAIT_FOR_PAGE_TO_LOAD_TIMEOUT);
285 } catch (Exception e) {
286 Assert.fail("Login page not loaded app started?");
287 }
288 if (!"Login".equals(selenium.getTitle())) {
289 fail("Title is not Login as expected, but " + selenium.getTitle());
290 }
291 selenium.type("__login_user", user);
292 selenium.click("//input[@type='submit']"); //using css selector fails
293 selenium.waitForPageToLoad(DEFAULT_WAIT_FOR_PAGE_TO_LOAD_TIMEOUT);
294 String contents = selenium.getHtmlSource();
295 checkForInvalidUserName(user, contents);
296 }
297 }
298
299
300
301
302
303
304 public static String stackTrace(Throwable throwable) {
305 StringWriter wrt=new StringWriter();
306 PrintWriter pw=new PrintWriter(wrt);
307 throwable.printStackTrace(pw);
308 pw.flush();
309 return wrt.toString();
310 }
311
312
313
314
315
316
317
318 public static boolean dontTearDownPropertyNotSet() {
319 return System.getProperty(DONT_TEAR_DOWN_PROPERTY) == null ||
320 "f".startsWith(System.getProperty(DONT_TEAR_DOWN_PROPERTY).toLowerCase()) ||
321 "n".startsWith(System.getProperty(DONT_TEAR_DOWN_PROPERTY).toLowerCase());
322 }
323
324
325
326
327
328
329
330 public static void waitAndClick(Selenium selenium, String elementLocator) throws InterruptedException {
331 waitAndClick(selenium, elementLocator, WAIT_DEFAULT_SECONDS);
332 }
333
334
335
336
337
338
339
340
341 public static void waitAndClick(Selenium selenium, String elementLocator, String message) throws InterruptedException {
342 waitAndClick(selenium, elementLocator, WAIT_DEFAULT_SECONDS, message);
343 }
344
345
346
347
348
349
350
351
352 public static void waitAndClick(Selenium selenium, String elementLocator, int seconds) throws InterruptedException {
353 waitAndClick(selenium, elementLocator, seconds, "");
354 }
355
356
357
358
359
360
361
362
363
364 public static void waitAndClick(Selenium selenium, String elementLocator, int seconds, String message) throws InterruptedException {
365 waitForElement(selenium, elementLocator, seconds, message);
366 selenium.click(elementLocator);
367 Thread.sleep(1000);
368 ITUtil.checkForIncidentReport(selenium, elementLocator, message);
369 }
370
371
372
373
374
375
376
377
378 public static void waitAndType(Selenium selenium, String elementLocator, String text) throws InterruptedException {
379 waitAndType(selenium, elementLocator, text, "");
380 }
381
382
383
384
385
386
387
388
389
390 public static void waitAndType(Selenium selenium, String elementLocator, String text, String message) throws InterruptedException {
391 waitAndType(selenium, elementLocator, WAIT_DEFAULT_SECONDS, text, message);
392 }
393
394
395
396
397
398
399
400
401
402
403 public static void waitAndType(Selenium selenium, String elementLocator, int seconds, String text, String message) throws InterruptedException {
404 waitForElement(selenium, elementLocator, seconds, message);
405 selenium.type(elementLocator, text);
406 Thread.sleep(1000);
407 }
408
409
410
411
412
413
414
415 public static void waitForElement(Selenium selenium, String elementLocator) throws InterruptedException {
416 waitForElement(selenium, elementLocator, WAIT_DEFAULT_SECONDS);
417 }
418
419
420
421
422
423
424
425
426 public static void waitForElement(Selenium selenium, String elementLocator, String message) throws InterruptedException {
427 waitForElement(selenium, elementLocator, WAIT_DEFAULT_SECONDS, message);
428 }
429
430
431
432
433
434
435
436
437 public static void waitForElement(Selenium selenium, String elementLocator, int seconds) throws InterruptedException {
438 waitForElement(selenium, elementLocator, WAIT_DEFAULT_SECONDS, "");
439 }
440
441
442
443
444
445
446
447
448
449 public static void waitForElement(Selenium selenium, String elementLocator, int seconds, String message) throws InterruptedException {
450 boolean failed = false;
451 for (int second = 0;; second++) {
452 if (second >= seconds) failed = true;
453 try { if (failed || selenium.isElementPresent(elementLocator)) break; } catch (Exception e) {}
454 Thread.sleep(1000);
455 }
456 ITUtil.checkForIncidentReport(selenium, elementLocator);
457 if (failed) fail("timeout of " + seconds + " seconds waiting for " + elementLocator + " " + message);
458 }
459
460
461
462
463
464
465
466 public static void waitForElementVisible(Selenium selenium, String elementLocator) throws InterruptedException {
467 waitForElementVisible(selenium, elementLocator, WAIT_DEFAULT_SECONDS, "");
468 }
469
470
471
472
473
474
475
476
477 public static void waitForElementVisible(Selenium selenium, String elementLocator, String message) throws InterruptedException {
478 waitForElementVisible(selenium, elementLocator, WAIT_DEFAULT_SECONDS, message);
479 }
480
481
482
483
484
485
486
487
488 public static void waitForElementVisible(Selenium selenium, String elementLocator, int seconds, String message) throws InterruptedException {
489 for (int second = 0;; second++) {
490 if (second >= seconds) fail("timeout of " + seconds + " seconds waiting for " + elementLocator + " " + message);
491 try { if (selenium.isVisible(elementLocator)) break; } catch (Exception e) {}
492 Thread.sleep(1000);
493 }
494 }
495
496
497
498
499
500
501
502 public static void waitForTitleToEqual(Selenium selenium, String title) throws InterruptedException {
503 waitForTitleToEqual(selenium, title, "");
504 }
505
506
507
508
509
510
511
512
513 public static void waitForTitleToEqual(Selenium selenium, String title, String message) throws InterruptedException {
514 Thread.sleep(2000);
515
516
517
518
519
520 }
521
522
523
524
525
526
527 public static void checkForIncidentReport(Selenium selenium, String linkLocator) {
528 checkForIncidentReport(selenium, linkLocator, "");
529 }
530
531
532
533
534
535
536 public static void checkForIncidentReport(Selenium selenium, String linkLocator, String message) {
537 selenium.waitForPageToLoad(DEFAULT_WAIT_FOR_PAGE_TO_LOAD_TIMEOUT);
538 String contents = selenium.getHtmlSource();
539 checkForIncidentReport(contents, linkLocator, message);
540 }
541
542 protected static void checkForIncidentReport(String contents, String linkLocator, String message) {
543 if (contents != null &&
544 contents.contains("Incident Report") &&
545 !contents.contains("portal.do?channelTitle=Incident%20Report&") &&
546 !contents.contains("portal.do?channelTitle=Incident Report&") &&
547 !contents.contains("SeleniumException")) {
548 try {
549 if (contents.indexOf("Incident Feedback") > -1) {
550 Iterator<String> iter = jiraMatches.keySet().iterator();
551 String key = null;
552 while (iter.hasNext()) {
553 key = iter.next();
554 if (contents.contains(key)) {
555 Assert.fail("https://jira.kuali.org/browse/" + jiraMatches.get(key));
556 }
557 }
558
559 String chunk = contents.substring(contents.indexOf("Incident Feedback"), contents.lastIndexOf("</div>") );
560 String docId = chunk.substring(chunk.lastIndexOf("Document Id"), chunk.indexOf("View Id"));
561 docId = docId.substring(0, docId.indexOf("</span>"));
562 docId = docId.substring(docId.lastIndexOf(">") + 2, docId.length());
563
564 String viewId = chunk.substring(chunk.lastIndexOf("View Id"), chunk.indexOf("Error Message"));
565 viewId = viewId.substring(0, viewId.indexOf("</span>"));
566 viewId = viewId.substring(viewId.lastIndexOf(">") + 2, viewId.length());
567
568 String stackTrace = chunk.substring(chunk.lastIndexOf("(only in dev mode)"), chunk.length());
569 stackTrace = stackTrace.substring(stackTrace.indexOf("<span id=\"") + 3, stackTrace.length());
570 stackTrace = stackTrace.substring(stackTrace.indexOf("\">") + 2, stackTrace.indexOf("</span>"));
571
572
573
574
575 Assert.fail("\nIncident report " + message + " navigating to "
576 + linkLocator
577 + " : View Id: "
578 + viewId.trim()
579 + " Doc Id: "
580 + docId.trim()
581 + "\nStackTrace: "
582 + stackTrace.trim());
583 } else {
584 Assert.fail("\nIncident report detected " + message + "\nContents that triggered exception: " + deLinespace(contents));
585 }
586 } catch (IndexOutOfBoundsException e) {
587 Assert.fail("\nIncident report detected " + message + " but there was an exception during processing: " + e.getMessage() + "\nStack Trace from processing exception" + stackTrace(e) + "\nContents that triggered exception: " + deLinespace(
588 contents));
589 }
590 } else {
591 if (contents.contains("HTTP Status 404")) {
592 Assert.fail("\nHTTP Status 404 " + linkLocator + " " + message + " " + "\ncontents:" + contents);
593 }
594 }
595 }
596
597 private static String deLinespace(String contents) {
598 while (contents.contains("\n\n")) {
599 contents = contents.replace("\n\n", "\n");
600 }
601 return contents;
602 }
603 }