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 com.thoughtworks.selenium.SeleneseTestBase;
19 import org.junit.After;
20 import org.junit.AfterClass;
21 import org.junit.Before;
22 import org.junit.BeforeClass;
23 import org.kuali.rice.testtools.common.JiraAwareFailable;
24 import org.openqa.selenium.Alert;
25 import org.openqa.selenium.By;
26 import org.openqa.selenium.JavascriptExecutor;
27 import org.openqa.selenium.WebDriver;
28 import org.openqa.selenium.chrome.ChromeDriverService;
29
30 import java.util.ArrayList;
31 import java.util.List;
32 import java.util.Set;
33 import java.util.concurrent.TimeUnit;
34
35
36
37
38
39 public abstract class WebDriverITBase {
40
41 public WebDriver driver;
42 static ChromeDriverService chromeDriverService;
43
44
45
46
47
48
49 public abstract String getTestUrl();
50
51
52
53
54
55 public String getUserName() {
56 return "admin";
57 }
58
59 @BeforeClass
60 public static void createAndStartService() throws Exception {
61 chromeDriverService = WebDriverUtils.chromeDriverCreateCheck();
62 if (chromeDriverService != null) chromeDriverService.start();
63 }
64
65
66
67
68
69
70
71
72
73
74
75
76 public void login(WebDriver driver, String userName, JiraAwareFailable failable) throws InterruptedException {
77 if ("true".equalsIgnoreCase(System.getProperty(WebDriverUtils.REMOTE_AUTOLOGIN_PROPERTY, "true"))) {
78 driver.findElement(By.name("login_user")).clear();
79 driver.findElement(By.name("login_user")).sendKeys(userName);
80 driver.findElement(By.id("Rice-LoginButton")).click();
81 Thread.sleep(1000);
82 String contents = driver.getPageSource();
83 AutomatedFunctionalTestUtils.failOnInvalidUserName(userName, contents, failable);
84 AutomatedFunctionalTestUtils.checkForIncidentReport(driver.getPageSource(), "Login",
85 "Login failure", failable);
86 }
87 }
88
89 public void fail(String message) {
90 SeleneseTestBase.fail(message);
91 }
92
93
94
95
96
97
98 @Before
99 public void setUp() throws Exception {
100 driver = WebDriverUtils.setUp(this.getClass().getSimpleName(), "DeprecatedWebDriverItBaseTest");
101
102 WebDriverUtils.openTestUrl(driver, WebDriverUtils.getBaseUrlString() + "/" + getTestUrl());
103
104 login(driver, getUserName(), new JiraAwareFailable() {
105 @Override
106 public void fail(String message) {
107 SeleneseTestBase.fail(message);
108 }
109
110 @Override
111 public void jiraAwareFail(String message) {
112 SeleneseTestBase.fail(message);
113 }
114
115 @Override
116 public void jiraAwareFail(String contents, String message) {
117 SeleneseTestBase.fail(contents + " " + message);
118 }
119
120 @Override
121 public void jiraAwareFail(String contents, String message, Throwable throwable) {
122 SeleneseTestBase.fail(contents + " " + message + " " + throwable.getMessage());
123 }
124 });
125 }
126
127
128
129
130
131
132 @After
133 public void tearDown() throws Exception {
134 if (WebDriverUtils.dontTearDownPropertyNotSet()) {
135 if (driver != null) {
136 driver.quit();
137 } else {
138 System.out.println("WebDriver was null in WebDriverUtils.tearDown()");
139 }
140 }
141 }
142
143
144
145
146
147
148 @AfterClass
149 public static void stopService() throws Exception {
150 if (chromeDriverService != null) {
151 chromeDriverService.stop();
152 }
153 }
154
155
156
157
158
159
160
161
162
163
164
165 public boolean isElementPresent(By by) {
166 if (driver.findElements(by).isEmpty()) {
167 return false;
168 } else {
169 return true;
170 }
171 }
172
173
174
175
176
177
178
179
180
181
182
183
184 public boolean isElementPresentQuick(By by) {
185 driver.manage().timeouts().implicitlyWait(WebDriverUtils.IMPLICIT_WAIT_TIME_LOOP_MS, TimeUnit.MILLISECONDS);
186 boolean result = isElementPresent(by);
187 driver.manage().timeouts().implicitlyWait(WebDriverUtils.IMPLICIT_WAIT_TIME_SECONDS_DEFAULT, TimeUnit.SECONDS);
188 return result;
189 }
190
191
192
193
194
195
196
197
198 public void assertPopUpWindowUrl(By by, String windowName, String url) {
199 driver.findElement(by).click();
200 String parentWindowHandle = driver.getWindowHandle();
201
202 driver.switchTo().window(windowName).findElements(By.tagName("head"));
203 SeleneseTestBase.assertEquals(url, driver.getCurrentUrl());
204 driver.switchTo().window(parentWindowHandle);
205 }
206
207
208
209
210
211
212 protected void waitFor(By by) throws InterruptedException {
213 waitFor(by, "");
214 }
215
216
217
218
219
220
221
222 protected void waitFor(By by, String message) throws InterruptedException {
223 Thread.sleep(1000);
224 try {
225 driver.findElement(by);
226 } catch (Exception e) {
227
228 }
229 }
230
231
232
233
234
235
236
237 protected void waitAndType(By by, String text) throws InterruptedException {
238 waitFor(by, "");
239 try {
240 (driver.findElement(by)).sendKeys(text);
241 } catch (Exception e) {
242 fail(e.getMessage() + " " + by.toString() + " " + text);
243 e.printStackTrace();
244 }
245 }
246
247
248
249
250
251
252
253
254 protected void waitAndType(By by, String text, String message) throws InterruptedException {
255 waitFor(by, "");
256 try {
257 (driver.findElement(by)).sendKeys(text);
258 } catch (Exception e) {
259 fail(e.getMessage() + " " + by.toString() + " " + text + " "+message);
260 e.printStackTrace();
261 }
262 }
263
264
265
266
267
268
269
270 protected void waitAndTypeByXpath(String locator, String text) throws InterruptedException {
271 waitAndType(By.xpath(locator), text);
272 }
273
274
275
276
277
278
279
280
281 protected void waitAndTypeByXpath(String locator, String text, String message) throws InterruptedException {
282 waitAndType(By.xpath(locator), text, message);
283 }
284
285
286
287
288
289
290
291 protected void waitAndTypeByName(String name, String text) throws InterruptedException {
292 waitAndType(By.name(name), text);
293 }
294
295
296
297
298
299
300 protected void clearTextByName(String name) throws InterruptedException {
301 clearText(By.name(name));
302 }
303
304
305
306
307
308
309 protected void clearTextByXpath(String locator) throws InterruptedException {
310 clearText(By.xpath(locator));
311 }
312
313
314
315
316
317
318 protected void clearText(By by) throws InterruptedException {
319 driver.findElement(by).clear();
320 }
321
322
323
324
325
326 protected void dismissAlert()
327 {
328 Alert alert = driver.switchTo().alert();
329
330 alert.dismiss();
331 }
332
333
334
335
336
337 protected void acceptAlert()
338 {
339 Alert alert = driver.switchTo().alert();
340
341 alert.accept();
342 }
343
344 protected String getEval(String script)
345 {
346 JavascriptExecutor js = (JavascriptExecutor) driver;
347 return (String)js.executeScript(script);
348 }
349
350
351
352
353
354 protected void switchWindow()
355 {
356 Set<String> winSet = driver.getWindowHandles();
357 List<String> winList = new ArrayList<String>(winSet);
358 String newTab = winList.get(winList.size() - 1);
359 driver.switchTo().window(newTab);
360 }
361
362
363
364
365
366
367
368 protected String getAttributeByName(String name,String attribute) throws InterruptedException {
369 return getAttribute(By.name(name),attribute);
370 }
371
372
373
374
375
376
377
378 protected String getAttributeByXpath(String locator,String attribute) throws InterruptedException {
379 return getAttribute(By.xpath(locator),attribute);
380 }
381
382
383
384
385
386
387
388 protected String getAttribute(By by,String attribute) throws InterruptedException {
389 return driver.findElement(by).getAttribute(attribute);
390 }
391
392
393
394
395
396
397 protected void waitAndClickByLinkText(String text) throws InterruptedException {
398 waitAndClick(By.linkText(text),"");
399 }
400
401
402
403
404
405
406
407 protected void waitAndClickByLinkText(String text, String message) throws InterruptedException {
408 waitAndClick(By.linkText(text), message);
409 }
410
411
412
413
414
415
416 protected void waitAndClick(By by) throws InterruptedException {
417 waitAndClick(by, "");
418 }
419
420
421
422
423
424
425
426 protected void waitAndClick(By by, String message) throws InterruptedException {
427 waitFor(by, message);
428 try {
429 (driver.findElement(by)).click();
430 } catch (Exception e) {
431 fail(e.getMessage() + " " + by.toString() + " " + message);
432 }
433 }
434
435
436
437
438
439
440 protected void waitAndClick(String locator) throws InterruptedException {
441 waitAndClick(locator, "");
442 }
443
444
445
446
447
448
449
450 protected void waitAndClick(String locator, String message) throws InterruptedException {
451 waitAndClick(By.cssSelector(locator), message);
452 }
453
454
455
456
457
458
459 protected void waitForElementPresent(String locator) throws InterruptedException {
460 waitFor(By.cssSelector(locator));
461 }
462
463
464
465
466
467
468 protected void waitForElementPresentByXpath(String locator) throws InterruptedException {
469 waitFor(By.xpath(locator));
470 }
471
472
473
474
475
476
477 protected void waitForElementPresentByName(String name) throws InterruptedException {
478 waitFor(By.name(name));
479 }
480
481 protected void checkForIncidentReport(JiraAwareFailable failable) {
482 checkForIncidentReport("", failable, "");
483 }
484
485 protected void checkForIncidentReport(String locator, JiraAwareFailable failable) {
486 checkForIncidentReport(locator, failable, "");
487 }
488
489 protected void checkForIncidentReport(String locator, JiraAwareFailable failable, String message) {
490 AutomatedFunctionalTestUtils.checkForIncidentReport(driver.getPageSource(), locator, message, failable);
491 }
492
493
494 }
495