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