Best Carina code snippet using com.qaprosoft.carina.core.foundation.utils.android.AndroidService.isSupported
Source:AndroidService.java
...53 }54 public int getWorkflow() {55 return workflow;56 }57 public boolean isSupported(ChangeTimeZoneWorkflow workflow) {58 return (this.workflow & workflow.getWorkflow()) > 0;59 }60 }61 private static AndroidService instance;62 private AndroidService() {63 executor = new AdbExecutor();64 baseInitCmd = executor.getDefaultCmd();65 }66 static {67 try {68 instance = new AndroidService();69 } catch (Exception e) {70 throw new RuntimeException("Exception occurred in creating singleton AndroidService!");71 }72 }73 public static AndroidService getInstance() {74 return instance;75 }76 // Common methods77 /**78 * executeAbdCommand79 *80 * @param command String81 * @return String command output in one line82 */83 public String executeAbdCommand(String command) {84 String udid = DevicePool.getDeviceUdid();85 if (!udid.isEmpty()) {86 // add udid reference87 command = "-s " + udid + " " + command;88 }89 String result = "";90 LOGGER.info("Command: " + command);91 String[] listOfCommands = command.split(" ");92 String[] execCmd = CmdLine.insertCommandsAfter(baseInitCmd, listOfCommands);93 try {94 LOGGER.info("Try to execute following cmd: " + CmdLine.arrayToString(execCmd));95 List<String> execOutput = executor.execute(execCmd);96 LOGGER.info("Output after execution ADB command: " + execOutput);97 result = execOutput.toString().replaceAll("\\[|\\]", "").replaceAll(", ", " ").trim();98 LOGGER.info("Returning Output: " + result);99 } catch (Exception e) {100 LOGGER.error(e);101 }102 return result;103 }104 /**105 * openApp106 *107 * @param pkg String108 * @param activity String109 */110 public void openApp(String pkg, String activity) {111 openApp(pkg.trim() + "/" + activity.trim());112 }113 /**114 * openApp115 *116 * @param app String117 */118 public void openApp(String app) {119 executeAbdCommand("shell am start -n " + app);120 }121 /**122 * clear Apk Cache123 *124 * @param appPackageName for example: com.bamnetworks.mobile.android.gameday.atbat125 * @return boolean126 */127 public boolean clearApkCache(String appPackageName) {128 //Later can be used:129 /*130 String packageName = executor.getApkPackageName(String apkFile);131 executor.clearAppData(Device device, String appPackage);132 */133 String result = executeAbdCommand("shell pm clear " + appPackageName);134 if (result.contains("Success")) {135 LOGGER.info("Cache was cleared correctly");136 return true;137 } else {138 LOGGER.error("Cache was not cleared. May be application does not exist on this device.");139 return false;140 }141 }142 /**143 * checkCurrentDeviceFocus - return actual device focused apk and compare with expected.144 *145 * @param apk String146 * @return boolean147 */148 public boolean checkCurrentDeviceFocus(String apk) {149 String res = getCurrentDeviceFocus();150 if (res.contains(apk)) {151 LOGGER.info("Actual device focus is as expected and contains package or activity: '" + apk + "'.");152 return true;153 } else {154 LOGGER.error("Not expected apk '" + apk + "' is in focus. Actual result is: " + res);155 return false;156 }157 }158 /**159 * getCurrentDeviceFocus - get actual device apk in focus160 *161 * @return String162 */163 public String getCurrentDeviceFocus() {164 String result = executeAbdCommand("shell dumpsys window windows | grep -E 'mCurrentFocus|mFocusedApp'");165 return result;166 }167 /**168 * install android Apk by path to apk file.169 *170 * @param apkPath String171 */172 public void installApk(final String apkPath) {173 installApk(apkPath, false);174 }175 /**176 * install android Apk by path to apk or by name in classpath.177 *178 * @param apkPath String179 * @param inClasspath boolean180 */181 public void installApk(final String apkPath, boolean inClasspath) {182 String filePath = apkPath;183 if (inClasspath) {184 URL baseResource = ClassLoader.getSystemResource(apkPath);185 if (baseResource == null) {186 throw new RuntimeException("Unable to get resource from classpath: " + apkPath);187 } else {188 LOGGER.debug("Resource was found: " + baseResource.getPath());189 }190 String fileName = FilenameUtils.getBaseName(baseResource.getPath()) + "."191 + FilenameUtils.getExtension(baseResource.getPath());192 // make temporary copy of resource in artifacts folder193 filePath = ReportContext.getArtifactsFolder().getAbsolutePath() + File.separator + fileName;194 File file = new File(filePath);195 if (!file.exists()) {196 InputStream link = (ClassLoader.getSystemResourceAsStream(apkPath));197 try {198 Files.copy(link, file.getAbsoluteFile().toPath());199 } catch (IOException e) {200 LOGGER.error("Unable to extract resource from ClassLoader!", e);201 }202 }203 }204 executeAbdCommand("install " + filePath);205 }206 /**207 * Open Development Settings on device208 */209 public void openDeveloperOptions() {210 executeAbdCommand("shell am start -n com.android.settings/.DevelopmentSettings");211 }212 // End of Common Methods213 //Notification section214 /**215 * expandStatusBar216 */217 public void expandStatusBar() {218 executeAbdCommand("shell service call statusbar 1");219 }220 /**221 * collapseStatusBar222 */223 public void collapseStatusBar() {224 executeAbdCommand("shell service call statusbar 2");225 }226 //TODO: move notifications methods into separate class if possible. Maybe declare notification service instance inside AndroidService 227 /**228 * getNotifications229 *230 * @return List of Notification231 */232 public List<Notification> getNotifications() {233 return getNotifications(true);234 }235 /**236 * getNotifications237 *238 * @param withLogger boolean239 * @return List of Notification240 */241 public List<Notification> getNotifications(boolean withLogger) {242 String[] getNotificationsCmd = null;243 String udid = DevicePool.getDeviceUdid();244 if (!udid.isEmpty()) {245 getNotificationsCmd = CmdLine.insertCommandsAfter(baseInitCmd, "-s", udid, "shell", "dumpsys",246 "notification");247 } else {248 getNotificationsCmd = CmdLine.insertCommandsAfter(baseInitCmd, "shell", "dumpsys", "notification");249 }250 LOGGER.info("getNotifications cmd was built: " + CmdLine.arrayToString(getNotificationsCmd));251 //TODO: migrate to executeAbdCommand later252 List<Notification> resultList = new ArrayList<Notification>();253 List<String> notificationsOutput = executor.execute(getNotificationsCmd);254 Notification notification = new Notification();255 for (String output : notificationsOutput) {256 boolean found = false;257 Matcher matcher = NOTIFICATION_PATTERN.matcher(output);258 while (matcher.find()) {259 notification.setNotificationPkg(matcher.group(1));260 if (withLogger)261 LOGGER.info(matcher.group(1));262 }263 Matcher matcher2 = NOTIFICATION_TEXT_PATTERN.matcher(output);264 while (matcher2.find()) {265 notification.setNotificationText(matcher2.group(1));266 if (withLogger)267 LOGGER.info(matcher2.group(1));268 found = true;269 }270 if (found) {271 resultList.add(notification);272 if (withLogger)273 LOGGER.info(notification);274 notification = new Notification();275 found = false;276 }277 }278 if (withLogger)279 LOGGER.info("Found: " + resultList.size() + " notifications.");280 return resultList;281 }282 /**283 * notificationsCount284 *285 * @return notificationsCount286 */287 public int notificationsCount() {288 List<Notification> resultList = getNotifications(false);289 LOGGER.info("Found: " + resultList.size() + " notifications.");290 return resultList.size();291 }292 /**293 * isNotificationWithTextExist294 *295 * @param text String296 * @return boolean297 */298 public boolean isNotificationWithTextExist(String text) {299 List<Notification> resultList = getNotifications(false);300 for (Notification notify : resultList) {301 if (notify.getNotificationText().contains(text)) {302 LOGGER.info("Found '" + text + "' in notification '" + notify.getNotificationText() + "'.");303 return true;304 }305 }306 return false;307 }308 /**309 * waitUntilNewNotificationAppear310 *311 * @param text String312 * @param timeout long313 * @return boolean314 */315 public boolean waitUntilNewNotificationAppear(String text, long timeout) {316 //boolean found = false;317 int base = notificationsCount();318 int time = 0;319 boolean foundText = isNotificationWithTextExist(text);320 int actual = notificationsCount();321 while (actual <= base && ++time < timeout && !foundText) {322 LOGGER.info("Wait for notification. Second: " + time + ". Actual number:" + actual);323 pause(1);324 actual = notificationsCount();325 foundText = isNotificationWithTextExist(text);326 }327 return (foundText);328 }329 /**330 * isNotificationPkgExist331 *332 * @param text package text333 * @return boolean334 */335 public boolean isNotificationPkgExist(String text) {336 List<Notification> resultList = getNotifications(false);337 for (Notification notify : resultList) {338 if (notify.getNotificationPkg().contains(text)) {339 LOGGER.info("Found '" + text + "' in notification packages '" + notify.getNotificationPkg() + "' with text '" + notify.getNotificationText() + "'.");340 return true;341 }342 }343 return false;344 }345 /**346 * waitUntilNewNotificationPackageAppear347 *348 * @param pkg String349 * @param timeout long350 * @return boolean351 */352 public boolean waitUntilNewNotificationPackageAppear(String pkg, long timeout) {353 //boolean found = false;354 int base = notificationsCount();355 int time = 0;356 boolean foundText = isNotificationPkgExist(pkg);357 int actual = notificationsCount();358 while (actual <= base && ++time < timeout && !foundText) {359 LOGGER.info("Wait for notification. Second: " + time + ". Actual number:" + actual);360 pause(1);361 actual = notificationsCount();362 foundText = isNotificationPkgExist(pkg);363 }364 return (foundText);365 }366 /**367 * find Expected Notification with partial text368 *369 * @param expectedTitle String370 * @param expectedText String371 * @return boolean372 */373 public boolean findExpectedNotification(String expectedTitle, String expectedText) {374 return findExpectedNotification(expectedTitle, expectedText, true);375 }376 /**377 * find Expected Notification378 *379 * @param expectedTitle String380 * @param expectedText String381 * @param partially boolean382 * @return boolean383 */384 @SuppressWarnings("rawtypes")385 public boolean findExpectedNotification(String expectedTitle, String expectedText, boolean partially) {386 //open notification387 try {388 ((AndroidDriver) getDriver()).openNotifications();389 pause(2); //wait while notifications are playing animation to appear to avoid missed taps390 } catch (Exception e) {391 LOGGER.error(e);392 LOGGER.info("Using adb to expand Status bar. ");393 expandStatusBar();394 }395 NotificationPage nativeNotificationPage = new NotificationPage(getDriver());396 LOGGER.info("Native notification page is loaded: " + nativeNotificationPage.isNativeNotificationPage());397 int itemsListSize = nativeNotificationPage.getLastItemsContentSize();398 String title, text;399 int notificationItemNum = 0;400 for (int i = 0; i <= itemsListSize; i++) {401 title = nativeNotificationPage.getItemTitle(i);402 text = nativeNotificationPage.getItemText(i);403 LOGGER.info("Notification title is: " + title);404 LOGGER.info("Notification text is: " + text);405 if (!expectedTitle.isEmpty()) {406 if (title.equals(expectedTitle)) {407 notificationItemNum = i;408 LOGGER.info("Found expected title '" + expectedTitle + "' in notification #" + notificationItemNum);409 return true;410 } else if (partially) {411 if (expectedTitle.contains(title)) {412 notificationItemNum = i;413 LOGGER.info("Found that expected title '" + expectedTitle + "' contains '" + title + "' in notification #" + notificationItemNum);414 return true;415 }416 }417 }418 if (!expectedText.isEmpty()) {419 if (text.equals(expectedText)) {420 notificationItemNum = i;421 LOGGER.info("Found expected text '" + expectedText + "' in notification #" + notificationItemNum);422 return true;423 } else if (partially) {424 if (expectedText.contains(text)) {425 notificationItemNum = i;426 LOGGER.info("Found that expected text '" + expectedText + "' contains '" + text + "' in notification #" + notificationItemNum);427 return true;428 }429 }430 }431 }432 return false;433 }434 /**435 * clearNotifications436 */437 public void clearNotifications() {438 LOGGER.info("Clear notifications");439 NotificationPage notificationPage = new NotificationPage(getDriver());440 int attempts = 3;441 boolean isStatusBarOpened;442 // three attempts will be executed to clear notifications443 for (int i = 0; i < attempts; i++) {444 collapseStatusBar();445 expandStatusBar();446 // wait until status bar will be opened447 isStatusBarOpened = notificationPage.isOpened(INIT_TIMEOUT);448 if (!isStatusBarOpened) {449 LOGGER.info(String.format("Status bar isn't opened after %d seconds. One more attempt.",450 (int) INIT_TIMEOUT));451 expandStatusBar();452 }453 LOGGER.debug("Page source [expand status bar]: ".concat(454 getDriver().getPageSource()));455 Screenshot.capture(getDriver(),456 "Clear notification - screenshot. Status bar should be opened. Attempt: " + i);457 try {458 notificationPage.clearNotifications();459 } catch (Exception e) {460 LOGGER.info("Exception during notification extraction.");461 }462 }463 collapseStatusBar();464 }465 /**466 * isStatusBarExpanded467 *468 * @return boolean469 */470 public boolean isStatusBarExpanded() {471 NotificationPage notificationPage = new NotificationPage(getDriver());472 return notificationPage.isStatusBarExpanded();473 }474 // End of Notification section475 // Change Device Language section476 /**477 * change Android Device Language with default parameters478 *479 * @param language String480 * @return boolean481 */482 public boolean setDeviceLanguage(String language) {483 return setDeviceLanguage(language, true, 20);484 }485 /**486 * change Android Device Language487 * <p>488 * Url: <a href="http://play.google.com/store/apps/details?id=net.sanapeli.adbchangelanguage&hl=ru&rdid=net.sanapeli.adbchangelanguage">489 * ADBChangeLanguage apk490 * </a>491 * Change locale (language) of your device via ADB (on Android OS version 6.0, 5.0, 4.4, 4.3, 4.2 and older).492 * No need to root your device! With ADB (Android Debug Bridge) on your computer,493 * you can fast switch the device locale to see how your application UI looks on different languages.494 * Usage:495 * - install this app496 * - setup adb connection to your device (http://developer.android.com/tools/help/adb.html)497 * - Android OS 4.2 onwards (tip: you can copy the command here and paste it to your command console):498 * adb shell pm grant net.sanapeli.adbchangelanguage android.permission.CHANGE_CONFIGURATION499 * <p>500 * English: adb shell am start -n net.sanapeli.adbchangelanguage/.AdbChangeLanguage -e language en501 * Russian: adb shell am start -n net.sanapeli.adbchangelanguage/.AdbChangeLanguage -e language ru502 * Spanish: adb shell am start -n net.sanapeli.adbchangelanguage/.AdbChangeLanguage -e language es503 *504 * @param language to set. Can be es, en, etc.505 * @param changeConfig boolean if true - update config locale and language params506 * @param waitTime int wait in seconds before device refresh.507 * @return boolean508 */509 public boolean setDeviceLanguage(String language, boolean changeConfig, int waitTime) {510 boolean status = false;511 language = language.toLowerCase();512 if (getDeviceLanguage().toLowerCase().contains(language)) {513 LOGGER.info("Device already have expected language.");514 return true;515 }516 String setLocalizationChangePermissionCmd = "shell pm grant net.sanapeli.adbchangelanguage android.permission.CHANGE_CONFIGURATION";517 String setLocalizationCmd = "shell am start -n net.sanapeli.adbchangelanguage/.AdbChangeLanguage -e language " + language;518 LOGGER.info("Try set localization change permission with following cmd:"519 + setLocalizationChangePermissionCmd);520 String expandOutput = executeAbdCommand(setLocalizationChangePermissionCmd);521 if (expandOutput.contains("Unknown package: net.sanapeli.adbchangelanguage")) {522 LOGGER.info("Looks like 'ADB Change Language apk' is not installed. Install it and try again.");523 installApk(LANGUAGE_CHANGE_APP_PATH, true);524 expandOutput = executeAbdCommand(setLocalizationChangePermissionCmd);525 }526 LOGGER.info("Output after set localization change permission using 'ADB Change Language apk': "527 + expandOutput);528 LOGGER.info("Try set localization to '" + language529 + "' with following cmd: "530 + setLocalizationCmd);531 String changeLocaleOutput = executeAbdCommand(setLocalizationCmd);532 LOGGER.info("Output after set localization to '" + language533 + "' using 'ADB Change Language apk' : " + changeLocaleOutput);534 if (waitTime > 0) {535 LOGGER.info("Wait for at least '" + waitTime536 + "' seconds before device refresh.");537 pause(waitTime);538 }539 if (changeConfig) {540 String loc;541 String lang;542 if (language.contains("_")) {543 lang = language.split("_")[0];544 loc = language.split("_")[1];545 } else {546 lang = language;547 loc = language;548 }549 LOGGER.info("Update config.properties locale to '" + loc + "' and language to '" + lang + "'.");550 R.CONFIG.put("locale", loc);551 R.CONFIG.put("language", lang);552 }553 if (getDeviceLanguage().toLowerCase().contains(language)) {554 status = true;555 } else {556 if (getDeviceLanguage().isEmpty()) {557 LOGGER.info("Adb return empty response without errors.");558 status = true;559 } else {560 String currentAndroidVersion = DevicePool.getDevice().getOsVersion();561 LOGGER.info("currentAndroidVersion=" + currentAndroidVersion);562 if (currentAndroidVersion.contains("7.")) {563 LOGGER.info("Adb return language command do not work on some Android 7+ devices." +564 " Check that there are no error.");565 status = !getDeviceLanguage().toLowerCase().contains("error");566 }567 }568 }569 return status;570 }571 /**572 * getDeviceLanguage573 *574 * @return String575 */576 public String getDeviceLanguage() {577 return executeAbdCommand("shell getprop persist.sys.language");578 }579 // End Language Change section580 // Fake GPS section581 /**582 * startFakeGPS to emulate GPS location583 *584 * @param location String - existing city (for ex. New York)585 * @return boolean return true if everything is ok.586 */587 public boolean setFakeGPSLocation(String location) {588 return setFakeGPSLocation(location, false);589 }590 /**591 * startFakeGPS to emulate GPS location592 *593 * @param location String - existing city (for ex. New York)594 * @param restartApk - if true DriverPool.restartDriver(true);595 * @return boolean return true if everything is ok.596 */597 public boolean setFakeGPSLocation(String location, boolean restartApk) {598 getDriver();599 boolean res = false;600 installApk(FAKE_GPS_APP_PATH, true);601 String activity = FAKE_GPS_APP_ACTIVITY;602 try {603 forceFakeGPSApkOpen();604 FakeGpsPage fakeGpsPage = new FakeGpsPage(getDriver());605 if (!fakeGpsPage.isOpened(1)) {606 LOGGER.error("Fake GPS application should be open but wasn't. Force opening.");607 openApp(activity);608 pause(2);609 }610 res = fakeGpsPage.locationSearch(location);611 if (res) {612 LOGGER.info("Set Fake GPS locale: " + location);613 AndroidUtils.hideKeyboard();614 fakeGpsPage.clickSetLocation();615 }616 res = true;617 if (restartApk) DriverPool.restartDriver(true);618 } catch (Exception e) {619 LOGGER.error("Exception: ", e);620 }621 return res;622 }623 /**624 * stopFakeGPS stop using Fake GPS625 *626 * @return boolean627 */628 public boolean stopFakeGPS() {629 return stopFakeGPS(false);630 }631 /**632 * stopFakeGPS stop using Fake GPS633 *634 * @param restartApk - if true DriverPool.restartDriver(true);635 * @return boolean636 */637 public boolean stopFakeGPS(boolean restartApk) {638 getDriver();639 boolean res = false;640 String activity = FAKE_GPS_APP_ACTIVITY;641 try {642 forceFakeGPSApkOpen();643 FakeGpsPage fakeGpsPage = new FakeGpsPage(getDriver());644 if (!fakeGpsPage.isOpened(1)) {645 LOGGER.error("Fake GPS application should be open but wasn't. Force opening.");646 openApp(activity);647 pause(2);648 }649 LOGGER.info("STOP Fake GPS locale");650 res = fakeGpsPage.clickStopFakeGps();651 if (restartApk) DriverPool.restartDriver(true);652 } catch (Exception e) {653 LOGGER.error("Exception: ", e);654 }655 LOGGER.info("Stop Fake GPS button was clicked: " + res);656 return res;657 }658 /**659 * forceFakeGPSApkOpen660 *661 * @return boolean662 */663 private boolean forceFakeGPSApkOpen() {664 return forceApkOpen(FAKE_GPS_APP_ACTIVITY, FAKE_GPS_APP_PACKAGE, FAKE_GPS_APP_PATH);665 }666 /**667 * forceApkOpen668 *669 * @param activity String670 * @param packageName String671 * @param apkPath String672 * @return boolean673 */674 private boolean forceApkOpen(String activity, String packageName, String apkPath) {675 boolean res;676 int attemps = 3;677 boolean isApkOpened = checkCurrentDeviceFocus(packageName);678 while (!isApkOpened && attemps > 0) {679 LOGGER.info("Apk was not open. Attempt to open...");680 openApp(activity);681 pause(2);682 isApkOpened = checkCurrentDeviceFocus(packageName);683 attemps--;684 }685 if (!isApkOpened) {686 LOGGER.info("Probably APK was not installed correctly. Try to reinstall.");687 installApk(apkPath, true);688 openApp(activity);689 pause(2);690 }691 if (checkCurrentDeviceFocus(packageName)) {692 LOGGER.info("On '" + packageName + "' apk page");693 res = true;694 } else {695 LOGGER.error("Not on '" + packageName + "' page after all tries. Please check logs.");696 res = false;697 }698 return res;699 }700 // End of Fake GPS section701 // TimeZone change section702 /**703 * switchDeviceAutoTimeAndTimeZone704 *705 * @param autoSwitch boolean. If true - auto Time and TimeZone will be set as On.706 */707 public void switchDeviceAutoTimeAndTimeZone(boolean autoSwitch) {708 String value = "0";709 if (autoSwitch) {710 value = "1";711 }712 executeAbdCommand("shell settings put global auto_time " + value);713 executeAbdCommand("shell settings put global auto_time_zone " + value);714 }715 /**716 * get Device Time Zone717 *718 * @return DeviceTimeZone719 */720 public DeviceTimeZone getDeviceTimeZone() {721 return getDeviceTimeZone("");722 }723 /**724 * get Device Time Zone. Set default TimeZone725 *726 * @param defaultTZ - default string.727 * @return DeviceTimeZone728 */729 public DeviceTimeZone getDeviceTimeZone(String defaultTZ) {730 getDriver(); //start driver in before class to assign it for particular thread731 DeviceTimeZone dt = new DeviceTimeZone();732 String value = executeAbdCommand("shell settings get global auto_time");733 if (value.contains("0")) {734 dt.setAutoTime(false);735 } else {736 dt.setAutoTime(true);737 }738 value = executeAbdCommand("shell settings get global auto_time_zone");739 if (value.contains("0")) {740 dt.setAutoTimezone(false);741 } else {742 dt.setAutoTimezone(true);743 }744 value = executeAbdCommand("shell settings get system time_12_24");745 if (value.contains("12")) {746 dt.setTimeFormat(TimeFormat.FORMAT_12);747 } else {748 dt.setTimeFormat(TimeFormat.FORMAT_24);749 }750 if (defaultTZ.isEmpty()) {751 value = executeAbdCommand("shell getprop persist.sys.timezone");752 if (!value.isEmpty()) {753 dt.setTimezone(value);754 }755 } else {756 dt.setTimezone(defaultTZ);757 }758 value = executeAbdCommand("shell date -s %mynow%");759 LOGGER.info(value);760 if (!value.isEmpty()) {761 value = convertDateInCorrectString(parseOutputDate(value));762 dt.setSetDeviceDateTime(value);763 LOGGER.info(value);764 }765 dt.setChangeDateTime(false);766 dt.setRefreshDeviceTime(true);767 LOGGER.info(dt.toString());768 return dt;769 }770 /**771 * get Device Actual TimeZone772 *773 * @return String774 */775 public String getDeviceActualTimeZone() {776 String value = executeAbdCommand("shell getprop persist.sys.timezone");777 if (!value.isEmpty()) {778 LOGGER.info(value);779 }780 return value;781 }782 //Start of TimeZone Setting section783 /**784 * set Device TimeZone by using Apk785 *786 * @param timeZone String required timeZone in Android standard format (Europe/London)787 * @param timeFormat String 12 or 24788 * @return boolean789 */790 public boolean setDeviceTimeZone(String timeZone, TimeFormat timeFormat) {791 return setDeviceTimeZone(timeZone, "", timeFormat, ChangeTimeZoneWorkflow.APK);792 }793 /**794 * set Device TimeZone using all supported workflows. By ADB, Settings and Apk795 *796 * @param timeZone String required timeZone797 * @param timeFormat String 12 or 24798 * @param settingsTZ TimeFormat799 * @return boolean800 */801 public boolean setDeviceTimeZone(String timeZone, String settingsTZ, TimeFormat timeFormat) {802 return setDeviceTimeZone(timeZone, settingsTZ, timeFormat, ChangeTimeZoneWorkflow.ALL);803 }804 /**805 * set Device TimeZone. By required workflow: ADB, Settings or APK806 *807 * @param timeZone String required timeZone808 * @param timeFormat String 12 or 24809 * @param settingsTZ TimeFormat810 * @param workflow ChangeTimeZoneWorkflow811 * @return boolean812 */813 public boolean setDeviceTimeZone(String timeZone, String settingsTZ, TimeFormat timeFormat, ChangeTimeZoneWorkflow workflow) {814 boolean changed = false;815 getDriver(); //start driver in before class to assign it for particular thread816 String actualTZ = getDeviceActualTimeZone();817 if (isRequiredTimeZone(actualTZ, timeZone)) {818 LOGGER.info("Required TimeZone is already set.");819 return true;820 }821 String currentAndroidVersion = DevicePool.getDevice().getOsVersion();822 LOGGER.info("currentAndroidVersion=" + currentAndroidVersion);823 if (currentAndroidVersion.contains("7.") || (DevicePool.getDeviceType() == DeviceType.Type.ANDROID_TABLET)) {824 LOGGER.info("TimeZone changing for Android 7+ and tablets works only by TimeZone changer apk.");825 workflow = ChangeTimeZoneWorkflow.APK;826 }827 //Solution for ADB timezone changing.828 if (ChangeTimeZoneWorkflow.ADB.isSupported(workflow)) {829 LOGGER.info("Try to change TimeZone by ADB");830 LOGGER.info(setDeviceTimeZoneByADB(timeZone, timeFormat, ""));831 changed = applyTZChanges(ChangeTimeZoneWorkflow.ADB, timeZone);832 }833 // Solution for timezone changing by device Settings. (Tested on S7, Note 3, S6, S5).834 if (!changed && ChangeTimeZoneWorkflow.SETTINGS.isSupported(workflow)) {835 LOGGER.info("Try to change TimeZone by Device Settings");836 setDeviceTimeZoneBySetting(timeZone, settingsTZ, timeFormat);837 changed = applyTZChanges(ChangeTimeZoneWorkflow.SETTINGS, timeZone);838 }839 // Solution for using TimeZone Changer apk.840 if (!changed && ChangeTimeZoneWorkflow.APK.isSupported(workflow)) {841 LOGGER.info("Try to change TimeZone by TimeZone Changer apk.");842 setDeviceTimeZoneByChangerApk(timeZone, timeFormat);843 changed = applyTZChanges(ChangeTimeZoneWorkflow.APK, timeZone);844 }845 return changed;846 }847 //End of TimeZone change sections848 //Private section849 //TimeZone Private methods850 /**851 * setDeviceTimeZoneByADB852 *853 * @param timeZone String854 * @param timeFormat TimeFormat...
isSupported
Using AI Code Generation
1import com.qaprosoft.carina.core.foundation.utils.android.AndroidService;2import org.testng.Assert;3import org.testng.annotations.Test;4public class AndroidServiceTest {5 public void testIsSupported() {6 Assert.assertTrue(AndroidService.isSupported("com.android.chrome"));7 Assert.assertFalse(AndroidService.isSupported("com.android.chrome1"));8 }9}10import com.qaprosoft.carina.core.foundation.utils.ios.IOSDriverFactory;11import org.testng.Assert;12import org.testng.annotations.Test;13public class IOSDriverFactoryTest {14 public void testIsSupported() {15 Assert.assertTrue(IOSDriverFactory.isSupported("Safari"));16 Assert.assertFalse(IOSDriverFactory.isSupported("Safari1"));17 }18}19import com.qaprosoft.carina.core.foundation.utils.mobile.MobileService;20import org.testng.Assert;21import org.testng.annotations.Test;22public class MobileServiceTest {23 public void testIsSupported() {24 Assert.assertTrue(MobileService.isSupported("com.android.chrome"));25 Assert.assertFalse(MobileService.isSupported("com.android.chrome1"));26 }27}28import com.qaprosoft.carina.core.foundation.utils.ios.IOSDriverFactory;29import org.testng.Assert;30import org.testng.annotations.Test;31public class IOSDriverFactoryTest {32 public void testIsSupported() {33 Assert.assertTrue(IOSDriverFactory.isSupported("Safari"));34 Assert.assertFalse(IOSDriverFactory.isSupported("Safari1"));35 }36}37import com.qaprosoft.carina.core.foundation.utils.mobile.MobileService;38import org.testng.Assert;39import org.testng.annotations.Test;40public class MobileServiceTest {41 public void testIsSupported() {42 Assert.assertTrue(MobileService.isSupported("com.android.chrome"));43 Assert.assertFalse(MobileService.isSupported("com.android.chrome1"));44 }45}46import com.qaprosoft.carina.core
isSupported
Using AI Code Generation
1import com.qaprosoft.carina.core.foundation.utils.android.AndroidService;2import org.testng.Assert;3import org.testng.annotations.Test;4public class AndroidServiceTest {5 public void testIsSupported() {6 Assert.assertTrue(AndroidService.isSupported("com.qaprosoft.carina.demo:id/phone"));7 }8}9import com.qaprosoft.carina.core.foundation.utils.ios.IosService;10import org.testng.Assert;11import org.testng.annotations.Test;12public class IosServiceTest {13 public void testIsSupported() {14 Assert.assertTrue(IosService.isSupported("com.qaprosoft.carina.demo:id/phone"));15 }16}17import com.qaprosoft.carina.core.foundation.utils.mobile.MobileService;18import org.testng.Assert;19import org.testng.annotations.Test;20public class MobileServiceTest {21 public void testIsSupported() {22 Assert.assertTrue(MobileService.isSupported("com.qaprosoft.carina.demo:id/phone"));23 }24}25import com.qaprosoft.carina.core.foundation.utils.ios.IosService;26import org.testng.Assert;27import org.testng.annotations.Test;28public class IosServiceTest {29 public void testIsSupported() {30 Assert.assertTrue(IosService.isSupported("com.qaprosoft.carina.demo:id/phone"));31 }32}33import com.qaprosoft.carina.core.foundation.utils.mobile.MobileService;34import org.testng.Assert;35import org.testng.annotations.Test;36public class MobileServiceTest {37 public void testIsSupported() {38 Assert.assertTrue(MobileService.isSupported("com.qaprosoft.carina.demo:id/phone"));39 }40}41import com.qaprosoft.carina.core.foundation.utils.ios.IosService;42import org.testng.Assert;43import org.testng.annotations.Test;44public class IosServiceTest {
isSupported
Using AI Code Generation
1import com.qaprosoft.carina.core.foundation.utils.android.AndroidService;2import com.qaprosoft.carina.core.foundation.utils.android.IDevice;3public class AndroidServiceTest {4 public static void main(String[] args) {5 IDevice device = AndroidService.getAndroidDevice();6 System.out.println("Android device is supported: " + device.isSupported());7 }8}
isSupported
Using AI Code Generation
1import com.qaprosoft.carina.core.foundation.utils.android.AndroidService;2public class AndroidServiceTest {3 public static void main(String[] args) {4 AndroidService service = new AndroidService("com.android.vending");5 if (service.isSupported()) {6 service.startService();7 service.stopService();8 }9 }10}11public class AndroidService {12 private String service;13 private String deviceSerial;14 public AndroidService(String service) {15 this.service = service;16 this.deviceSerial = DevicePool.getDevice().getSerialNumber();17 }18 public boolean isSupported() {19 return AndroidUtils.isServiceSupported(service, deviceSerial);20 }21 public void startService() {22 AndroidUtils.startService(service, deviceSerial);23 }24 public void stopService() {25 AndroidUtils.stopService(service, deviceSerial);26 }27}28public class AndroidUtils {29 public static boolean isServiceSupported(String service, String deviceSerial) {30 String output = AdbUtils.executeAdbCommand(deviceSerial, "shell pm list features");31 return output.contains(service);32 }33 public static void startService(String service, String deviceSerial) {34 AdbUtils.executeAdbCommand(deviceSerial, "shell am startservice " + service);35 }36 public static void stopService(String service, String deviceSerial) {37 AdbUtils.executeAdbCommand(deviceSerial, "shell am stopservice " + service);38 }39}40public class AdbUtils {41 public static String executeAdbCommand(String deviceSerial, String command) {42 String adbCommand = "adb -s " + deviceSerial + " " + command;43 return executeCommand(adbCommand);44 }45 public static String executeCommand(String command) {46 String output = "";47 try {48 Process process = Runtime.getRuntime().exec(command);49 BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));50 String line;51 while ((line = reader.readLine()) != null) {52";53 }54 } catch (IOException e) {55 LOGGER.error("Unable to execute command
isSupported
Using AI Code Generation
1AndroidService service = new AndroidService(driver, "com.qaprosoft.carina.demo:id/et1");2Assert.assertTrue(service.isSupported(), "Service is not supported!");3AndroidService service = new AndroidService(driver, "com.qaprosoft.carina.demo:id/et1");4Assert.assertTrue(service.isRunning(), "Service is not running!");5AndroidService service = new AndroidService(driver, "com.qaprosoft.carina.demo:id/et1");6Assert.assertTrue(service.isStopped(), "Service is not stopped!");7AndroidService service = new AndroidService(driver, "com.qaprosoft.carina.demo:id/et1");8service.stop();9AndroidService service = new AndroidService(driver, "com.qaprosoft.carina.demo:id/et1");10service.start();11AndroidService service = new AndroidService(driver, "com.qaprosoft.carina.demo:id/et1");12service.restart();13AndroidService service = new AndroidService(driver, "com.qaprosoft.carina.demo:id/et1");14String packageName = service.getPackageName();
isSupported
Using AI Code Generation
1public class AndroidServiceTest extends AndroidTest {2 public void testIsSupported() {3 Assert.assertTrue(AndroidService.isSupported());4 }5}6mCurrentFocus=Window{2f0b3f3 u0 com.qaprosoft.carina.demo/com.qaprosoft.carina.demo.gui.activities.WebViewActivity}7mCurrentFocus=Window{2f0b3f3 u0 com.qaprosoft.carina.demo/com.qaprosoft.carina.demo.gui.activities.WebViewActivity} mFocusedApp=AppWindowToken{e9e9d7c token=Token{d4a6b4f ActivityRecord{1c7b4f4 u0 com.qaprosoft.carina.demo/com.qaprosoft.carina.demo.gui.activities.WebViewActivity t9}}}8Starting service: Intent { cmp=io.appium.settings/.Settings }9Starting service: Intent { cmp=io.appium.settings/.Settings }10Starting service: Intent { cmp=io.appium.settings/.Settings }
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!