-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommonUtils.java
1351 lines (1196 loc) · 44.2 KB
/
CommonUtils.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
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
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package base;
import com.cucumber.listener.Reporter;
import com.google.common.collect.ImmutableMap;
import com.opencsv.CSVReader;
import contants.GlobalVariables;
import cucumber.api.Scenario;
import enums.BrowserType;
import enums.MobileType;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MobileElement;
import io.appium.java_client.PerformsTouchActions;
import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.ios.IOSDriver;
import io.appium.java_client.touch.offset.PointOption;
import org.apache.commons.codec.binary.Base64;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.RemoteWebElement;
import org.openqa.selenium.safari.SafariDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Field;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;
import static io.appium.java_client.touch.WaitOptions.waitOptions;
import static io.appium.java_client.touch.offset.PointOption.point;
public class CommonUtils extends GlobalVariables implements Wrappers, Wrappers.SelectDropDown {
private static Logger logger = Logger.getLogger(String.valueOf(CommonUtils.class));
private Select select;
public int retryattempts = 1;
static BrowserType browserType;
static MobileType mobileType;
Dimension size;
public static RemoteWebDriver driver;
public static ChromeOptions opt;
/**
* This method used to get url
* Name: Prabagaran
* Role: SDET
*/
public void getUrl(String url) {
driver.get(url);
}
/**
* This method used to set implicit wait
* Name: Prabagaran
* Role: SDET
*/
public void setImplicit(int timeOut) {
driver.manage().timeouts().implicitlyWait(timeOut, TimeUnit.SECONDS);
}
/**
* This method used to WebdriverWait
*/
public WebDriverWait webDriverWait() {
return new WebDriverWait(driver, elementWaitInSeconds);
}
/**
* This method used to quit browser
*/
public void quitBrowser() {
driver.quit();
}
/**
* This method used to close broswer
*/
public void closeBrowser() {
driver.close();
}
/**
* This method used to mouseOver a element
*/
public void mouseOver(WebElement element) {
waitVisibilityOfElement(element);
new Actions(driver).moveToElement(element).build().perform();
}
/**
* This method used to mouseOver a element using list
*/
public void mouseOver(List<WebElement> element, int index) {
waitVisibilityOfElement(element.get(index));
new Actions(driver).moveToElement(element.get(index)).build().perform();
}
/**
* This method used to get element text
*/
public String getText(WebElement element) {
waitVisibilityOfElement(element);
highLighterMethod(driver, element);
logger.info("Element Text - " + element.getText());
return element.getText();
}
/**
* This method used to match actual and expected text
*/
public boolean isTextMatch(String actual, String expected) {
logger.info("Actual Value - " + actual + "\n" + "Expected Value - " + expected);
return actual.equalsIgnoreCase(expected);
}
/**
* This method used to check element contain actual and expected text
*/
public boolean isTextContain(String actual, String expected) {
logger.info("Actual text - " + actual + "\n" + "Expected text - " + expected);
return actual.contains(expected);
}
/**
* This method used to apply Thread.sleep for waiting element.
*/
public void waitFor(int sleepTime) {
try {
Thread.sleep(sleepTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
/**
* This method used to check element is not clickable with retry
*/
public void notClickableAtPointRetryClick(WebElement element) {
int attempts = 0;
while (attempts < retryattempts) {
try {
waitVisibilityOfElement(element);
waitElementToBeClickable(element);
element.click();
break;
} catch (Exception e) {
waitFor(1000);
}
attempts++;
}
}
/**
* This method used to click element.
*/
public void clickButton(WebElement element) {
try {
scrollToElement(element);
waitVisibilityOfElement(element);
waitElementToBeClickable(element);
element.click();
} catch (StaleElementReferenceException e) {
staleRetryingFindClick(element);
} catch (Exception e) {
notClickableAtPointRetryClick(element);
}
}
/**
* This method used to refresh a page
*/
public void refreshPage() {
driver.navigate().refresh();
}
/**
* This method used to click element without scroll
*/
public void clickButtonWithOutScroll(WebElement element) {
waitVisibilityOfElement(element);
waitElementToBeClickable(element);
waitFor(2000);
element.click();
}
/**
* This method used to click element using JS executor
*/
public void jsclickButtonWithOutScroll(WebElement element) {
waitVisibilityOfElement(element);
waitElementToBeClickable(element);
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", element);
}
/**
* This method used to scroll to top using JS executor
*/
public void scrollToTop() {
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollTo(0,0)");
waitFor(1000);
}
/**
* This method used to scroll to bottom using JS executor
*/
public void scrollToBottom() {
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollTo(0, document.body.scrollHeight)");
waitFor(1000);
}
/**
* This method used to return xpath object
*/
public By locateXpath(String xpath) {
return By.xpath(xpath);
}
/**
* This method used to return CSS object
*/
public By locateCss(String css) {
return By.cssSelector(css);
}
/**
* This method used to click element using webelement
*/
public void clickButton(WebElement scrollToElement, WebElement clickElement) {
waitFor(1000);
scrollToElement(scrollToElement);
waitVisibilityOfElement(clickElement);
waitElementToBeClickable(clickElement);
clickElement.click();
}
/**
* This method used to click dropdown object
*/
public void clickDropDown(WebElement element, String xpath) {
waitFor(1000);
waitPresenceOfElementLocated(locateCss(xpath));
element.click();
}
/**
* This method used to enter text with scroll to element
*/
public void enterText(WebElement element, String... textValue) {
scrollToElement(element);
waitVisibilityOfElement(element);
element.clear();
waitFor(1000);
logger.info("Entered Text - " + textValue);
element.sendKeys(textValue);
}
/**
* This method used to enter text without scroll to element
*/
public void enterTextWithoutScroll(WebElement element, String textValue) {
waitVisibilityOfElement(element);
element.clear();
waitFor(1000);
logger.info("Entered Text - " + textValue);
element.sendKeys(textValue);
}
/**
* This method used to apply explicity wait for webelement
*/
public void waitVisibilityOfElement(WebElement element) {
webDriverWait().until(ExpectedConditions.visibilityOf(element));
}
/**
* This method used to apply explicity wait for Mob element
*/
public void waitVisibilityOfElement(MobileElement element) {
webDriverWait().until(ExpectedConditions.visibilityOf(element));
}
/**
* This method used to wait element until clickable for web element
*/
public void waitElementToBeClickable(WebElement element) {
webDriverWait().until(ExpectedConditions.elementToBeClickable(element));
}
/**
* This method used to wait element until clickable for mobile element
*/
public void waitElementToBeClickable(MobileElement element) {
webDriverWait().until(ExpectedConditions.elementToBeClickable(element));
}
/**
* This method used to wait until element present
*/
public void waitPresenceOfElementLocated(By by) {
webDriverWait().until(ExpectedConditions.presenceOfElementLocated(by));
}
/**
* This method used for load disapper
*/
public void loaderDisappear() {
List<WebElement> loader = driver.findElements(By.xpath("//div[@class='modal-backdrop fade in']/span"));
if (!loader.isEmpty()) {
webDriverWait().until(ExpectedConditions
.invisibilityOfElementLocated((By.xpath("//div[@class='modal-backdrop fade in']/span"))));
waitFor(500);
}
}
/**
* This method used for switch to parent window
*/
public void switchToParentWindow() {
Set<String> winHandles = driver.getWindowHandles();
for (String wHandle : winHandles) {
driver.switchTo().window(wHandle);
break;
}
}
/**
* This method used for switch to last window
*/
public void switchToLastWindow() {
Set<String> winHandles = driver.getWindowHandles();
for (String wHandle : winHandles) {
driver.switchTo().window(wHandle);
}
}
/**
* This method used for highlighing the element
*/
public void highLighterMethod(WebDriver driver, WebElement element) {
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].setAttribute('style', 'background: ; border: 2px solid blue;');", element);
}
/**
* This method used to check whether the element is displayed.
*/
public boolean isElementDisplayed(List<WebElement> elements) {
return !elements.isEmpty();
}
/**
* This method used to handle stale Retrying Find Click
*/
public boolean staleRetryingFindClick(WebElement element) {
System.out.println("Inside in the staleRetryingFindClick");
boolean result = false;
int attempts = 0;
while (attempts < retryattempts) {
try {
waitVisibilityOfElement(element);
waitElementToBeClickable(element);
element.click();
result = true;
break;
} catch (StaleElementReferenceException e) {
waitFor(500);
}
attempts++;
}
return result;
}
/**
* This method used to handle stale Retrying enter text
*/
public boolean staleRetryingEnterText(WebElement element, String... value) {
boolean result = false;
int attempts = 0;
while (attempts < retryattempts) {
try {
waitVisibilityOfElement(element);
element.sendKeys(value);
result = true;
break;
} catch (StaleElementReferenceException e) {
waitFor(500);
}
attempts++;
}
return result;
}
/**
* This method used to handle stale Retrying element
*/
public boolean staleRetryingElementIsDisplayed(WebElement element) {
boolean result = false;
int attempts = 0;
while (attempts < retryattempts) {
try {
waitVisibilityOfElement(element);
element.isDisplayed();
result = true;
break;
} catch (StaleElementReferenceException e) {
waitFor(500);
}
attempts++;
}
return result;
}
/**
* This method used to display elemnet using web element
*/
public boolean isElementDisplayed(WebElement element) {
boolean flag;
try {
waitFor(1000);
setImplicit(10);
scrollToElement(element);
waitVisibilityOfElement(element);
highLighterMethod(driver, element);
flag = element.isDisplayed();
} catch (StaleElementReferenceException stale) {
flag = staleRetryingElementIsDisplayed(element);
} catch (Exception e) {
flag = false;
}
logger.info("Is element " + element + " displayed - " + flag);
return flag;
}
/**
* This method used to display elemnet without scroll using web element
*/
public boolean isElementDisplayedWithoutScroll(WebElement element) {
boolean flag;
try {
waitFor(1000);
setImplicit(10);
highLighterMethod(driver, element);
element.isDisplayed();
flag = true;
} catch (Exception e) {
setImplicit(timeOut);
flag = false;
}
logger.info("Is element " + element + " displayed - " + flag);
return flag;
}
/**
* This method used to validate the element us enabled or not
*/
public boolean isElementEnabled(WebElement element) {
boolean flag;
try {
waitFor(1000);
scrollToElement(element);
highLighterMethod(driver, element);
element.isEnabled();
flag = true;
} catch (Exception e) {
flag = false;
}
logger.info("Is element " + element + " enabled - " + flag);
return flag;
}
/**
* This method used for validate element is enabled
*/
public boolean isEnabled(WebElement element) {
logger.info("Is element " + element + "enabled - " + element.isEnabled());
return element.isEnabled();
}
/**
* This method used to get attribute value
*/
public String getAttributeValue(WebElement element, String attributeName) {
waitVisibilityOfElement(element);
logger.info("Attribute Value - " + element.getAttribute(attributeName));
return element.getAttribute(attributeName);
}
/**
* This method used to take screenshot without encoded algorithm
*/
public void takeSnap(Scenario scenario) throws IOException {
String scrname = scenario.getId().replace(";", "").replace("-", "");
File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
org.apache.commons.io.FileUtils.copyFile(scrFile,
new File(getCurrentDir() + "/target/extent-report/FailureScreenShots/" + scrname + ".png"));
System.out.println("inside screenshot");
final byte[] screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);
scenario.embed(screenshot, "image/png"); // ... and embed it in the report.
Reporter.addScreenCaptureFromPath("./FailureScreenShots/" + scrname + ".png");
driver.quit();
}
/**
* This method used to take screenshot with image is embeded
*/
public void takeScreenShotonFailure(Scenario scenario) {
String encodedBase64 = null;
FileInputStream fileInputStreamReader = null;
if (scenario.getStatus().equalsIgnoreCase("failed")) {
try {
File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
fileInputStreamReader = new FileInputStream(scrFile);
byte[] bytes = new byte[(int) scrFile.length()];
fileInputStreamReader.read(bytes);
encodedBase64 = new String(Base64.encodeBase64(bytes));
scenario.embed(bytes, "image/png");
Reporter.addScreenCaptureFromPath("data:image/png;base64," + encodedBase64);
} catch (Exception e) {
e.printStackTrace();
}
logger.info("Screen Shot taken");
}
}
public static String getCurrentDir() {
String currentDir = System.getProperty("user.dir");
currentDir = currentDir.replace('\\', '/');
return currentDir;
}
public void scrollToElement(WebElement element) {
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);
}
public Select selectDropdown(WebElement element) {
select = new Select(element);
return select;
}
public void selectByIndex(WebElement element, int index) {
selectDropdown(element).selectByIndex(index);
}
public void SelectByValue(WebElement element, String value) {
waitFor(1000);
selectDropdown(element).selectByValue(value);
}
public void SelectByVisibleText(WebElement element, String text) {
waitFor(1000);
selectDropdown(element).selectByVisibleText(text);
}
public void waitForLoadIconDisappear() {
webDriverWait()
.until(ExpectedConditions.invisibilityOfElementLocated(By.cssSelector(".oak-searchResults_preloader")));
}
public String getPageTitle() {
return driver.getTitle();
}
/****
* swipe element right or left by co-ordinated
*
* @param element
* @param offSet - (-1 swipe left or +1 swipe right)
*/
public void swipeByX(WebElement element, int offSet) {
Point point = element.getLocation();
Actions actions = new Actions(driver);
System.out.println("val: " + point.getX());
actions.dragAndDropBy(element, point.getX() - offSet, point.getY()).build().perform();
}
public void swipeByXCoordinates(WebElement element, int offSet) {
Point point = element.getLocation();
Actions actions = new Actions(driver);
System.out.println("val: " + point.getX());
actions.dragAndDropBy(element, offSet, point.getY()).build().perform();
}
public static WebElement getXpathElement(String xpath) {
return driver.findElement(By.xpath(xpath));
}
@SuppressWarnings("unused")
public void switchToFirstFrame() {
List<WebElement> frames = driver.findElements(By.xpath("//iframe"));
for (int i = 0; i < frames.size(); i++) {
driver.switchTo().frame(i);
break;
}
}
public void clickAcceptInBrowserAlertPopUp() {
try {
webDriverWait().until(ExpectedConditions.alertIsPresent());
driver.switchTo().alert().accept();
} catch (Exception e) {
System.out.println(e);
}
}
public void clearTextInTheTextField(WebElement element) {
waitVisibilityOfElement(element);
element.clear();
}
public String removingWhiteSpaces(String data) {
String csvReqID = data.trim();
String finalReqID = "";
char[] value = csvReqID.toCharArray();
for (int i = 0; i < value.length; i++) {
if ((i % 2) == 0) {
finalReqID = finalReqID + value[i];
}
}
return finalReqID;
}
public void deleteAFile(String filePath) {
File file = new File(filePath);
file.delete();
}
public ArrayList<String> storingDataFromCSVFileToArray(String CSVPath) throws IOException {
@SuppressWarnings("resource")
CSVReader reader = new CSVReader(new FileReader(CSVPath));
String[] cell;
ArrayList<String> records = new ArrayList<String>();
while ((cell = reader.readNext()) != null) {
for (String record : cell) {
records.add(record);
}
}
return records;
}
public String gettingTextMessageFromAlertPopUp() {
return (driver.switchTo().alert().getText());
}
public void closingTheAlertPopUp() {
Alert alert = driver.switchTo().alert();
alert.accept();
}
public Map<String, List<String>> getHorizontalData(cucumber.api.DataTable dataTable) {
Map<String, List<String>> dataMap = null;
try {
dataMap = new HashMap<String, List<String>>();
List<String> headingRow = dataTable.raw().get(0);
int dataTableRowsCount = dataTable.getGherkinRows().size() - 1;
ArrayList<String> totalRowCount = new ArrayList<String>();
totalRowCount.add(Integer.toString(dataTableRowsCount));
dataMap.put("totalRowCount", totalRowCount);
for (int i = 0; i < headingRow.size(); i++) {
List<String> dataList = new ArrayList<String>();
dataMap.put(headingRow.get(i), dataList);
for (int j = 1; j <= dataTableRowsCount; j++) {
List<String> dataRow = dataTable.raw().get(j);
dataList.add(dataRow.get(i));
}
}
} catch (Exception e) {
e.printStackTrace();
}
return dataMap;
}
public String getTestDataValue(String classPath, String fieldName) {
String expectedPageName = null;
try {
Class<?> classObj = Class.forName(classPath);
Object obj = classObj.newInstance();
Field fieldValue = classObj.getDeclaredField(fieldName);
expectedPageName = (String) fieldValue.get(obj);
} catch (Exception e) {
System.out.println(e);
}
return expectedPageName;
}
public String[] getTestDataValues(String classPath, String fieldName) {
String[] expectedPageName = null;
try {
Class<?> classObj = Class.forName(classPath);
Object obj = classObj.newInstance();
Field fieldValue = classObj.getDeclaredField(fieldName);
expectedPageName = (String[]) fieldValue.get(obj);
} catch (Exception e) {
System.out.println(e);
}
return expectedPageName;
}
/**
* This method used to switch To NativeApp Context
* Name: Prabagaran
* Role: SDET
* Employee Id: 119584
* Project: Medimpact Mobile Automation
*/
public void switchToNativeAppContext(AppiumDriver<?> app) {
try {
Set<String> contextHandles = app.getContextHandles();
for (String context : contextHandles) {
if (context.contains("NATIVE")) {
app.context(context);
Thread.sleep(200L);
}
}
} catch (Exception e) {
logger.info(e.getMessage());
}
}
/**
* This method used to switch To WebApp Context
*/
public boolean switchToWebAppContext(AndroidDriver<?> app) throws InterruptedException {
boolean flag = true;
try {
System.out.println("**********************Started switching to web context********************");
Set<String> contextHandles = app.getContextHandles();
for (String context : contextHandles) {
if (context.contains("WEBVIEW")) {
app.context(context);
Thread.sleep(200L);
}
}
flag = false;
System.out.println("**********************Successfully switching to web context***************");
} catch (Exception e) {
System.err.println("********************Not able to switching to web context******************");
logger.info(e.getMessage());
} finally {
System.out.println("flag Value : " + flag);
if (flag) {
System.out.println("Finally Block Executed : Switch to Native App Method Called ");
switchToNativeAppContext(app);
System.out.println("********************** switching back to Native App context***************");
}
}
return flag;
}
/**
* This method used to hide mobile keyboard
*/
public boolean hidemobileKeyboard(AndroidDriver app) {
boolean isKeyboardHideResult = false;
try {
app.hideKeyboard();
isKeyboardHideResult = true;
} catch (Exception e) {
logger.info(e.getMessage());
}
return isKeyboardHideResult;
}
/**
* This method used to find element is display without scroll.
*/
public boolean isElementDisplayed(MobileElement element) {
boolean flag;
try {
waitVisibilityOfElement(element);
flag = element.isDisplayed();
} catch (StaleElementReferenceException stale) {
flag = staleRetryingElementIsDisplayed(element);
} catch (Exception e) {
flag = false;
}
logger.info("Is element " + element + " displayed - " + flag);
return flag;
}
/**
* This method used to element is present
*/
public boolean isElementDisplayedWithoutScroll(MobileElement element) {
boolean flag;
try {
waitFor(2000);
flag = element.isDisplayed();
} catch (Exception e) {
setImplicit(timeOut);
flag = false;
}
logger.info("Is element " + element + " displayed - " + flag);
return flag;
}
/**
* This method used to enter a text without clear.
*/
public void enterTextWithoutScroll(MobileElement element, String textValue) {
waitFor(1000);
element.click();
if (platformName.equalsIgnoreCase("android")) {
element.clear();
}else{
clearTextFields(element);
}
logger.info("Entered Text - " + textValue);
element.sendKeys(textValue);
}
/**
* This method used to enter a text with clear.
*/
public void mobile_enterText(MobileElement element, String textValue) {
waitFor(1000);
element.click();
element.sendKeys(textValue);
logger.info("Entered Text - " + textValue);
}
/**
* This method used to click button without scroll
*/
public void clickButtonWithOutScroll(MobileElement element) {
waitElementToBeClickable(element);
element.click();
}
/**
* This method used to clear text in textbox.
*/
public void clearTextFields(MobileElement element){
int isCount = element.getAttribute("value").length() + 4;
System.err.println(isCount);
for (int i = 0; i <= isCount; i++) {
driver.findElement(By.xpath("//XCUIElementTypeKey[@name='Delete'] | //XCUIElementTypeKey[@name='delete']")).click();
}
}
/**
* This method used for hide a keyboard.
*/
public void hideMobileKeyboard() {
if (platformName.equalsIgnoreCase("android")) {
hidemobileKeyboard((AndroidDriver) driver);
} else {
mob_KeyboardDoneButton();
}
}
/**
* used to click mobile keyboard 'Done' Button
*/
public void mob_KeyboardDoneButton() {
int isPresent = driver.findElements(By.xpath("//XCUIElementTypeButton[@name='Done'] | //android.widget.TextView[@text='Done']")).size();
if (isPresent == 1) {
clickButtonWithOutScroll(driver.findElement(By.xpath("//XCUIElementTypeButton[@name='Done'] | //android.widget.TextView[@text='Done']")));
}
}
/**
* used to scroll up screen
* Name: Prabagaran
*/
public void mobile_fnScrollUp() {
try {
size = driver.manage().window().getSize();
int x1 = (int) (size.width * 0.50);
int y1 = (int) (size.height * 0.45);
int y2 = (int) (size.height * 0.75);
new TouchAction((PerformsTouchActions) driver).press(point(x1, y1)).waitAction(waitOptions(Duration.ofMillis(1000)))
.moveTo(point(x1, y2)).release().perform();
setImplicit(4);
logger.info("Object has been scrolled up");
} catch (Exception e) {
logger.info("Object has been not scrolled up");
}
}
/**
* used to click mobile keyboard 'Search' Button
*/
public void mob_keyBoardSearchButton(){
try{
if(platformName.equalsIgnoreCase("android")){
driver.executeScript( "mobile: performEditorAction", ImmutableMap.of("action", "Search"));
}
else{
driver.findElement(By.xpath("//XCUIElementTypeButton[@name='Search']")).click();
}
logger.info("Search button has been clicked");
}catch(Exception e){
logger.info("Search button has been not clicked");
}
}
/**
* used to click keybaord Go Button
*/
public void mob_keyBoardGoButton(){
try{
if (platformName.equalsIgnoreCase("ios")){
driver.findElement(By.xpath("//XCUIElementTypeButton[@name='Go']")).click();
}else{
driver.executeScript("mobile: performEditorAction", ImmutableMap.of("action", "Go"));
}
logger.info("Go button has been clicked");
}catch(Exception e){
logger.info("Go button has been not clicked");
}
}
/**
* used to click mobile keyboard 'Next' Button
*/
public void mob_keyBoardNextButton(){
try{
if (platformName.equalsIgnoreCase("ios")){
driver.findElement(By.xpath("//XCUIElementTypeButton[@name='Next:']")).click();
}else{
driver.executeScript( "mobile: performEditorAction", ImmutableMap.of("action", "next"));
}
logger.info("Search button has been clicked");
}catch(Exception e){
logger.info("Search button has been not clicked");
}
}
/**
* used to scroll up by element
*/
public void mobile_fnScrollUptoElement(By byProperty) {
try {
int elePresent = driver.findElements(byProperty).size();
while (elePresent == 0) {
size = driver.manage().window().getSize();
int x1 = (int) (size.width * 0.50);
int y1 = (int) (size.height * 0.45);
int y2 = (int) (size.height * 0.75);
new TouchAction((PerformsTouchActions) driver).press(point(x1, y1)).waitAction(waitOptions(Duration.ofMillis(1000)))
.moveTo(point(x1, y2)).release().perform();
elePresent = driver.findElements(byProperty).size();
}
logger.info("Object has been scrolled up");
} catch (Exception e) {
logger.info("Object has been not scrolled up");
}
}
/**
* used to scroll down
*/
public void mobile_fnScrollDown() {
try {
size = driver.manage().window().getSize();