-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommonFunctions.java
More file actions
764 lines (662 loc) · 18 KB
/
CommonFunctions.java
File metadata and controls
764 lines (662 loc) · 18 KB
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
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
/*###################################################################################################
Name: CommonFunctions.java
Description: Common used function accross the project
Author: Venkat
Date Created:01/17/2014
Date Modified:
Updated Comments:
#####################################################################################################*/
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Comparator;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.FluentWait;
import org.openqa.selenium.support.ui.Select;
public class CommonFunctions {
public static String FileName;
public static String DownloadedFileName;
public static String Filepath;
/**
* it will wait until element invisible on the web page within specified
* time
*
* @author Venkata
* @param int
* Timeouty
* @param String
* objxpath
* @param final
* WebDriver aDriver
* @throws Exception
*/
public void waitUntilElementInvisibe(int Timeout, String objxpath, final WebDriver aDriver) throws Exception {
try {
new FluentWait<WebDriver>(aDriver).withTimeout(Timeout, TimeUnit.SECONDS).pollingEvery(5, TimeUnit.SECONDS)
.ignoring(NoSuchElementException.class)
.until(ExpectedConditions.invisibilityOfElementLocated(By.xpath(objxpath)));
} catch (Exception e) {
}
}
/**
* it will wait until element visible on the web page within specified time
*
* @author Venkata
* @param int
* Timeouty
* @param String
* objxpath
* @param final
* WebDriver aDriver
* @throws Exception
*/
public void waitforElement(int Timeout, String objxpath, final WebDriver aDriver) throws Exception {
try {
new FluentWait<WebDriver>(aDriver).withTimeout(Timeout, TimeUnit.SECONDS).pollingEvery(5, TimeUnit.SECONDS)
.ignoring(NoSuchElementException.class)
.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(objxpath)));
} catch (Exception e) {
}
}
/**
* it will wait until element visible on the web page within specified time
*
* @author Venkata
* @param int
* Timeouty
* @param final
* WebElement key
* @param final
* WebDriver aDriver
* @throws Exception
*/
public void waitforElement(int Timeout, final WebElement key, final WebDriver aDriver) throws Exception {
try {
FluentWait<WebDriver> wait = new FluentWait<WebDriver>(aDriver).withTimeout(Timeout, TimeUnit.SECONDS)
.pollingEvery(5, TimeUnit.SECONDS).ignoring(NoSuchElementException.class);
wait.until(ExpectedConditions.visibilityOf(key));
} catch (Exception e) {
}
}
/**
* Type value in given webelement field
*
* @author Venkata
* @param webelement
* key
* @param String
* value
* @throws Exception
*/
public void type(WebElement key, String value) throws Exception {
if (key.isDisplayed()) {
key.sendKeys(value);
} else {
throw new Exception(key + "is not found");
}
}
/**
* it will perform the tab action on given webelement field
*
* @author Venkata
* @param webelement
* @throws Exception
*/
public void sendkeysTab(WebElement key) throws Exception {
if (key.isDisplayed()) {
key.sendKeys(Keys.TAB);
} else {
throw new Exception(key + "is not found");
}
}
/**
* it will perform the Back Space action on given webelement field
*
* @author Venkata
* @param webelement
* key
* @throws Exception
*/
public void sendkeybackSpace(WebElement key) throws Exception {
if (key.isDisplayed()) {
key.sendKeys(Keys.BACK_SPACE);
} else {
throw new Exception(key + "is not found");
}
}
/**
* it will perform the Enter action on given webelement field
*
* @author Venkata
* @param webelement
* key
* @throws Exception
*/
public void sendkeysEnter(WebElement key) throws Exception {
if (key.isDisplayed()) {
key.sendKeys(Keys.ENTER);
} else {
throw new Exception(key + "is not found");
}
}
/**
* it will perform the Return action on given webelement field
*
* @author Venkata
* @param webelement
* key
* @throws Exception
*/
public void sendkeysReturn(WebElement key) throws Exception {
if (key.isDisplayed()) {
key.sendKeys(Keys.RETURN);
} else {
throw new Exception(key + "is not found");
}
}
/**
* Type value in given webelement field using JavaScript Executor
*
* @author Venkata
* @param WebDriver
* @param webelement
* @param value
* @throws Exception
*/
public void JavaScripttype(final WebDriver aDriver, WebElement key, String value) throws Exception {
if (key.isDisplayed()) {
JavascriptExecutor jse = ((JavascriptExecutor) aDriver);
jse.executeScript("arguments[0].value='" + value + "';", key);
} else {
throw new Exception(key + "is not found");
}
}
/**
* it will perform the Back Space action on given webelement field using
* Javascript executor
*
* @author Venkata
* @param WebDriver
* @param webelement
* @param value
* @throws Exception
*/
public void JavaScripttypespace(final WebDriver aDriver, WebElement key, String value) throws Exception {
if (key.isDisplayed()) {
JavascriptExecutor jse = ((JavascriptExecutor) aDriver);
jse.executeScript("arguments[0].value='" + value + " " + "';", key);
} else {
throw new Exception(key + "is not found");
}
}
/**
* Click on the given webelement field
*
* @author Venkata
* @param webelement
* key
* @throws Exception
* @throws InterruptedException
*/
public void click(WebElement key) throws Exception {
if (key.isDisplayed()) {
key.click();
} else {
throw new Exception(key + "is not found");
}
}
/**
* Click on the given webelement field using JavaScript Executor
*
* @author Venkata
* @param WebDriver
* @param webelement
* @throws Exception
* @throws InterruptedException
*/
public void JavaScriptclick(final WebDriver aDriver, WebElement key) throws Exception {
if (key.isDisplayed()) {
JavascriptExecutor jse = (JavascriptExecutor) aDriver;
jse.executeScript("arguments[0].click();", key);
} else {
throw new Exception(key + "is not found");
}
}
/**
* it will perform the Mouse Hover action on given webelement field using
* Javascript executor
*
* @author Venkata
* @param WebDriver
* @param webelement
* @throws Exception
* @throws InterruptedException
*/
public void JavaScriptMousehover(final WebDriver aDriver, WebElement key) throws Exception {
if (key.isDisplayed()) {
String mouseOverScript = "if(document.createEvent){ " + "var evObj = document.createEvent('MouseEvents');"
+ "evObj.initEvent('mouseover',true, false); " + "arguments[0].dispatchEvent(evObj);} "
+ "else if(document.createEventObject) { " + "arguments[0].fireEvent('onmouseover');}";
((JavascriptExecutor) aDriver).executeScript(mouseOverScript, key);
} else {
throw new Exception(key + "is not found");
}
}
/**
* Click on the given maskedit textboxes webelement field and send the data
* into the WebElement Field
*
* @author Venkata maskedit textboxes (Phone Number, Zip etc......)
* @param webelement
* @param String
* @throws Exception
* @throws InterruptedException
*/
public void clickandtype(WebElement key, String value) throws Exception {
if (key.isDisplayed()) {
key.click();
key.sendKeys(value);
} else {
throw new Exception(key + "is not found");
}
}
/**
* Get innerText of given webelement from html doc
*
* @author Venkata
* @param key
* @return visible inner html text
* @throws Exception
*/
public String getText(WebElement key) throws Exception {
if (key.isDisplayed()) {
key.getText();
} else {
throw new Exception(key + "is not found");
}
return key.isDisplayed() ? key.getText() : null;
}
/**
* Get Attribute name based on id, name, class, value of given webelement
* from html doc
*
* @author Venkata
* @param key
* @param String
* @return visible inner html text
* @throws Exception
*/
public String getAttribute(WebElement key, String AttributeName) throws Exception {
if (key.isDisplayed()) {
key.getAttribute(AttributeName);
} else {
throw new Exception(key + "is not found");
}
return key.isDisplayed() ? key.getAttribute(AttributeName) : null;
}
/**
* Verifying the webelement field using isDisplayed method
*
* @author Venkata
* @param WebDriver
* aDriver
* @param webelement
* element
* @throws InterruptedException
*/
public boolean VerifyObject(final WebDriver aDriver, WebElement element) throws Exception {
try {
aDriver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
if (element.isDisplayed())
;
return true;
} catch (Exception e) {
return false;
}
}
/**
* Verifying the webelement field using isDisplayed method
*
* @author Venkata
* @param WebDriver
* aDriver
* @param String
* objxpath
* @throws InterruptedException
*/
public boolean VerifyObject(final WebDriver aDriver, String objxpath) throws Exception {
try {
aDriver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
if (aDriver.findElement(By.xpath(objxpath)).isDisplayed())
;
return true;
} catch (NoSuchElementException e) {
return false;
}
}
/**
* Clear the input value from webelement field
*
* @author Venkata
* @param webelement
* @throws Exception
* @throws InterruptedException
*/
public void clear(final WebElement key) throws Exception {
if (key.isDisplayed()) {
key.clear();
} else {
throw new Exception(key + "is not found");
}
}
/**
* Select value in given webelement field
*
* @author Venkata
* @param webelement
* @param value
* @throws Exception
*/
public void selectByValue(final WebElement key, final String value) throws Exception {
if (key.isDisplayed()) {
new Select(key).selectByVisibleText(value);
;
} else {
throw new Exception(key + "is not found");
}
}
/**
* Select value in given webelement field based on Index
*
* @author Venkata
* @param webelement
* @param int
* index
* @throws Exception
*/
public void selectByIndex(final WebElement key, final int value) throws Exception {
if (key.isDisplayed()) {
new Select(key).selectByIndex(value);
;
} else {
throw new Exception(key + "is not found");
}
}
/*
* it is used to down the page based on WebElement
*
* @author Venkata
*
* @param webelement
*
* @throws Exception
*/
public void sendKeysPagedown(final WebElement key) throws Exception {
if (key.isDisplayed()) {
key.sendKeys(Keys.PAGE_DOWN);
;
} else {
throw new Exception(key + "is not found");
}
}
/**
* Switch to the Frame based on the id or name
*
* @author Venkata
* @param WebDriver
* @param String
*/
public void switchToFrame(final WebDriver aDriver, String frameName) {
aDriver.switchTo().frame(frameName);
}
/**
* Switch to the Frame based on the frame index
*
* @author Venkata
* @param WebDriver
* @param int
*/
public void switchToFrame(final WebDriver aDriver, int frameindex) {
aDriver.switchTo().frame(frameindex);
}
/**
* Switch to the Frame based on the WebElement field
*
* @author Venkata
* @param WebDriver
* @param WebElement
*/
public void switchToFrame(final WebDriver aDriver, WebElement frameName) {
aDriver.switchTo().frame(frameName);
}
/**
* Switch to the default Content
*
* @author Venkata
* @param WebDriver
*/
public void switchToMain(final WebDriver aDriver) {
aDriver.switchTo().defaultContent();
}
/**
* Switch to the Alert window and then Accept
*
* @author Venkata
* @param WebDriver
*/
public void switchToAlertAccept(final WebDriver aDriver) {
Alert alert = aDriver.switchTo().alert();
alert.accept();
}
/**
* Switch to the Alert window and then Dismiss
*
* @author Venkata
* @param WebDriver
*/
public void switchToAlertDismiss(final WebDriver aDriver) {
Alert alert = aDriver.switchTo().alert();
alert.dismiss();
}
/**
* Switch to the Alert Prompt box and then type value and Accept
*
* @author Venkata
* @param WebDriver
* @param String
*/
public void switchToAlertPrompttypeandAccept(final WebDriver aDriver, String value) {
Alert alert = aDriver.switchTo().alert();
alert.sendKeys(value);
alert.accept();
}
/**
* Return true if alert present else return false
*
* @author Venkata
* @param WebDriver
* @throws Exception
*/
public boolean isAlertPresent(final WebDriver aDriver) throws Exception {
try {
aDriver.switchTo().alert();
return true;
} catch (Exception e) {
throw new Exception("alert is not found");
}
}
/**
* if condition to compare expected and actual
*
* @author Venkata
* @param String
* expected
* @param String
* actual
*/
public void ifcondition(String expected, String actual) {
if (expected.equals(actual)) {
System.out.println("expected matches with actual");
}
}
/**
* Switch to the child window from parent window
*
* @author Venkata
* @param String
* @param WebDriver
*/
public void switchToChildWindow(String parentHandler, final WebDriver aDriver) {
for (String winHandle : aDriver.getWindowHandles()) {
if (!winHandle.equals(parentHandler)) {
aDriver.switchTo().window(winHandle);
}
}
}
/**
* Close the Child Window and Switch to the Parent Window
*
* @author Venkata
* @param String
* @param WebDriver
*/
public void switchTomainWindow(String parentHandler, final WebDriver aDriver) {
aDriver.close();
aDriver.switchTo().window(parentHandler);
}
/**
* get the title of the page
*
* @author Venkata
* @param WebDriver
*/
public String gettitle(final WebDriver aDriver) {
return aDriver.getTitle();
}
/**
* Scroll down page using the Java script
*
* @author Venkata
* @param WebDriver
*/
public void scrollDown(final WebDriver aDriver) {
JavascriptExecutor js = (JavascriptExecutor) aDriver;
js.executeScript("scroll(0,300)");
}
/**
* Scroll Up page using the Java script
*
* @author Venkata
* @param WebDriver
*/
public void scrollUp(final WebDriver aDriver) {
JavascriptExecutor js = (JavascriptExecutor) aDriver;
js.executeScript("scroll(300,0)");
}
public boolean VerifyText(WebDriver driver, String Object, String text) throws Exception {
try {
WebElement element = driver.findElement(By.xpath(Object));
String elementText = element.getText();
System.out.println(elementText);
if (elementText.equalsIgnoreCase(text)) {
return true;
}
} catch (NoSuchElementException e) {
throw new Exception(Object + "is not found");
}
return false;
}
@SuppressWarnings("unchecked")
public String LatestDownloadedFile(String FolderName, boolean isFromChrome) throws Exception {
try {
if (!isFromChrome) {
File directory = new File(".//");
String DownloadFilePath = directory.getCanonicalPath() + "\\" + "DownloadingFiles";
File dir = new File(DownloadFilePath);
File[] files = dir.listFiles();
Arrays.sort(files);
Arrays.sort(files, new Comparator() {
public int compare(Object o1, Object o2) {
return compare((File) o1, (File) o2);
}
private int compare(File f1, File f2) {
long result = f2.lastModified() - f1.lastModified();
if (result > 0) {
return 1;
} else if (result < 0) {
return -1;
} else {
return 0;
}
}
});
String LatestDownloadedFile = Arrays.asList(files[0]).toString();
String regex = "\\[|\\]";
String DownloadPath = LatestDownloadedFile.replaceAll(regex, "");
String[] arrFileName = LatestDownloadedFile.split("\\\\");
FileName = arrFileName[arrFileName.length - 1];
if (FileName.contains("(")) {
String[] splitfilename = FileName.split("\\(");
FileName = splitfilename[0];
System.out.println("split filename " + splitfilename[0]);
}
String[] arrFileNames = FileName.split("\\.");
String FileNames = arrFileNames[0];
DownloadedFileName = FileNames;
String s = xDownloadFiles(FolderName);
Filepath = s + "\\" + FileName.replace("]", "");
Path RSource = Paths.get(DownloadPath);
Path RTarget = Paths.get(Filepath);
Files.move(RSource, RTarget, REPLACE_EXISTING);
} else {
String DownloadFilePath = xDownloadFiles(FolderName);
File dir = new File(DownloadFilePath);
File[] files = dir.listFiles();
Arrays.sort(files);
Arrays.sort(files, new Comparator() {
public int compare(Object o1, Object o2) {
return compare((File) o1, (File) o2);
}
private int compare(File f1, File f2) {
long result = f2.lastModified() - f1.lastModified();
if (result > 0) {
return 1;
} else if (result < 0) {
return -1;
} else {
return 0;
}
}
});
String LatestDownloadedFile = Arrays.asList(files[0]).toString();
String regex = "\\[|\\]";
String DownloadPath = LatestDownloadedFile.replaceAll(regex, "");
Filepath = DownloadPath;
String[] arrFileName = LatestDownloadedFile.split("\\\\");
FileName = arrFileName[arrFileName.length - 1];
if (FileName.contains("(")) {
String[] splitfilename = FileName.split("\\(");
FileName = splitfilename[0];
System.out.println("split filename " + splitfilename[0]);
}
String[] arrFileNames = FileName.split("\\.");
String FileNames = arrFileNames[0];
DownloadedFileName = FileNames;
}
} catch (Exception e) {
return e.getMessage();
}
return Filepath;
}
}