Best Carina code snippet using com.qaprosoft.carina.core.foundation.utils.android.AndroidService.switchDeviceAutoTimeAndTimeZone
Source:AndroidService.java
...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 TimeFormat855 * @param deviceSetDate String in format yyyyMMdd.HHmmss. Can be empty.856 * @return String857 */858 private String setDeviceTimeZoneByADB(String timeZone, TimeFormat timeFormat, String deviceSetDate) {859 boolean changeDateTime = true;860 String tzGMT = "";861 if (deviceSetDate.isEmpty()) {862 changeDateTime = false;863 }864 DeviceTimeZone dt = new DeviceTimeZone(false, false, timeFormat, timeZone, tzGMT, deviceSetDate, changeDateTime, true);865 return setDeviceTimeZoneByADB(dt);866 }867 /**868 * setDeviceTimeZoneByADB869 * Automatic date and time = OFF (settings - date and time)870 * adb shell settings put global auto_time 0871 * Automatic time zone = OFF (settings - date and time)872 * adb shell settings put global auto_time_zone 0873 * <p>874 * Set Time Zone on device875 * adb shell setprop persist.sys.timezone "America/Chicago"876 * <p>877 * Check timezones:878 * <a href="https://en.wikipedia.org/wiki/List_of_tz_database_time_zones">List_of_tz_database_time_zones</a>879 * <p>880 * Check time on device881 * adb shell date -s %mynow%882 * <p>883 * Restart application884 *885 * @param dt DeviceTimeZone886 * @return String actual Device Date and Time887 */888 private String setDeviceTimeZoneByADB(DeviceTimeZone dt) {889 if (dt == null) {890 LOGGER.error("DeviceTimeZone is not initialised.");891 dt = new DeviceTimeZone();892 }893 LOGGER.info(dt.toString());894 String autoTime = "0";895 String autoTimeZone = "0";896 if (dt.isAutoTime()) {897 autoTime = "1";898 }899 executeAbdCommand("shell settings put global auto_time " + autoTime);900 if (dt.isAutoTimezone()) {901 autoTimeZone = "1";902 }903 executeAbdCommand("shell settings put global auto_time_zone " + autoTimeZone);904 setSystemTime(dt.getTimeFormat());905 if (!dt.getTimezone().isEmpty()) {906 executeAbdCommand("shell setprop persist.sys.timezone \"" + dt.getTimezone() + "\"");907 }908 if (dt.isRefreshDeviceTime()) {909 executeAbdCommand("shell am broadcast -a android.intent.action.TIME_SET");910 }911 if (dt.isChangeDateTime() && !dt.getSetDeviceDateTime().isEmpty()) {912 // Try to set date for device but it will not work on not rooted913 // devices914 executeAbdCommand("shell date " + dt.getSetDeviceDateTime());915 }916 String actualDT = executeAbdCommand("shell date -s %mynow%");917 LOGGER.info(actualDT);918 return actualDT;919 }920 /**921 * setDeviceTimeZoneBySetting922 *923 * @param timeZone String924 * @param settingsTZ String925 * @param timeFormat TimeFormat926 */927 private void setDeviceTimeZoneBySetting(String timeZone, String settingsTZ, TimeFormat timeFormat) {928 String actualTZ = getDeviceActualTimeZone();929 String tz = DeviceTimeZone.getTimezoneOffset(timeZone);930 if (isRequiredTimeZone(actualTZ, timeZone)) {931 LOGGER.info("Required timeZone is already set.");932 return;933 }934 try {935 openDateTimeSettingsSetupWizard(true, timeFormat);936 String res = getCurrentDeviceFocus();937 if (res.contains("settings.DateTimeSettingsSetupWizard")) {938 LOGGER.info("On settings.DateTimeSettingsSetupWizard page");939 } else {940 LOGGER.error("Not on settings.DateTimeSettingsSetupWizard page");941 }942 DateTimeSettingsPage dtSettingsPage = new DateTimeSettingsPage(getDriver());943 if (!dtSettingsPage.isOpened(3)) {944 openDateTimeSettingsSetupWizard(true, timeFormat);945 }946 if (dtSettingsPage.isOpened(3)) {947 LOGGER.info("Date Time Settings page was open.");948 } else {949 LOGGER.error("Date Time Settings page should be open.");950 }951 dtSettingsPage.openTimeZoneSetting();952 dtSettingsPage.selectTimeZone(tz, settingsTZ);953 dtSettingsPage.clickNextButton();954 } catch (Exception e) {955 LOGGER.error("Exception: ", e);956 }957 }958 /**959 * setDeviceTimeZoneByChangerApk960 *961 * @param timeZone String962 * @param timeFormat TimeFormat963 */964 private void setDeviceTimeZoneByChangerApk(String timeZone, TimeFormat timeFormat) {965 String actualTZ = getDeviceActualTimeZone();966 String tz = DeviceTimeZone.getTimezoneOffset(timeZone);967 LOGGER.info("Required TimeZone offset: " + tz);968 if (isRequiredTimeZone(actualTZ, timeZone)) {969 LOGGER.info("Required timeZone is already set.");970 return;971 }972 installApk(TZ_CHANGE_APP_PATH, true);973 try {974 forceTZChangingApkOpen(true, timeFormat);975 TZChangerPage tzChangerPage = new TZChangerPage(getDriver());976 if (tzChangerPage.isOpened(3)) {977 LOGGER.info("TimeZone changer main page was open.");978 } else {979 LOGGER.error("TimeZone changer main page should be open. Retry to open.");980 openTZChangingApk(true, timeFormat);981 }982 tzChangerPage.selectTimeZone(timeZone);983 } catch (Exception e) {984 LOGGER.error("Exception: ", e);985 }986 }987 private boolean applyTZChanges(ChangeTimeZoneWorkflow workflow, String expectedZone) {988 boolean res = false;989 String actualTZ = getDeviceActualTimeZone();990 if (isRequiredTimeZone(actualTZ, expectedZone)) {991 LOGGER.info("Required timeZone '" + expectedZone + "' was set by " + workflow.toString() + ". Restarting driver to apply changes.");992 DriverPool.restartDriver(true);993 res = true;994 } else {995 LOGGER.error("TimeZone was not changed by " + workflow.toString() + ". Actual TZ is: " + actualTZ);996 }997 return res;998 }999 /**1000 * comparingExpectedAndActualTZ1001 *1002 * @param actualTZ String1003 * @param expextedTZ String1004 * @return boolean1005 */1006 private boolean isRequiredTimeZone(String actualTZ, String expextedTZ) {1007 boolean res = actualTZ.equals(expextedTZ);1008 if (!res) {1009 String[] actTZ = actualTZ.split("/");1010 String lastActTZ = actTZ[actTZ.length - 1];1011 String[] timeZoneTZ = expextedTZ.split("/");1012 String lastTimeZoneTZ = timeZoneTZ[timeZoneTZ.length - 1];1013 LOGGER.debug("Comparing '" + lastActTZ + "' with '" + lastTimeZoneTZ + "'.");1014 res = lastActTZ.equals(lastTimeZoneTZ);1015 }1016 return res;1017 }1018 /**1019 * @param turnOffAuto boolean1020 * @param timeFormat TimeFormat1021 * @return boolean1022 */1023 private boolean forceTZChangingApkOpen(boolean turnOffAuto, TimeFormat timeFormat) {1024 boolean res = false;1025 String tzPackageName = TZ_CHANGE_APP_PACKAGE;1026 int attemps = 3;1027 boolean isTzOpened = checkCurrentDeviceFocus(tzPackageName);1028 while (!isTzOpened && attemps > 0) {1029 LOGGER.info("TimeZoneChanger apk was not open. Attempt to open...");1030 openTZChangingApk(turnOffAuto, timeFormat);1031 isTzOpened = checkCurrentDeviceFocus(tzPackageName);1032 attemps--;1033 }1034 if (!isTzOpened) {1035 LOGGER.info("Probably TimeZone Changer APK was not installed correctly. Try to reinstall.");1036 installApk(TZ_CHANGE_APP_PATH, true);1037 openTZChangingApk(turnOffAuto, timeFormat);1038 }1039 TZChangerPage tzChangerPage = new TZChangerPage(getDriver());1040 if (!tzChangerPage.isOpened(10)) {1041 openTZChangingApk(turnOffAuto, timeFormat);1042 }1043 if (tzChangerPage.isOpened(3)) {1044 LOGGER.info("TimeZone changer main page was open.");1045 res = true;1046 } else {1047 LOGGER.error("TimeZone changer main page should be open.");1048 openTZChangingApk(turnOffAuto, timeFormat);1049 res = false;1050 }1051 if (checkCurrentDeviceFocus(tzPackageName)) {1052 LOGGER.info("On TZ changer apk page");1053 res = true;1054 } else {1055 LOGGER.error("Not on com.futurek.android.tzc page after all tries. Please check logs.");1056 res = false;1057 }1058 return res;1059 }1060 /**1061 * openDateTimeSettingsSetupWizard in settings1062 *1063 * @param turnOffAuto - turn off AutoTimeZone and AutoTime1064 * @param timeFormat - can be 12 or 24. Or empty.1065 */1066 private void openDateTimeSettingsSetupWizard(boolean turnOffAuto, TimeFormat timeFormat) {1067 if (turnOffAuto) {1068 switchDeviceAutoTimeAndTimeZone(false);1069 }1070 setSystemTime(timeFormat);1071 openApp("com.android.settings/.DateTimeSettingsSetupWizard");1072 }1073 /**1074 * openDateTimeSettingsSetupWizard in settings1075 *1076 * @param turnOffAuto - turn off AutoTimeZone and AutoTime1077 * @param timeFormat - can be 12 or 24. Or empty.1078 */1079 private void openTZChangingApk(boolean turnOffAuto, TimeFormat timeFormat) {1080 if (turnOffAuto) {1081 switchDeviceAutoTimeAndTimeZone(false);1082 }1083 setSystemTime(timeFormat);1084 openApp(TZ_CHANGE_APP_ACTIVITY);1085 pause(2);1086 }1087 private void setSystemTime(TimeFormat timeFormat) {1088 switch (timeFormat) {1089 case FORMAT_12:1090 LOGGER.info("Set 12 hours format");1091 executeAbdCommand("shell settings put system time_12_24 12");1092 break;1093 case FORMAT_24:1094 LOGGER.info("Set 24 hours format");1095 executeAbdCommand("shell settings put system time_12_24 24");...
switchDeviceAutoTimeAndTimeZone
Using AI Code Generation
1AndroidService.switchDeviceAutoTimeAndTimeZone(false);2IOSService.switchDeviceAutoTimeAndTimeZone(false);3AndroidService.switchDeviceAutoTimeAndTimeZone(true);4IOSService.switchDeviceAutoTimeAndTimeZone(true);5AndroidService.switchDeviceAutoTimeAndTimeZone();6IOSService.switchDeviceAutoTimeAndTimeZone();7AndroidService.switchDeviceAutoTimeAndTimeZone(false);8IOSService.switchDeviceAutoTimeAndTimeZone(false);9AndroidService.switchDeviceAutoTimeAndTimeZone(true);10IOSService.switchDeviceAutoTimeAndTimeZone(true);11AndroidService.switchDeviceAutoTimeAndTimeZone();12IOSService.switchDeviceAutoTimeAndTimeZone();13AndroidService.switchDeviceAutoTimeAndTimeZone(false);14IOSService.switchDeviceAutoTimeAndTimeZone(false);
switchDeviceAutoTimeAndTimeZone
Using AI Code Generation
1import com.qaprosoft.carina.core.foundation.utils.android.AndroidService;2import com.qaprosoft.carina.core.foundation.utils.android.AndroidUtils;3import com.qaprosoft.carina.core.foundation.utils.android.DeviceTimeZone;4import com.qaprosoft.carina.core.foundation.utils.android.DeviceTimeFormat;5import org.apache.log4j.Logger;6import org.testng.Assert;7import org.testng.annotations.Test;8public class AndroidServiceTest {9 private static final Logger LOGGER = Logger.getLogger(AndroidServiceTest.class);10 public void testSwitchDeviceTimeAndTimeZone() {11 LOGGER.info("Switching device time and timezone");12 AndroidUtils.switchDeviceAutoTimeAndTimeZone(DeviceTimeZone.PST, DeviceTimeFormat.HH_MM_24);13 Assert.assertTrue(AndroidService.isDeviceTimeZone(DeviceTimeZone.PST));14 Assert.assertTrue(AndroidService.isDeviceTimeFormat(DeviceTimeFormat.HH_MM_24));15 }16}17The method switchDeviceAutoTimeAndTimeZone() has two parameters:18The method isDeviceTimeZone() has one parameter:19The method isDeviceTimeFormat() has one parameter:20The method switchDeviceAutoTimeAndTimeZone() switches device time zone and time format. The method has two parameters:21import com.qaprosoft.carina.core.foundation.utils.android.AndroidService;22import com.qaprosoft.carina.core.foundation.utils.android.AndroidUtils;23import com.qaprosoft.carina.core.foundation.utils.android.DeviceTimeZone;24import com.qaprosoft.carina.core.foundation.utils.android.DeviceTimeFormat;25import org.apache.log4j.Logger;26import org.testng.Assert;27import org
switchDeviceAutoTimeAndTimeZone
Using AI Code Generation
1import com.qaprosoft.carina.core.foundation.utils.android.AndroidService;2import com.qaprosoft.carina.core.foundation.utils.android.DeviceTimezone;3public class AndroidServiceTest {4 public static void main(String[] args) {5 AndroidService androidService = new AndroidService();6 androidService.switchDeviceAutoTimeAndTimeZone(DeviceTimezone.EUROPE_MOSCOW);7 }8}9import com.qaprosoft.carina.core.foundation.utils.ios.IosService;10import com.qaprosoft.carina.core.foundation.utils.ios.DeviceTimezone;11public class IosServiceTest {12 public static void main(String[] args) {13 IosService iosService = new IosService();14 iosService.switchDeviceAutoTimeAndTimeZone(DeviceTimezone.EUROPE_MOSCOW);15 }16}17import com.qaprosoft.carina.core.foundation.utils.mobile.MobileService;18import com.qaprosoft.carina.core.foundation.utils.mobile.DeviceTimezone;19public class MobileServiceTest {20 public static void main(String[] args) {21 MobileService mobileService = new MobileService();22 mobileService.switchDeviceAutoTimeAndTimeZone(DeviceTimezone.EUROPE_MOSCOW);23 }24}25import com.qaprosoft.carina.core.foundation.utils.mobile.MobileService;26import com.qaprosoft.carina.core.foundation.utils.mobile.DeviceTimezone;27public class MobileServiceTest {28 public static void main(String[] args) {29 MobileService mobileService = new MobileService();30 mobileService.switchDeviceAutoTimeAndTimeZone(DeviceTimezone.EUROPE_MOSCOW);31 }32}33import com.qaprosoft.carina.core.foundation.utils.android.AndroidService;34import com.qaprosoft.carina.core.foundation.utils.android.DeviceTimezone;35public class AndroidServiceTest {36 public static void main(String[] args) {37 AndroidService androidService = new AndroidService();38 androidService.switchDeviceAutoTimeAndTimeZone(DeviceTimezone.EUROPE_M
switchDeviceAutoTimeAndTimeZone
Using AI Code Generation
1 switchDeviceAutoTimeAndTimeZone(true);2 switchDeviceAutoTimeAndTimeZone(false);3 boolean autoTimeAndTimeZone = getDeviceAutoTimeAndTimeZone();4 Assert.assertTrue(autoTimeAndTimeZone);5 setDeviceAutoTimeAndTimeZone(true);6 setDeviceAutoTimeAndTimeZone(false);7 boolean autoTimeAndTimeZone = getDeviceAutoTimeAndTimeZone();8 Assert.assertTrue(autoTimeAndTimeZone);9 setDeviceAutoTimeAndTimeZone(true);10 setDeviceAutoTimeAndTimeZone(false);11 boolean autoTimeAndTimeZone = getDeviceAutoTimeAndTimeZone();12 Assert.assertTrue(autoTimeAndTimeZone);13 }14}15import com.qaprosoft.carina.core.foundation.utils.android.AndroidService;16switchDeviceAutoTimeAndTimeZone(true);17boolean autoTimeAndTimeZone = getDeviceAutoTimeAndTimeZone();18setDeviceAutoTimeAndTimeZone(true);19boolean autoTimeAndTimeZone = getDeviceAutoTimeAndTimeZone();20setDeviceAutoTimeAndTimeZone(true);
switchDeviceAutoTimeAndTimeZone
Using AI Code Generation
1switchDeviceAutoTimeAndTimeZone(true);2switchDeviceAutoTimeAndTimeZone(false);3switchDeviceAutoTimeAndTimeZone(true, "Asia/Calcutta");4switchDeviceAutoTimeAndTimeZone(true, "America/New_York");5switchDeviceAutoTimeAndTimeZone(true, "America/Los_Angeles");6switchDeviceAutoTimeAndTimeZone(true, "America/Chicago");7switchDeviceAutoTimeAndTimeZone(true, "Europe/London");8switchDeviceAutoTimeAndTimeZone(true, "Europe/Paris");9switchDeviceAutoTimeAndTimeZone(true, "Africa/Cairo");10switchDeviceAutoTimeAndTimeZone(true, "Asia/Baghdad");11switchDeviceAutoTimeAndTimeZone(true, "Asia/Dubai");12switchDeviceAutoTimeAndTimeZone(true, "Asia/Karachi");
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!!