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