How to use toString method of com.qaprosoft.carina.core.foundation.utils.mobile.notifications.android.Notification class

Best Carina code snippet using com.qaprosoft.carina.core.foundation.utils.mobile.notifications.android.Notification.toString

Source:AndroidService.java Github

copy

Full Screen

...112 try {113 LOGGER.info("Try to execute following cmd: " + CmdLine.arrayToString(execCmd));114 List<String> execOutput = executor.execute(execCmd);115 LOGGER.info("Output after execution ADB command: " + execOutput);116 result = execOutput.toString().replaceAll("\\[|\\]", "").replaceAll(", ", " ").trim();117 LOGGER.info("Returning Output: " + result);118 } catch (Exception e) {119 LOGGER.error(e);120 }121 return result;122 }123 /**124 * press Home button to open home screen125 */126 public void gotoAndroidHome() {127 executeAdbCommand("shell input keyevent 3");128 }129 /**130 * openApp131 *132 * @param pkg String133 * @param activity String134 */135 public void openApp(String pkg, String activity) {136 openApp(pkg.trim() + "/" + activity.trim());137 }138 /**139 * openApp140 *141 * @param app String142 */143 public void openApp(String app) {144 String result = executeAdbCommand("shell am start -n " + app);145 if (result.contains("Exception")) {146 String appPackage = app.split("/")[0];147 if (!checkCurrentDeviceFocus(appPackage)) {148 LOGGER.info("Expected app is not in focus. We will try another solution.");149 executeAdbCommand("shell monkey -p " + appPackage + " -c android.intent.category.LAUNCHER 1");150 }151 }152 }153 /**154 * clear Apk Cache155 *156 * @param appPackageName for example:157 * com.bamnetworks.mobile.android.gameday.atbat158 * @return boolean159 */160 public boolean clearApkCache(String appPackageName) {161 // Later can be used:162 /*163 * String packageName = executor.getApkPackageName(String apkFile);164 * executor.clearAppData(Device device, String appPackage);165 */166 String result = executeAdbCommand("shell pm clear " + appPackageName);167 if (result.contains("Success")) {168 LOGGER.info("Cache was cleared correctly");169 return true;170 } else {171 LOGGER.error("Cache was not cleared. May be application does not exist on this device.");172 return false;173 }174 }175 /**176 * checkCurrentDeviceFocus - return actual device focused apk and compare177 * with expected.178 *179 * @param apk String180 * @return boolean181 */182 public boolean checkCurrentDeviceFocus(String apk) {183 String res = getCurrentDeviceFocus();184 if (res.contains(apk)) {185 LOGGER.info("Actual device focus is as expected and contains package or activity: '" + apk + "'.");186 return true;187 } else {188 LOGGER.error("Not expected apk '" + apk + "' is in focus. Actual result is: " + res);189 return false;190 }191 }192 /**193 * getCurrentDeviceFocus - get actual device apk in focus194 *195 * @return String196 */197 public String getCurrentDeviceFocus() {198 String result = executeAdbCommand("shell dumpsys window windows | grep -E 'mCurrentFocus|mFocusedApp'");199 return result;200 }201 /**202 * get Current Focused Apk Package Name203 *204 * @return String205 */206 public String getCurrentFocusedApkPackageName() {207 String res = "";208 String txt = getCurrentDeviceFocus();209 String regEx1 = ".*?";210// String regEx2 = "((?:[a-z][a-z\\.\\d\\-]+)\\.(?:[a-z][a-z\\-]+))(?![\\w\\.])";211 Pattern pattern1 = Pattern.compile(regEx1 + regEx1, Pattern.CASE_INSENSITIVE | Pattern.DOTALL);212 Matcher matcher1 = pattern1.matcher(txt);213 if (matcher1.find()) {214 res = matcher1.group(1);215 }216 LOGGER.info("Found package name for application in focus : " + res);217 return res;218 }219 /**220 * get Current Focused Apk Details (apkPackage/apkActivity)221 * 222 * @return apkPackage/apkActivity to use it in openApp method.223 */224 public String getCurrentFocusedApkDetails() {225 try {226 String packageName = "";227 String activityName = "";228 String txt = getCurrentDeviceFocus();229 String regEx1 = ".*?";230 String regEx2 = "((?:[a-z][a-z\\.\\d\\-]+)\\.(?:[a-z][a-z\\-]+))(?![\\w\\.])";231 Pattern pattern1 = Pattern.compile(regEx1 + regEx2, Pattern.CASE_INSENSITIVE | Pattern.DOTALL);232 Matcher matcher1 = pattern1.matcher(txt);233 if (matcher1.find()) {234 packageName = matcher1.group(1);235 }236 LOGGER.info("Found package name for application in focus : " + packageName);237 String regEx3 = "\\/((?:[a-z][a-z\\.\\d\\-]+)\\.(?:[a-z][a-z\\-\\_]+))(?![\\w\\.])";238 Pattern pattern2 = Pattern.compile(regEx1 + regEx3, Pattern.CASE_INSENSITIVE | Pattern.DOTALL);239 Matcher matcher2 = pattern2.matcher(txt);240 if (matcher2.find()) {241 activityName = matcher2.group(1);242 }243 LOGGER.info("Found activity name for application in focus : " + activityName);244 return packageName + "/" + activityName;245 } catch (Exception e) {246 LOGGER.error(e);247 return "";248 }249 }250 /**251 * install android Apk by path to apk file.252 *253 * @param apkPath String254 */255 public void installApk(final String apkPath) {256 installApk(apkPath, false);257 }258 /**259 * install android Apk by path to apk or by name in classpath.260 *261 * @param apkPath String262 * @param inClasspath boolean263 */264 public void installApk(final String apkPath, boolean inClasspath) {265 String filePath = apkPath;266 if (inClasspath) {267 URL baseResource = ClassLoader.getSystemResource(apkPath);268 if (baseResource == null) {269 throw new RuntimeException("Unable to get resource from classpath: " + apkPath);270 } else {271 LOGGER.debug("Resource was found: " + baseResource.getPath());272 }273 String fileName = FilenameUtils.getBaseName(baseResource.getPath()) + "." + FilenameUtils.getExtension(baseResource.getPath());274 // make temporary copy of resource in artifacts folder275 filePath = ReportContext.getArtifactsFolder().getAbsolutePath() + File.separator + fileName;276 File file = new File(filePath);277 if (!file.exists()) {278 InputStream link = (ClassLoader.getSystemResourceAsStream(apkPath));279 try {280 Files.copy(link, file.getAbsoluteFile().toPath());281 } catch (IOException e) {282 LOGGER.error("Unable to extract resource from ClassLoader!", e);283 }284 }285 }286 executeAdbCommand("install " + filePath);287 }288 /**289 * Open Development Settings on device290 */291 public void openDeveloperOptions() {292 executeAdbCommand("shell am start -n com.android.settings/.DevelopmentSettings");293 }294 // End of Common Methods295 // Notification section296 /**297 * expandStatusBar298 */299 public void expandStatusBar() {300 executeAdbCommand("shell service call statusbar 1");301 }302 /**303 * collapseStatusBar304 */305 public void collapseStatusBar() {306 executeAdbCommand("shell service call statusbar 2");307 }308 // TODO: move notifications methods into separate class if possible. Maybe309 // declare notification service instance inside AndroidService310 /**311 * getNotifications312 *313 * @return List of Notification314 */315 public List<Notification> getNotifications() {316 return getNotifications(true);317 }318 /**319 * getNotifications320 *321 * @param withLogger boolean322 * @return List of Notification323 */324 public List<Notification> getNotifications(boolean withLogger) {325 String[] getNotificationsCmd = null;326 String deviceName = DevicePool.getDevice().getAdbName();327 if (!deviceName.isEmpty()) {328 getNotificationsCmd = CmdLine.insertCommandsAfter(baseInitCmd, "-s", deviceName, "shell", "dumpsys", "notification");329 } else {330 getNotificationsCmd = CmdLine.insertCommandsAfter(baseInitCmd, "shell", "dumpsys", "notification");331 }332 LOGGER.info("getNotifications cmd was built: " + CmdLine.arrayToString(getNotificationsCmd));333 // TODO: migrate to executeAbdCommand later334 List<Notification> resultList = new ArrayList<Notification>();335 List<String> notificationsOutput = executor.execute(getNotificationsCmd);336 Notification notification = new Notification();337 for (String output : notificationsOutput) {338 boolean found = false;339 Matcher matcher = NOTIFICATION_PATTERN.matcher(output);340 while (matcher.find()) {341 notification.setNotificationPkg(matcher.group(1));342 if (withLogger)343 LOGGER.info(matcher.group(1));344 }345 Matcher matcher2 = NOTIFICATION_TEXT_PATTERN.matcher(output);346 while (matcher2.find()) {347 notification.setNotificationText(matcher2.group(1));348 if (withLogger)349 LOGGER.info(matcher2.group(1));350 found = true;351 }352 if (found) {353 resultList.add(notification);354 if (withLogger)355 LOGGER.info(notification);356 notification = new Notification();357 found = false;358 }359 }360 if (withLogger)361 LOGGER.info("Found: " + resultList.size() + " notifications.");362 return resultList;363 }364 /**365 * notificationsCount366 *367 * @return notificationsCount368 */369 public int notificationsCount() {370 List<Notification> resultList = getNotifications(false);371 LOGGER.info("Found: " + resultList.size() + " notifications.");372 return resultList.size();373 }374 /**375 * isNotificationWithTextExist376 *377 * @param text String378 * @return boolean379 */380 public boolean isNotificationWithTextExist(String text) {381 List<Notification> resultList = getNotifications(false);382 for (Notification notify : resultList) {383 if (notify.getNotificationText().contains(text)) {384 LOGGER.info("Found '" + text + "' in notification '" + notify.getNotificationText() + "'.");385 return true;386 }387 }388 return false;389 }390 /**391 * waitUntilNewNotificationAppear392 *393 * @param text String394 * @param timeout long395 * @return boolean396 */397 public boolean waitUntilNewNotificationAppear(String text, long timeout) {398 // boolean found = false;399 int base = notificationsCount();400 int time = 0;401 boolean foundText = isNotificationWithTextExist(text);402 int actual = notificationsCount();403 while (actual <= base && ++time < timeout && !foundText) {404 LOGGER.info("Wait for notification. Second: " + time + ". Actual number:" + actual);405 CommonUtils.pause(1);406 actual = notificationsCount();407 foundText = isNotificationWithTextExist(text);408 }409 return (foundText);410 }411 /**412 * isNotificationPkgExist413 *414 * @param text package text415 * @return boolean416 */417 public boolean isNotificationPkgExist(String text) {418 List<Notification> resultList = getNotifications(false);419 for (Notification notify : resultList) {420 if (notify.getNotificationPkg().contains(text)) {421 LOGGER.info("Found '" + text + "' in notification packages '" + notify.getNotificationPkg() + "' with text '"422 + notify.getNotificationText() + "'.");423 return true;424 }425 }426 return false;427 }428 /**429 * waitUntilNewNotificationPackageAppear430 *431 * @param pkg String432 * @param timeout long433 * @return boolean434 */435 public boolean waitUntilNewNotificationPackageAppear(String pkg, long timeout) {436 // boolean found = false;437 int base = notificationsCount();438 int time = 0;439 boolean foundText = isNotificationPkgExist(pkg);440 int actual = notificationsCount();441 while (actual <= base && ++time < timeout && !foundText) {442 LOGGER.info("Wait for notification. Second: " + time + ". Actual number:" + actual);443 CommonUtils.pause(1);444 actual = notificationsCount();445 foundText = isNotificationPkgExist(pkg);446 }447 return (foundText);448 }449 /**450 * find Expected Notification with partial text451 *452 * @param expectedTitle String453 * @param expectedText String454 * @return boolean455 */456 public boolean findExpectedNotification(String expectedTitle, String expectedText) {457 return findExpectedNotification(expectedTitle, expectedText, true);458 }459 /**460 * find Expected Notification461 *462 * @param expectedTitle String463 * @param expectedText String464 * @param partially boolean465 * @return boolean466 */467 @SuppressWarnings("rawtypes")468 public boolean findExpectedNotification(String expectedTitle, String expectedText, boolean partially) {469 // open notification470 try {471 ((AndroidDriver) getDriver()).openNotifications();472 CommonUtils.pause(2); // wait while notifications are playing animation to473 // appear to avoid missed taps474 } catch (Exception e) {475 LOGGER.error(e);476 LOGGER.info("Using adb to expand Status bar. ");477 expandStatusBar();478 }479 NotificationPage nativeNotificationPage = new NotificationPage(getDriver());480 LOGGER.info("Native notification page is loaded: " + nativeNotificationPage.isNativeNotificationPage());481 int itemsListSize = nativeNotificationPage.getLastItemsContentSize();482 String title, text;483 int notificationItemNum = 0;484 for (int i = 0; i <= itemsListSize; i++) {485 title = nativeNotificationPage.getItemTitle(i);486 text = nativeNotificationPage.getItemText(i);487 LOGGER.info("Notification title is: " + title);488 LOGGER.info("Notification text is: " + text);489 if (!expectedTitle.isEmpty()) {490 if (title.equals(expectedTitle)) {491 notificationItemNum = i;492 LOGGER.info("Found expected title '" + expectedTitle + "' in notification #" + notificationItemNum);493 return true;494 } else if (partially) {495 if (expectedTitle.contains(title)) {496 notificationItemNum = i;497 LOGGER.info(498 "Found that expected title '" + expectedTitle + "' contains '" + title + "' in notification #" + notificationItemNum);499 return true;500 }501 }502 }503 if (!expectedText.isEmpty()) {504 if (text.equals(expectedText)) {505 notificationItemNum = i;506 LOGGER.info("Found expected text '" + expectedText + "' in notification #" + notificationItemNum);507 return true;508 } else if (partially) {509 if (expectedText.contains(text)) {510 notificationItemNum = i;511 LOGGER.info(512 "Found that expected text '" + expectedText + "' contains '" + text + "' in notification #" + notificationItemNum);513 return true;514 }515 }516 }517 }518 return false;519 }520 /**521 * clearNotifications522 */523 public void clearNotifications() {524 LOGGER.info("Clear notifications");525 NotificationPage notificationPage = new NotificationPage(getDriver());526 int attempts = 3;527 boolean isStatusBarOpened;528 // three attempts will be executed to clear notifications529 for (int i = 0; i < attempts; i++) {530 collapseStatusBar();531 expandStatusBar();532 // wait until status bar will be opened533 isStatusBarOpened = notificationPage.isOpened(INIT_TIMEOUT);534 if (!isStatusBarOpened) {535 LOGGER.info(String.format("Status bar isn't opened after %d seconds. One more attempt.", (int) INIT_TIMEOUT));536 expandStatusBar();537 }538 LOGGER.debug("Page source [expand status bar]: ".concat(getDriver().getPageSource()));539 Screenshot.capture(getDriver(), "Clear notification - screenshot. Status bar should be opened. Attempt: " + i);540 try {541 notificationPage.clearNotifications();542 } catch (Exception e) {543 LOGGER.info("Exception during notification extraction.");544 }545 }546 collapseStatusBar();547 }548 /**549 * isStatusBarExpanded550 *551 * @return boolean552 */553 public boolean isStatusBarExpanded() {554 NotificationPage notificationPage = new NotificationPage(getDriver());555 return notificationPage.isStatusBarExpanded();556 }557 // End of Notification section558 // Change Device Language section559 /**560 * change Android Device Language with default parameters561 *562 * @param language String563 * @return boolean564 */565 public boolean setDeviceLanguage(String language) {566 return setDeviceLanguage(language, true, 20);567 }568 /**569 * change Android Device Language570 * <p>571 * Url: <a href=572 * "http://play.google.com/store/apps/details?id=net.sanapeli.adbchangelanguage&hl=ru&rdid=net.sanapeli.adbchangelanguage">573 * ADBChangeLanguage apk </a> Change locale (language) of your device via574 * ADB (on Android OS version 6.0, 5.0, 4.4, 4.3, 4.2 and older). No need to575 * root your device! With ADB (Android Debug Bridge) on your computer, you576 * can fast switch the device locale to see how your application UI looks on577 * different languages. Usage: - install this app - setup adb connection to578 * your device (http://developer.android.com/tools/help/adb.html) - Android579 * OS 4.2 onwards (tip: you can copy the command here and paste it to your580 * command console): adb shell pm grant net.sanapeli.adbchangelanguage581 * android.permission.CHANGE_CONFIGURATION582 * <p>583 * English: adb shell am start -n584 * net.sanapeli.adbchangelanguage/.AdbChangeLanguage -e language en Russian:585 * adb shell am start -n net.sanapeli.adbchangelanguage/.AdbChangeLanguage586 * -e language ru Spanish: adb shell am start -n587 * net.sanapeli.adbchangelanguage/.AdbChangeLanguage -e language es588 *589 * @param language to set. Can be es, en, etc.590 * @param changeConfig boolean if true - update config locale and language591 * params592 * @param waitTime int wait in seconds before device refresh.593 * @return boolean594 */595 public boolean setDeviceLanguage(String language, boolean changeConfig, int waitTime) {596 boolean status = false;597 String initLanguage = language;598 String currentAndroidVersion = DevicePool.getDevice().getOsVersion();599 LOGGER.info("Do not concat language for Android. Keep: " + language);600 language = language.replace("_", "-");601 LOGGER.info("Refactor language to : " + language);602 String actualDeviceLanguage = getDeviceLanguage();603 if (language.contains(actualDeviceLanguage.toLowerCase()) || actualDeviceLanguage.toLowerCase().contains(language)) {604 LOGGER.info("Device already have expected language: " + actualDeviceLanguage);605 return true;606 }607 String setLocalizationChangePermissionCmd = "shell pm grant net.sanapeli.adbchangelanguage android.permission.CHANGE_CONFIGURATION";608 String setLocalizationCmd = "shell am start -n net.sanapeli.adbchangelanguage/.AdbChangeLanguage -e language " + language;609 LOGGER.info("Try set localization change permission with following cmd:" + setLocalizationChangePermissionCmd);610 String expandOutput = executeAdbCommand(setLocalizationChangePermissionCmd);611 if (expandOutput.contains("Unknown package: net.sanapeli.adbchangelanguage")) {612 LOGGER.info("Looks like 'ADB Change Language apk' is not installed. Install it and try again.");613 installApk(LANGUAGE_CHANGE_APP_PATH, true);614 expandOutput = executeAdbCommand(setLocalizationChangePermissionCmd);615 }616 LOGGER.info("Output after set localization change permission using 'ADB Change Language apk': " + expandOutput);617 LOGGER.info("Try set localization to '" + language + "' with following cmd: " + setLocalizationCmd);618 String changeLocaleOutput = executeAdbCommand(setLocalizationCmd);619 LOGGER.info("Output after set localization to '" + language + "' using 'ADB Change Language apk' : " + changeLocaleOutput);620 if (waitTime > 0) {621 LOGGER.info("Wait for at least '" + waitTime + "' seconds before device refresh.");622 CommonUtils.pause(waitTime);623 }624 if (changeConfig) {625 String loc;626 String lang;627 if (initLanguage.contains("_")) {628 lang = initLanguage.split("_")[0];629 loc = initLanguage.split("_")[1];630 } else {631 lang = initLanguage;632 loc = initLanguage;633 }634 LOGGER.info("Update config.properties locale to '" + loc + "' and language to '" + lang + "'.");635 R.CONFIG.put("locale", loc);636 R.CONFIG.put("language", lang);637 }638 actualDeviceLanguage = getDeviceLanguage();639 LOGGER.info("Actual Device Language: " + actualDeviceLanguage);640 if (language.contains(actualDeviceLanguage.toLowerCase()) || actualDeviceLanguage.toLowerCase().contains(language)) {641 status = true;642 } else {643 if (getDeviceLanguage().isEmpty()) {644 LOGGER.info("Adb return empty response without errors.");645 status = true;646 } else {647 currentAndroidVersion = DevicePool.getDevice().getOsVersion();648 LOGGER.info("currentAndroidVersion=" + currentAndroidVersion);649 if (currentAndroidVersion.contains("7.")) {650 LOGGER.info("Adb return language command do not work on some Android 7+ devices." + " Check that there are no error.");651 status = !getDeviceLanguage().toLowerCase().contains("error");652 }653 }654 }655 return status;656 }657 /**658 * getDeviceLanguage659 *660 * @return String661 */662 public String getDeviceLanguage() {663 String locale = executeAdbCommand("shell getprop persist.sys.language");664 if (locale.isEmpty()) {665 locale = executeAdbCommand("shell getprop persist.sys.locale");666 }667 return locale;668 }669 // End Language Change section670 // Fake GPS section671 /**672 * startFakeGPS to emulate GPS location673 *674 * @param location String - existing city (for ex. New York)675 * @return boolean return true if everything is ok.676 */677 public boolean setFakeGPSLocation(String location) {678 return setFakeGPSLocation(location, false);679 }680 /**681 * startFakeGPS to emulate GPS location682 *683 * @param location String - existing city (for ex. New York)684 * @param restartApk - if true DriverPool.restartDriver(true);685 * @return boolean return true if everything is ok.686 */687 public boolean setFakeGPSLocation(String location, boolean restartApk) {688 getDriver();689 boolean res = false;690 installApk(FAKE_GPS_APP_PATH, true);691 String activity = FAKE_GPS_APP_ACTIVITY;692 try {693 forceFakeGPSApkOpen();694 FakeGpsPage fakeGpsPage = new FakeGpsPage(getDriver());695 if (!fakeGpsPage.isOpened(1)) {696 LOGGER.error("Fake GPS application should be open but wasn't. Force opening.");697 openApp(activity);698 CommonUtils.pause(2);699 }700 res = fakeGpsPage.locationSearch(location);701 if (res) {702 LOGGER.info("Set Fake GPS locale: " + location);703 MobileUtils.hideKeyboard();704 fakeGpsPage.clickSetLocation();705 }706 res = true;707 if (restartApk)708 DriverPool.restartDriver(true);709 } catch (Exception e) {710 LOGGER.error("Exception: ", e);711 }712 return res;713 }714 /**715 * stopFakeGPS stop using Fake GPS716 *717 * @return boolean718 */719 public boolean stopFakeGPS() {720 return stopFakeGPS(false);721 }722 /**723 * stopFakeGPS stop using Fake GPS724 *725 * @param restartApk - if true DriverPool.restartDriver(true);726 * @return boolean727 */728 public boolean stopFakeGPS(boolean restartApk) {729 getDriver();730 boolean res = false;731 String activity = FAKE_GPS_APP_ACTIVITY;732 try {733 forceFakeGPSApkOpen();734 FakeGpsPage fakeGpsPage = new FakeGpsPage(getDriver());735 if (!fakeGpsPage.isOpened(1)) {736 LOGGER.error("Fake GPS application should be open but wasn't. Force opening.");737 openApp(activity);738 CommonUtils.pause(2);739 }740 LOGGER.info("STOP Fake GPS locale");741 res = fakeGpsPage.clickStopFakeGps();742 if (restartApk)743 DriverPool.restartDriver(true);744 } catch (Exception e) {745 LOGGER.error("Exception: ", e);746 }747 LOGGER.info("Stop Fake GPS button was clicked: " + res);748 return res;749 }750 /**751 * forceFakeGPSApkOpen752 *753 * @return boolean754 */755 private boolean forceFakeGPSApkOpen() {756 return forceApkOpen(FAKE_GPS_APP_ACTIVITY, FAKE_GPS_APP_PACKAGE, FAKE_GPS_APP_PATH);757 }758 /**759 * forceApkOpen760 *761 * @param activity String762 * @param packageName String763 * @param apkPath String764 * @return boolean765 */766 private boolean forceApkOpen(String activity, String packageName, String apkPath) {767 boolean res;768 int attemps = 3;769 boolean isApkOpened = checkCurrentDeviceFocus(packageName);770 while (!isApkOpened && attemps > 0) {771 LOGGER.info("Apk was not open. Attempt to open...");772 openApp(activity);773 CommonUtils.pause(2);774 isApkOpened = checkCurrentDeviceFocus(packageName);775 attemps--;776 }777 if (!isApkOpened) {778 LOGGER.info("Probably APK was not installed correctly. Try to reinstall.");779 installApk(apkPath, true);780 openApp(activity);781 CommonUtils.pause(2);782 }783 if (checkCurrentDeviceFocus(packageName)) {784 LOGGER.info("On '" + packageName + "' apk page");785 res = true;786 } else {787 LOGGER.error("Not on '" + packageName + "' page after all tries. Please check logs.");788 res = false;789 }790 return res;791 }792 // End of Fake GPS section793 // TimeZone change section794 /**795 * switchDeviceAutoTimeAndTimeZone796 *797 * @param autoSwitch boolean. If true - auto Time and TimeZone will be set798 * as On.799 */800 public void switchDeviceAutoTimeAndTimeZone(boolean autoSwitch) {801 String value = "0";802 if (autoSwitch) {803 value = "1";804 }805 executeAdbCommand("shell settings put global auto_time " + value);806 executeAdbCommand("shell settings put global auto_time_zone " + value);807 }808 /**809 * get Device Time Zone810 *811 * @return DeviceTimeZone812 */813 public DeviceTimeZone getDeviceTimeZone() {814 return getDeviceTimeZone("");815 }816 /**817 * get Device Time Zone. Set default TimeZone818 *819 * @param defaultTZ - default string.820 * @return DeviceTimeZone821 */822 public DeviceTimeZone getDeviceTimeZone(String defaultTZ) {823 getDriver(); // start driver in before class to assign it for particular824 // thread825 DeviceTimeZone dt = new DeviceTimeZone();826 String value = executeAdbCommand("shell settings get global auto_time");827 if (value.contains("0")) {828 dt.setAutoTime(false);829 } else {830 dt.setAutoTime(true);831 }832 value = executeAdbCommand("shell settings get global auto_time_zone");833 if (value.contains("0")) {834 dt.setAutoTimezone(false);835 } else {836 dt.setAutoTimezone(true);837 }838 value = executeAdbCommand("shell settings get system time_12_24");839 if (value.contains("12")) {840 dt.setTimeFormat(TimeFormat.FORMAT_12);841 } else {842 dt.setTimeFormat(TimeFormat.FORMAT_24);843 }844 if (defaultTZ.isEmpty()) {845 value = executeAdbCommand("shell getprop persist.sys.timezone");846 if (!value.isEmpty()) {847 dt.setTimezone(value);848 }849 } else {850 dt.setTimezone(defaultTZ);851 }852 value = executeAdbCommand("shell date -s %mynow%");853 LOGGER.info(value);854 if (!value.isEmpty()) {855 value = convertDateInCorrectString(parseOutputDate(value));856 dt.setSetDeviceDateTime(value);857 LOGGER.info(value);858 }859 dt.setChangeDateTime(false);860 dt.setRefreshDeviceTime(true);861 LOGGER.info(dt.toString());862 return dt;863 }864 /**865 * get Device Actual TimeZone866 *867 * @return String868 */869 public String getDeviceActualTimeZone() {870 String value = executeAdbCommand("shell getprop persist.sys.timezone");871 if (!value.isEmpty()) {872 LOGGER.info(value);873 }874 return value;875 }876 // Start of TimeZone Setting section877 /**878 * set Device TimeZone by using Apk879 *880 * @param timeZone String required timeZone in Android standard format881 * (Europe/London)882 * @param timeFormat String 12 or 24883 * @return boolean884 */885 public boolean setDeviceTimeZone(String timeZone, TimeFormat timeFormat) {886 return setDeviceTimeZone(timeZone, "", timeFormat, ChangeTimeZoneWorkflow.APK);887 }888 /**889 * set Device TimeZone using all supported workflows. By ADB, Settings and890 * Apk891 *892 * @param timeZone String required timeZone893 * @param timeFormat String 12 or 24894 * @param settingsTZ TimeFormat895 * @return boolean896 */897 public boolean setDeviceTimeZone(String timeZone, String settingsTZ, TimeFormat timeFormat) {898 return setDeviceTimeZone(timeZone, settingsTZ, timeFormat, ChangeTimeZoneWorkflow.ALL);899 }900 /**901 * set Device TimeZone. By required workflow: ADB, Settings or APK902 *903 * @param timeZone String required timeZone904 * @param timeFormat String 12 or 24905 * @param settingsTZ TimeFormat906 * @param workflow ChangeTimeZoneWorkflow907 * @return boolean908 */909 public boolean setDeviceTimeZone(String timeZone, String settingsTZ, TimeFormat timeFormat, ChangeTimeZoneWorkflow workflow) {910 boolean changed = false;911 getDriver(); // start driver in before class to assign it for particular912 // thread913 String actualTZ = getDeviceActualTimeZone();914 if (isRequiredTimeZone(actualTZ, timeZone)) {915 LOGGER.info("Required TimeZone is already set.");916 return true;917 }918 String currentAndroidVersion = DevicePool.getDevice().getOsVersion();919 LOGGER.info("currentAndroidVersion=" + currentAndroidVersion);920 if (currentAndroidVersion.contains("7.") ||921 (DevicePool.getDevice().getDeviceType() == DeviceType.Type.ANDROID_TABLET && !currentAndroidVersion.contains("8."))) {922 LOGGER.info("TimeZone changing for Android 7+ and tablets works only by TimeZone changer apk.");923 workflow = ChangeTimeZoneWorkflow.APK;924 }925 // Solution for ADB timezone changing.926 if (ChangeTimeZoneWorkflow.ADB.isSupported(workflow)) {927 LOGGER.info("Try to change TimeZone by ADB");928 LOGGER.info(setDeviceTimeZoneByADB(timeZone, timeFormat, ""));929 changed = applyTZChanges(ChangeTimeZoneWorkflow.ADB, timeZone);930 }931 // Solution for timezone changing by device Settings. (Tested on S7,932 // Note 3, S6, S5).933 if (!changed && ChangeTimeZoneWorkflow.SETTINGS.isSupported(workflow)) {934 LOGGER.info("Try to change TimeZone by Device Settings");935 setDeviceTimeZoneBySetting(timeZone, settingsTZ, timeFormat);936 changed = applyTZChanges(ChangeTimeZoneWorkflow.SETTINGS, timeZone);937 }938 // Solution for using TimeZone Changer apk.939 if (!changed && ChangeTimeZoneWorkflow.APK.isSupported(workflow)) {940 LOGGER.info("Try to change TimeZone by TimeZone Changer apk.");941 setDeviceTimeZoneByChangerApk(timeZone, timeFormat);942 changed = applyTZChanges(ChangeTimeZoneWorkflow.APK, timeZone);943 }944 return changed;945 }946 // End of TimeZone change sections947 /**948 * Open camera on device949 */950 public void openCamera() {951 LOGGER.info("Camera will be opened");952 executeAdbCommand("shell am start -a android.media.action.IMAGE_CAPTURE");953 }954 /**955 * Android camera should be already opened956 */957 public void takePhoto() {958 LOGGER.info("Will take photo");959 executeAdbCommand("shell input keyevent KEYCODE_CAMERA");960 }961 // Private section962 // TimeZone Private methods963 /**964 * setDeviceTimeZoneByADB965 *966 * @param timeZone String967 * @param timeFormat TimeFormat968 * @param deviceSetDate String in format yyyyMMdd.HHmmss. Can be empty.969 * @return String970 */971 private String setDeviceTimeZoneByADB(String timeZone, TimeFormat timeFormat, String deviceSetDate) {972 boolean changeDateTime = true;973 String tzGMT = "";974 if (deviceSetDate.isEmpty()) {975 changeDateTime = false;976 }977 DeviceTimeZone dt = new DeviceTimeZone(false, false, timeFormat, timeZone, tzGMT, deviceSetDate, changeDateTime, true);978 return setDeviceTimeZoneByADB(dt);979 }980 /**981 * setDeviceTimeZoneByADB Automatic date and time = OFF (settings - date and982 * time) adb shell settings put global auto_time 0 Automatic time zone = OFF983 * (settings - date and time) adb shell settings put global auto_time_zone 0984 * <p>985 * Set Time Zone on device adb shell setprop persist.sys.timezone986 * "America/Chicago"987 * <p>988 * Check timezones: <a href=989 * "https://en.wikipedia.org/wiki/List_of_tz_database_time_zones">List_of_tz_database_time_zones</a>990 * <p>991 * Check time on device adb shell date -s %mynow%992 * <p>993 * Restart application994 *995 * @param dt DeviceTimeZone996 * @return String actual Device Date and Time997 */998 private String setDeviceTimeZoneByADB(DeviceTimeZone dt) {999 if (dt == null) {1000 LOGGER.error("DeviceTimeZone is not initialised.");1001 dt = new DeviceTimeZone();1002 }1003 LOGGER.info(dt.toString());1004 String autoTime = "0";1005 String autoTimeZone = "0";1006 if (dt.isAutoTime()) {1007 autoTime = "1";1008 }1009 executeAdbCommand("shell settings put global auto_time " + autoTime);1010 if (dt.isAutoTimezone()) {1011 autoTimeZone = "1";1012 }1013 executeAdbCommand("shell settings put global auto_time_zone " + autoTimeZone);1014 setSystemTime(dt.getTimeFormat());1015 if (!dt.getTimezone().isEmpty()) {1016 executeAdbCommand("shell setprop persist.sys.timezone \"" + dt.getTimezone() + "\"");1017 }1018 if (dt.isRefreshDeviceTime()) {1019 executeAdbCommand("shell am broadcast -a android.intent.action.TIME_SET");1020 }1021 if (dt.isChangeDateTime() && !dt.getSetDeviceDateTime().isEmpty()) {1022 // Try to set date for device but it will not work on not rooted1023 // devices1024 executeAdbCommand("shell date " + dt.getSetDeviceDateTime());1025 }1026 String actualDT = executeAdbCommand("shell date -s %mynow%");1027 LOGGER.info(actualDT);1028 return actualDT;1029 }1030 /**1031 * setDeviceTimeZoneBySetting1032 *1033 * @param timeZone String1034 * @param settingsTZ String1035 * @param timeFormat TimeFormat1036 */1037 private void setDeviceTimeZoneBySetting(String timeZone, String settingsTZ, TimeFormat timeFormat) {1038 String actualTZ = getDeviceActualTimeZone();1039 String tz = DeviceTimeZone.getTimezoneOffset(timeZone);1040 if (isRequiredTimeZone(actualTZ, timeZone)) {1041 LOGGER.info("Required timeZone is already set.");1042 return;1043 }1044 try {1045 openDateTimeSettingsSetupWizard(true, timeFormat);1046 String res = getCurrentDeviceFocus();1047 if (res.contains(".Settings$DateTimeSettingsActivity")) {1048 LOGGER.info("On '.Settings$DateTimeSettingsActivity' page");1049 } else {1050 LOGGER.error("Not on '.Settings$DateTimeSettingsActivity' page");1051 }1052 DateTimeSettingsPage dtSettingsPage = new DateTimeSettingsPage(getDriver());1053 if (!dtSettingsPage.isOpened(3)) {1054 openDateTimeSettingsSetupWizard(true, timeFormat);1055 }1056 if (dtSettingsPage.isOpened(3)) {1057 LOGGER.info("Date Time Settings page was open.");1058 } else {1059 LOGGER.error("Date Time Settings page should be open.");1060 }1061 dtSettingsPage.openTimeZoneSetting();1062 dtSettingsPage.selectTimeZone(tz, settingsTZ);1063 dtSettingsPage.clickNextButton();1064 } catch (Exception e) {1065 LOGGER.error("Exception: ", e);1066 }1067 }1068 /**1069 * setDeviceTimeZoneByChangerApk1070 *1071 * @param timeZone String1072 * @param timeFormat TimeFormat1073 */1074 private void setDeviceTimeZoneByChangerApk(String timeZone, TimeFormat timeFormat) {1075 String actualTZ = getDeviceActualTimeZone();1076 String tz = DeviceTimeZone.getTimezoneOffset(timeZone);1077 LOGGER.info("Required TimeZone offset: " + tz);1078 if (isRequiredTimeZone(actualTZ, timeZone)) {1079 LOGGER.info("Required timeZone is already set.");1080 return;1081 }1082 installApk(TZ_CHANGE_APP_PATH, true);1083 try {1084 forceTZChangingApkOpen(true, timeFormat);1085 TZChangerPage tzChangerPage = new TZChangerPage(getDriver());1086 if (tzChangerPage.isOpened(3)) {1087 LOGGER.info("TimeZone changer main page was open.");1088 } else {1089 LOGGER.error("TimeZone changer main page should be open. Retry to open.");1090 openTZChangingApk(true, timeFormat);1091 }1092 tzChangerPage.selectTimeZone(timeZone);1093 } catch (Exception e) {1094 LOGGER.error("Exception: ", e);1095 }1096 }1097 private boolean applyTZChanges(ChangeTimeZoneWorkflow workflow, String expectedZone) {1098 boolean res = false;1099 String actualTZ = getDeviceActualTimeZone();1100 if (isRequiredTimeZone(actualTZ, expectedZone)) {1101 LOGGER.info("Required timeZone '" + expectedZone + "' was set by " + workflow.toString() + ". Restarting driver to apply changes.");1102 DriverPool.restartDriver(true);1103 res = true;1104 } else {1105 LOGGER.error("TimeZone was not changed by " + workflow.toString() + ". Actual TZ is: " + actualTZ);1106 }1107 return res;1108 }1109 /**1110 * comparingExpectedAndActualTZ1111 *1112 * @param actualTZ String1113 * @param expextedTZ String1114 * @return boolean1115 */1116 private boolean isRequiredTimeZone(String actualTZ, String expextedTZ) {1117 boolean res = actualTZ.equals(expextedTZ);1118 if (!res) {1119 String[] actTZ = actualTZ.split("/");...

Full Screen

Full Screen

toString

Using AI Code Generation

copy

Full Screen

1String notificationText = notification.toString();2notificationText = notification.getText();3String notificationIcon = notification.getIcon();4String notificationPackage = notification.getPackage();5String notificationTicker = notification.getTicker();6String notificationTitle = notification.getTitle();7String notificationWhen = notification.getWhen();8String notificationWhen = notification.getWhen();9String notificationExtras = notification.getExtras();10String notificationPriority = notification.getPriority();11String notificationVisibility = notification.getVisibility();12String notificationChannelId = notification.getChannelId();13String notificationChannelName = notification.getChannelName();

Full Screen

Full Screen

toString

Using AI Code Generation

copy

Full Screen

1public class AppiumAndroidNotificationTest extends AbstractTest {2 @Test(description = "JIRA#DEMO-0001")3 @MethodOwner(owner = "qpsdemo")4 public void testAndroidNotification() {5 String appName = "com.qaprosoft.carina.demo:id/app_name";6 String notificationTitle = "com.qaprosoft.carina.demo:id/notificationTitle";7 String notificationText = "com.qaprosoft.carina.demo:id/notificationText";8 String notificationTime = "com.qaprosoft.carina.demo:id/notificationTime";9 String notificationIcon = "com.qaprosoft.carina.demo:id/notificationIcon";10 String notification = "com.qaprosoft.carina.demo:id/notification";11 String notificationText1 = "com.qaprosoft.carina.demo:id/notificationText1";12 String notificationTime1 = "com.qaprosoft.carina.demo:id/notificationTime1";13 String notificationIcon1 = "com.qaprosoft.carina.demo:id/notificationIcon1";14 String notification1 = "com.qaprosoft.carina.demo:id/notification1";15 String notificationText2 = "com.qaprosoft.carina.demo:id/notificationText2";16 String notificationTime2 = "com.qaprosoft.carina.demo:id/notificationTime2";17 String notificationIcon2 = "com.qaprosoft.carina.demo:id/notificationIcon2";18 String notification2 = "com.qaprosoft.carina.demo:id/notification2";19 String notificationText3 = "com.qaprosoft.carina.demo:id/notificationText3";20 String notificationTime3 = "com.qaprosoft.carina.demo:id/notificationTime3";21 String notificationIcon3 = "com.qaprosoft.carina.demo:id/notificationIcon3";22 String notification3 = "com.qaprosoft.carina.demo:id/notification3";23 String notificationText4 = "com.qaprosoft.carina.demo:id/notificationText4";24 String notificationTime4 = "com.qaprosoft.carina.demo:id/notificationTime4";25 String notificationIcon4 = "com.qaprosoft.carina.demo:id/notificationIcon4";26 String notification4 = "com.qaprosoft.carina.demo:id/notification4";27 String notificationText5 = "com.qaprosoft.carina.demo:id/notificationText5";28 String notificationTime5 = "com.qaprosoft.carina.demo:id/notificationTime5";

Full Screen

Full Screen

toString

Using AI Code Generation

copy

Full Screen

1public void testNotification() {2 Notification notification = new Notification();3 String notificationText = notification.toString();4 Assert.assertTrue(notificationText.contains("text you want to assert"));5}6public void testNotification() {7 Notification notification = new Notification();8 String notificationText = notification.toString();9 Assert.assertTrue(notificationText.contains("text you want to assert"));10}11public void testNotification() {12 Notification notification = new Notification();13 String notificationText = notification.toString();14 Assert.assertTrue(notificationText.contains("text you want to assert"));15}16public void testNotification() {17 Notification notification = new Notification();18 String notificationText = notification.toString();19 Assert.assertTrue(notificationText.contains("text you want to assert"));20}21public void testNotification() {22 Notification notification = new Notification();23 String notificationText = notification.toString();24 Assert.assertTrue(notificationText.contains("text you want to assert"));25}26public void testNotification() {27 Notification notification = new Notification();28 String notificationText = notification.toString();29 Assert.assertTrue(notificationText.contains("text you want to assert"));30}31public void testNotification() {32 Notification notification = new Notification();33 String notificationText = notification.toString();34 Assert.assertTrue(notificationText.contains("text you want to assert"));35}36public void testNotification() {

Full Screen

Full Screen

toString

Using AI Code Generation

copy

Full Screen

1package com.qaprosoft.carina.demo.mobile.gui.pages.android;2import com.qaprosoft.carina.core.foundation.utils.mobile.notifications.android.Notification;3import org.openqa.selenium.WebDriver;4import org.openqa.selenium.support.FindBy;5import org.openqa.selenium.support.events.EventFiringWebDriver;6import org.openqa.selenium.support.ui.ExpectedConditions;7import org.openqa.selenium.support.ui.WebDriverWait;8import org.testng.Assert;9public class NotificationPage extends CommonPage {10 @FindBy(id = "com.qaprosoft.carina.demo:id/notification")11 private Notification notification;12 public NotificationPage(WebDriver driver) {13 super(driver);14 }15 public void verifyNotification() {16 Assert.assertEquals(notification.getTitle(), "Notification");17 Assert.assertEquals(notification.getText(), "Hello World!");18 }19}20[INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ carina-demo ---

Full Screen

Full Screen

toString

Using AI Code Generation

copy

Full Screen

1import com.qaprosoft.carina.core.foundation.utils.mobile.notifications.android.Notification2import com.qaprosoft.carina.core.foundation.utils.mobile.notifications.android.NotificationManager3import com.qaprosoft.carina.core.foundation.utils.mobile.notifications.android.NotificationManager4def notifications = NotificationManager().getNotifications()5notifications.each {6println it.toString()7}

Full Screen

Full Screen

toString

Using AI Code Generation

copy

Full Screen

1String notificationText = notification.toString();2String notificationText = notification.toString("en");3String notificationTitle = notification.getNotificationTitle();4String notificationTitle = notification.getNotificationTitle("en");5String notificationText = notification.getNotificationText();6String notificationText = notification.getNotificationText("en");7String notificationSubText = notification.getNotificationSubText();8String notificationSubText = notification.getNotificationSubText("en");9String notificationInfoText = notification.getNotificationInfoText();10String notificationInfoText = notification.getNotificationInfoText("en");11String notificationBigText = notification.getNotificationBigText();12String notificationBigText = notification.getNotificationBigText("en");

Full Screen

Full Screen

toString

Using AI Code Generation

copy

Full Screen

1String notificationString = notification.toString();2System.out.println("Notification string: " + notificationString);3String notificationString = notification.toString();4System.out.println("Notification string: " + notificationString);5String notificationString = notification.toString();6System.out.println("Notification string: " + notificationString);

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Carina automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful