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