This repository has been archived by the owner on Jun 28, 2019. It is now read-only.
/
EbselenCommands.java
599 lines (517 loc) · 22.1 KB
/
EbselenCommands.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
/*
* Copyright (c) 2010-2011 Ardesco Solutions - http://www.ardescosolutions.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.lazerycode.ebselen;
import com.lazerycode.ebselen.commands.*;
import com.lazerycode.ebselen.exceptions.*;
import com.lazerycode.ebselen.handlers.LocatorHandler;
import com.thoughtworks.selenium.Selenium;
import org.openqa.selenium.*;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.internal.seleniumemulation.JavascriptLibrary;
import org.openqa.selenium.remote.Augmenter;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.awt.*;
import java.io.File;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
public class EbselenCommands {
private WebDriver driver;
private JavascriptLibrary jsLib = new JavascriptLibrary();
private LocatorHandler loc = new LocatorHandler();
private int defaultTimeout = 10000;
public static final Logger logger = LoggerFactory.getLogger(EbselenCommands.class);
public EbselenCommands(WebDriver driverInstance) {
driver = driverInstance;
}
public EbselenCommands() {
}
/**
* Set the default timeout used by the wait functions.
*
* @param timeout in ms (e.g. 10 seconds = 10000)
*/
public void setDefaultTimeout(int timeout) {
defaultTimeout = timeout;
}
/**
* Return the currently set default timeout in ms
*
* @param timeout
* @return
*/
public int getDefaultTimeout(int timeout) {
return defaultTimeout;
}
/**
* Open site homepage but do not check that it has loaded correctly
* This will set the currently selected site to the value of page
*
* @param homepageURL URL of the homepage
*/
public void openHomepage(String homepageURL) throws Exception {
openHomepage(homepageURL, null);
}
/**
* Open site homepage and check that it has loaded correctly.
* This will set the currently selected site to the value of page
*
* @param homepageURL URL of the homepage
* @param homePageElement If homePageElement is not null kill tests if it does not exist
*/
public void openHomepage(String homepageURL, WebElement homePageElement) throws Exception {
driver.get(homepageURL);
if (homePageElement != null) {
if (!homePageElement.isDisplayed()) {
throw new HomepageNotLoadedException();
}
}
}
public APISwitch apiswitch() {
return new EbselenAPISwitch();
}
private class EbselenAPISwitch implements APISwitch {
public Selenium switchToSelenium(String homepageURL) {
// Create the Selenium implementation
return new WebDriverBackedSelenium(driver, homepageURL);
}
public void switchBackToWebdriver(Selenium seleniumObject) {
driver = ((WebDriverBackedSelenium) seleniumObject).getWrappedDriver();
}
}
public com.lazerycode.ebselen.commands.Windows window() {
return new EbselenWindow();
}
private class EbselenWindow implements Windows {
public Boolean switchBetweenWindows() throws Exception {
Set listOfWindows = driver.getWindowHandles();
if (listOfWindows.size() != 2) {
if (listOfWindows.size() > 2) {
throw new TooManyWindowsException();
} else {
throw new TooFewWindowsException();
}
}
String currentWindow = driver.getWindowHandle();
for (Iterator i = listOfWindows.iterator(); i.hasNext(); ) {
String selectedWindowHandle = i.next().toString();
if (!selectedWindowHandle.equals(currentWindow)) {
driver.switchTo().window(selectedWindowHandle);
return true;
}
}
// Just in case something goes wrong
throw new UnableToFindWindowException("Unable to switch windows!");
}
public Boolean switchToWindowTitled(String windowTitle) throws Exception {
try {
driver.switchTo().window(windowTitle);
return true;
} catch (Exception Ex) {
throw new UnableToFindWindowException("Unable to find a window with the title '" + windowTitle + "'!");
}
}
public File takeScreenshot() {
WebDriver augment = new Augmenter().augment(driver);
return ((TakesScreenshot) augment).getScreenshotAs(OutputType.FILE);
}
public void maximise() {
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension screenResolution = new Dimension((int) toolkit.getScreenSize().getWidth(), (int) toolkit.getScreenSize().getHeight());
driver.manage().window().setSize(screenResolution);
}
}
public com.lazerycode.ebselen.commands.JavaScript javascript() {
return new JavaScriptInteraction();
}
private class JavaScriptInteraction implements JavaScript {
public void triggerJavascriptEvent(jsEvent event, WebElement element) {
jsLib.callEmbeddedSelenium(driver, "triggerEvent", element, event.toString().toLowerCase());
}
public Object executeScript(String script, Object... args) {
return ((JavascriptExecutor) driver).executeScript(script, args);
}
}
public com.lazerycode.ebselen.commands.Element element() {
return new ElementInteraction();
}
private class ElementInteraction implements Element {
//TODO check exception thrown by a stale WebElement, this is not really an isElementPresent, more isElementSTale
/**
* Find out if an elementLocator exists or not.
*
* @param element - An xpath locator
* @return boolean - True if elementLocator is found, otherwise false.
* @throws Exception
*/
public boolean isElementStale(WebElement element) {
try {
element.getLocation();
} catch (org.openqa.selenium.NoSuchElementException Ex) {
return false;
}
return true;
}
//TODO end
public boolean doesElementExist(By locator) {
if (driver.findElements(locator).size() > 0) {
return true;
} else {
return false;
}
}
public boolean isElementDisplayed(By locator) {
if (doesElementExist(locator)) {
return driver.findElement(locator).isDisplayed();
} else {
return false;
}
}
public int getElementCount(String locator) {
List elementsFound = driver.findElements(By.xpath(locator));
return elementsFound.size();
}
public void check(String locator) {
WebElement checkBox = driver.findElement(loc.autoLocator(locator));
if (!checkBox.getAttribute("type").toLowerCase().equals("checkbox")) {
throw new InvalidElementTypeException("This elementLocator is not a checkbox!");
}
if (!checkBox.isSelected()) {
checkBox.click();
}
}
public void uncheck(String locator) {
WebElement checkBox = driver.findElement(loc.autoLocator(locator));
if (!checkBox.getAttribute("type").toLowerCase().equals("checkbox")) {
throw new InvalidElementTypeException("This elementLocator is not a checkbox!");
}
if (checkBox.isSelected()) {
checkBox.click();
}
}
public boolean isChecked(String locator) {
WebElement checkBox = driver.findElement(loc.autoLocator(locator));
if (!checkBox.getAttribute("type").toLowerCase().equals("checkbox")) {
throw new InvalidElementTypeException("This elementLocator is not a checkbox!");
}
if (checkBox.getAttribute("checked").equals("checked")) {
return true;
} else {
return false;
}
}
}
public com.lazerycode.ebselen.commands.Utility utility() {
return new EbselenUtility();
}
private class EbselenUtility implements Utility {
public String generateRandomEmailAddress(String domain) {
String emailAddress = "";
// Generate random email address
String alphabet = "abcdefghijklmnopqrstuvwxyz";
while (emailAddress.length() < 5) {
int character = (int) (Math.random() * 26);
emailAddress += alphabet.substring(character, character + 1);
}
emailAddress += Integer.valueOf((int) (Math.random() * 99)).toString();
emailAddress += "@" + domain;
return emailAddress;
}
public String stripGarbage(String garbageFilledString) {
return garbageFilledString.replaceAll("/", "_").replaceAll("\\W", "");
}
public String ucWords(String wordsToConvert) {
wordsToConvert = wordsToConvert.toLowerCase();
Boolean ucChar = false;
String convertedString = "";
for (int i = 0; i < wordsToConvert.length(); i++) {
if (i == 0 | ucChar) {
convertedString += Character.toUpperCase(wordsToConvert.charAt(i));
} else {
convertedString += Character.toString(wordsToConvert.charAt(i));
}
if (Character.isWhitespace(wordsToConvert.charAt(i))) {
ucChar = true;
} else {
ucChar = false;
}
}
return convertedString;
}
}
public com.lazerycode.ebselen.commands.Wait waiting() {
return new EbselenWait(defaultTimeout);
}
public com.lazerycode.ebselen.commands.Wait waitFor(int timeout) {
return new EbselenWait(timeout);
}
private class EbselenWait implements Wait {
final int timeout;
public EbselenWait(int passedTimeout) {
timeout = passedTimeout;
}
public ForWebElements untilWebElement(By elementLocator) {
return new EbselenForWebElements(elementLocator);
}
private class EbselenForWebElements implements ForWebElements {
final By elementLocator;
public EbselenForWebElements(By passedElement) {
elementLocator = passedElement;
}
public void exists() {
new WebDriverWait(driver, timeout) {
}.until(new ExpectedCondition<Boolean>() {
@Override
public Boolean apply(WebDriver driver) {
return (driver.findElements(elementLocator).size() > 0);
}
});
}
public void doesNotExist() {
new WebDriverWait(driver, timeout) {
}.until(new ExpectedCondition<Boolean>() {
@Override
public Boolean apply(WebDriver driver) {
return (driver.findElements(elementLocator).size() < 1);
}
});
}
public void instancesAreMoreThan(final int instances) {
new WebDriverWait(driver, timeout) {
}.until(new ExpectedCondition<Boolean>() {
@Override
public Boolean apply(WebDriver driver) {
return (driver.findElements(elementLocator).size() > instances);
}
});
}
public void instancesAreLessThan(final int instances) {
new WebDriverWait(driver, timeout) {
}.until(new ExpectedCondition<Boolean>() {
@Override
public Boolean apply(WebDriver driver) {
return (driver.findElements(elementLocator).size() < instances);
}
});
}
public void instancesEqual(final int instances) {
new WebDriverWait(driver, timeout) {
}.until(new ExpectedCondition<Boolean>() {
@Override
public Boolean apply(WebDriver driver) {
return (driver.findElements(elementLocator).size() == instances);
}
});
}
public void instancesDoNotEqual(final int instances) {
new WebDriverWait(driver, timeout) {
}.until(new ExpectedCondition<Boolean>() {
@Override
public Boolean apply(WebDriver driver) {
return (driver.findElements(elementLocator).size() != instances);
}
});
}
public void existsAfterRefreshingPage() {
new WebDriverWait(driver, timeout) {
}.until(new ExpectedCondition<Boolean>() {
@Override
public Boolean apply(WebDriver driver) {
driver.navigate().refresh();
return (driver.findElements(elementLocator).size() > 0);
}
});
}
public void doesNotExistAfterRefreshingPage() {
new WebDriverWait(driver, timeout) {
}.until(new ExpectedCondition<Boolean>() {
@Override
public Boolean apply(WebDriver driver) {
driver.navigate().refresh();
return (driver.findElements(elementLocator).size() < 1);
}
});
}
public void instancesAreMoreThanAfterRefreshingPage(final int instances) {
new WebDriverWait(driver, timeout) {
}.until(new ExpectedCondition<Boolean>() {
@Override
public Boolean apply(WebDriver driver) {
driver.navigate().refresh();
return (driver.findElements(elementLocator).size() > instances);
}
});
}
public void instancesAreLessThanAfterRefreshingPage(final int instances) {
new WebDriverWait(driver, timeout) {
}.until(new ExpectedCondition<Boolean>() {
@Override
public Boolean apply(WebDriver driver) {
driver.navigate().refresh();
return (driver.findElements(elementLocator).size() < instances);
}
});
}
public void instancesEqualAfterRefreshingPage(final int instances) {
new WebDriverWait(driver, timeout) {
}.until(new ExpectedCondition<Boolean>() {
@Override
public Boolean apply(WebDriver driver) {
driver.navigate().refresh();
return driver.findElements(elementLocator).size() == instances;
}
});
}
public void instancesDoNotEqualAfterRefreshingPage(final int instances) {
new WebDriverWait(driver, timeout) {
}.until(new ExpectedCondition<Boolean>() {
@Override
public Boolean apply(WebDriver driver) {
driver.navigate().refresh();
return driver.findElements(elementLocator).size() != instances;
}
});
}
public void textIsEqualTo(final String text) {
new WebDriverWait(driver, timeout) {
}.until(new ExpectedCondition<Boolean>() {
@Override
public Boolean apply(WebDriver driver) {
return driver.findElement(elementLocator).getText().equals(text);
}
});
}
public void textDoesNotEqual(final String text) {
new WebDriverWait(driver, timeout) {
}.until(new ExpectedCondition<Boolean>() {
@Override
public Boolean apply(WebDriver driver) {
return !driver.findElement(elementLocator).getText().equals(text);
}
});
}
public void textContains(final String text) {
new WebDriverWait(driver, timeout) {
}.until(new ExpectedCondition<Boolean>() {
@Override
public Boolean apply(WebDriver driver) {
return driver.findElement(elementLocator).getText().contains(text);
}
});
}
public void textDoesNotContain(final String text) {
new WebDriverWait(driver, timeout) {
}.until(new ExpectedCondition<Boolean>() {
@Override
public Boolean apply(WebDriver driver) {
return !driver.findElement(elementLocator).getText().contains(text);
}
});
}
public void titleIsEqualTo(final String text) {
new WebDriverWait(driver, timeout) {
}.until(new ExpectedCondition<Boolean>() {
@Override
public Boolean apply(WebDriver driver) {
return driver.getTitle().equals(text);
}
});
}
public void titleDoesNotEqual(final String text) {
new WebDriverWait(driver, timeout) {
}.until(new ExpectedCondition<Boolean>() {
@Override
public Boolean apply(WebDriver driver) {
return !driver.getTitle().equals(text);
}
});
}
public void titleContains(final String text) {
new WebDriverWait(driver, timeout) {
}.until(new ExpectedCondition<Boolean>() {
@Override
public Boolean apply(WebDriver driver) {
return driver.getTitle().contains(text);
}
});
}
public void titleDoesNotContain(final String text) {
new WebDriverWait(driver, timeout) {
}.until(new ExpectedCondition<Boolean>() {
@Override
public Boolean apply(WebDriver driver) {
return !driver.getTitle().contains(text);
}
});
}
}
public ForWindows untilWindow() {
return new EbselenForWindows();
}
private class EbselenForWindows implements ForWindows {
public void countEquals(final int count) {
new WebDriverWait(driver, timeout) {
}.until(new ExpectedCondition<Boolean>() {
@Override
public Boolean apply(WebDriver driver) {
return driver.getWindowHandles().size() == count;
}
});
}
public void countDoesNotEqual(final int count) {
new WebDriverWait(driver, timeout) {
}.until(new ExpectedCondition<Boolean>() {
@Override
public Boolean apply(WebDriver driver) {
return driver.getWindowHandles().size() != count;
}
});
}
public void countIsGreaterThan(final int count) {
new WebDriverWait(driver, timeout) {
}.until(new ExpectedCondition<Boolean>() {
@Override
public Boolean apply(WebDriver driver) {
return driver.getWindowHandles().size() > count;
}
});
}
public void countIsLessThan(final int count) {
new WebDriverWait(driver, timeout) {
}.until(new ExpectedCondition<Boolean>() {
@Override
public Boolean apply(WebDriver driver) {
return driver.getWindowHandles().size() < count;
}
});
}
}
}
public ControlObject getControlObject() {
return new EbselenControlObject();
}
private class EbselenControlObject implements ControlObject {
public Mouse getMouseObject() {
return ((HasInputDevices) driver).getMouse();
}
public Keyboard getKeyboardObject() {
return ((HasInputDevices) driver).getKeyboard();
}
}
}