Best Carina code snippet using com.qaprosoft.carina.core.foundation.utils.android.IAndroidUtils.executeShell
Source:IAndroidUtils.java
...634 * NOTE: shell arguments with space symbols are unsupported!635 * 636 * @return String - response (might be empty)637 */638 default public String executeShell(String command) {639 UTILS_LOGGER.info("ADB command to be executed: adb shell ".concat(command.trim()));640 List<String> literals = Arrays.asList(command.split(" "));641 return executeShell(literals);642 }643 /**644 * 645 * @param commands list of string commands646 * 647 * - ADB shell command represented as single String where 1st literal648 * is a command itself. Everything that follow is treated as649 * arguments.650 *651 * NOTE: "adb -s {UDID} shell" - should be omitted.652 * Example: "adb -s {UDID} shell list packages" - list packages653 * 654 * @return String - response (might be empty)655 */656 default public String executeShell(List<String> commands) {657 String commadKeyWord = commands.get(0);658 List<String> args = commands.subList(1, commands.size());659 Map<String, Object> preparedCommand = ImmutableMap.of("command", commadKeyWord, "args", args);660 String output = (String) ((AppiumDriver<?>) castDriver()).executeScript(SHELL_INIT_CONSOLE, preparedCommand);661 if (!StringUtils.isEmpty(output)) {662 UTILS_LOGGER.debug("ADB command output: " + output);663 }664 return output;665 }666 /**667 * This method performs an action corresponding to press Android device's native668 * button to show all recent applications.669 * 670 * NOTE: method could be used to get a list of running in background671 * applications with respect to particular device.672 */673 default public void displayRecentApps() {674 executeShell(SHELL_RECENT_APPS_CMD);675 }676 /**677 * Tapping at native 'Home' button will be emulated. All applications will be678 * closed to background.679 */680 default public void pressHome() {681 executeShell(SHELL_PRESS_HOME_CMD);682 }683 /**684 * Is used to get GPS service status.685 * 686 * Response reflects which services are used for obtaining location:687 * 688 * - "gps" - GPS only (device only);689 * 690 * - "gps,network" - GPS + Wi-Fi + Bluetooth or cellular networks (High accuracy691 * mode);692 * 693 * - "network" - Using Wi-Fi, Bluetooth or cellular networks (Battery saving694 * mode);695 * 696 * @return boolean697 */698 default public boolean isGPSEnabled() {699 String response = executeShell(SHELL_GPS_STATUS_CMD);700 return response.contains("gps");701 }702 default public void enableGPS() {703 executeShell(SHELL_ENABLE_GPS_CMD);704 }705 /**706 * Works if ONLY DEVICE (GPS sensor) is user for obtaining location707 */708 default public void disableGPS() {709 executeShell(SHELL_DISABLE_GPS_CMD);710 }711 /**712 * This command will save screenshot to specified folder on device's OS using713 * provided path.714 * 715 * @param filepath716 * - path to save screenshot to device's OS (/storage/emulated/0/Download/scr.png).717 */718 default public void takeScreenShot(String filepath) {719 UTILS_LOGGER.info("Screenshot will be saved to: " + filepath);720 String command = String.format(SHELL_TAKE_SCREENSHOT_CMD.concat(" %s"), filepath);721 executeShell(command);722 }723 /**724 * This method provides app's version for the app that is already installed to725 * devices, based on its package name.726 * In order to do that we search for "versionCode" parameter in system dump.727 * 728 * @param packageName String729 * 730 * @return appVersion String731 */732 default public String getAppVersion(String packageName) {733 String command = "dumpsys package ".concat(packageName);734 String output = executeShell(command);735 String versionCode = StringUtils.substringBetween(output, "versionCode=", " ");736 UTILS_LOGGER.info(String.format("Version code for '%s' package name is %s", packageName, versionCode));737 return versionCode;738 }739 /**740 * This method provides app's version name for the app that is already installed to741 * devices, based on its package name.742 * In order to do that we search for "versionName" parameter in system dump.743 * Ex. "versionCode" returns 11200050, "versionName" returns 11.2.0744 * 745 * @param packageName String746 * @return appVersion String747 */748 default public String getAppVersionName(String packageName) {749 String command = "dumpsys package ".concat(packageName);750 String output = this.executeShell(command);751 String versionName = StringUtils.substringBetween(output, "versionName=", "\n");752 UTILS_LOGGER.info(String.format("Version name for '%s' package name is %s", packageName, versionName));753 return versionName;754 }755 /**756 * To open Android device native settings757 */758 default public void openDeviceSettings() {759 executeShell(SHELL_OPEN_DEVICE_SETTINGS_CMD);760 }761 /**762 * Method to reset test specific application by package name763 * 764 * App's settings will be reset. User will be logged out. Application will be765 * closed to background.766 * 767 * @param packageName String768 */769 default public void clearAppCache(String packageName) {770 UTILS_LOGGER.info("Will clear data for the following app: " + packageName);771 String command = String.format(SHELL_CLEAR_CACHE_CMD.concat(" %s"), packageName);772 String response = executeShell(command);773 UTILS_LOGGER.info(774 String.format("Output after resetting custom application by package (%s): ", packageName) + response);775 if (!response.contains("Success")) {776 UTILS_LOGGER.warn(String.format("App data was not cleared for %s app", packageName));777 }778 }779 /**780 * With this method user is able to trigger a deeplink (link to specific place781 * within the application) or event open URL in mobile browser.782 * 783 * NOTE, that to open URL in browser, URL should starts with "https://www.{place784 * your link here}".785 * 786 * NOTE that not all deeplinks require package name.787 * 788 * @param link789 * - URL to trigger790 */791 default public void openURL(String link) {792 // TODO: #1380 make openURL call from this mobile interface in DriverHelper793 UTILS_LOGGER.info("Following link will be triggered via ADB: " + link);794 String command = String.format(SHELL_OPEN_URL_CMD.concat(" %s"), link);795 executeShell(command);796 }797 /**798 * With this method user is able to trigger a deeplink (link to specific place799 * within the application)800 * 801 * @param link String802 * @param packageName String803 */804 default public void triggerDeeplink(String link, String packageName) {805 Map<String, Object> preparedCommand = ImmutableMap.of("url", link, "package", packageName);806 try {807 ((AppiumDriver<?>) castDriver()).executeScript(SHELL_INIT_DEEPLINK_CONSOLE, preparedCommand);808 } catch (WebDriverException wde) {809 // TODO: need to pay attention810 UTILS_LOGGER.warn("org.openqa.selenium.WebDriverException is caught and ignored.", wde);811 }812 }813 /**814 * To get list of granted/denied/requested permission for specified application815 * 816 * @param packageName String817 * @param type PermissionType818 * @return ArrayList String819 */820 @SuppressWarnings("unchecked")821 default public ArrayList<String> getAppPermissions(String packageName, PermissionType type) {822 Map<String, Object> preparedCommand = ImmutableMap.of("type", type.getType(), "package", packageName);823 return (ArrayList<String>) ((AppiumDriver<?>) castDriver()).executeScript(SHELL_INIT_GET_PERMISSION_CONSOLE,824 preparedCommand);825 }826 /**827 * To change (grant or revoke) application permissions.828 * 829 * @param packageName String830 * @param action PermissionAction831 * @param permissions Permission832 */833 default public void changePermissions(String packageName, PermissionAction action, Permission... permissions) {834 ArrayList<String> permissionsStr = new ArrayList<>();835 Arrays.asList(permissions).forEach(p -> permissionsStr.add(p.getPermission()));836 Map<String, Object> preparedCommand = ImmutableMap.of("action", action.getAction(), "appPackage", packageName,837 "permissions", permissionsStr);838 ((AppiumDriver<?>) castDriver()).executeScript(SHELL_INIT_CHANGE_PERMISSION_CONSOLE, preparedCommand);839 }840 /**841 * Method to enter text to ACTIVATED input field.842 * 843 * NOTE: that it might be necessary to escape some special characters.844 * Space-symbol is already escaped.845 * 846 * NOTE2: input field should be cleared previously.847 * 848 * @param text String849 */850 default public void typeWithADB(String text) {851 UTILS_LOGGER.info(String.format("Will enter '%s' to an active input field via ADB.", text));852 // In this method characters are entered one by one because sometimes some853 // characters might be omitted if to enter whole text at a time.854 char[] array = text.toCharArray();855 for (char sym : array) {856 String ch = (sym == ' ') ? "%s" : String.valueOf(sym);857 String command = SHELL_INPUT_TXT_CMD + ch;858 executeShell(command);859 }860 }861 default public boolean isWifiEnabled() {862 boolean enabled = ((AndroidDriver<?>) castDriver()).getConnection().isWiFiEnabled();863 UTILS_LOGGER.info("Wi-Fi enabled: " + enabled);864 return enabled;865 }866 default public void enableWifi() {867 boolean enabled = isWifiEnabled();868 if (!enabled) {869 ((AndroidDriver<?>) castDriver()).toggleWifi();870 return;871 }872 UTILS_LOGGER.info("Wifi is already anebled. No actions needed");873 }874 default public void disableWifi() {875 boolean enabled = isWifiEnabled();876 if (enabled) {877 ((AndroidDriver<?>) castDriver()).toggleWifi();878 return;879 }880 UTILS_LOGGER.info("Wifi is already disabled. No actions needed");881 }882 /**883 * Method enters an App's menu within device System Settings884 * 885 * @param appName - Name of the app as it appears in the device's Apps list (Language specific)886 */887 default void openAppMenuFromDeviceSettings(String appName) {888 AndroidService androidService = AndroidService.getInstance();889 androidService.executeAdbCommand("shell am start -a android.settings.APPLICATION_SETTINGS");890 // initializing appItem with ExtendedWebElement constructor that initialize search context891 ExtendedWebElement appItem = new ExtendedWebElement(By.xpath(String.format("//*[contains(@text, '%s')]", appName)), "notifications",892 getDriver(), getDriver());893 swipe(appItem);894 appItem.click();895 }896 /**897 * Toggles a specified app's ability to recieve Push Notifications on the system level898 * 899 * @param appName - The app name as it appears within device System Settings900 * @param setValue - The value you wish to set the toggle to901 */902 default void toggleAppNotificationsFromDeviceSettings(String appName, boolean setValue) {903 openAppMenuFromDeviceSettings(appName);904 WebDriver driver = getDriver();905 // initializing with driver context906 ExtendedWebElement element = new ExtendedWebElement(By.xpath("//*[contains(@text, 'Notifications') or contains(@text, 'notifications')]"),907 "notifications", driver, driver);908 element.click();909 // initializing with driver context910 element = new ExtendedWebElement(By.xpath("//*[@resource-id='com.android.settings:id/switch_text']/following-sibling::android.widget.Switch"),911 "toggle", driver, driver);912 if (Boolean.valueOf(element.getAttribute("checked")) != setValue) {913 element.click();914 }915 }916 /**917 * @return - Returns if the device in use has a running LTE connection918 */919 default boolean isCarrierConnectionAvailable() {920 AndroidService androidService = AndroidService.getInstance();921 boolean status = ((AndroidDriver) this.castDriver()).getConnection().isDataEnabled();922 boolean linkProperties = false;923 String linkProp = androidService.executeAdbCommand("shell dumpsys telephony.registry | grep mPreciseDataConnectionState");924 UTILS_LOGGER.info("PROP: " + linkProp);925 if (!linkProp.isEmpty()) {926 linkProperties = !StringUtils.substringBetween(linkProp, "APN: ", " ").equals("null");927 }928 UTILS_LOGGER.info("STATUS ENABLED: " + status);929 UTILS_LOGGER.info("CARRIER AVAILABLE: " + linkProperties);930 return ((AndroidDriver) this.castDriver()).getConnection().isDataEnabled() && linkProperties;931 }932 /**933 * @return - Returns the value of the device model in use as a String934 */935 default String getDeviceModel() {936 AndroidService androidService = AndroidService.getInstance();937 return StringUtils.substringAfter(androidService.executeAdbCommand("shell getprop | grep 'ro.product.model'"), "ro.product.model: ");938 }939 default public void openStatusBar() {940 executeShell(SHELL_OPEN_STATUS_BAR_CMD);941 }942 default public void closeStatusBar() {943 executeShell(SHELL_CLOSE_STATUS_BAR_CMD);944 }945}...
executeShell
Using AI Code Generation
1IAndroidUtils androidUtils = (IAndroidUtils) AndroidUtils.getAndroidUtils();2androidUtils.executeShell("adb shell am start -n com.android.settings/.Settings");3IAndroidUtils androidUtils = (IAndroidUtils) AndroidUtils.getAndroidUtils();4androidUtils.executeShell("adb shell am start -n com.android.settings/.Settings");5IAndroidUtils androidUtils = (IAndroidUtils) AndroidUtils.getAndroidUtils();6androidUtils.executeShell("adb shell am start -n com.android.settings/.Settings");7IAndroidUtils androidUtils = (IAndroidUtils) AndroidUtils.getAndroidUtils();8androidUtils.executeShell("adb shell am start -n com.android.settings/.Settings");9IAndroidUtils androidUtils = (IAndroidUtils) AndroidUtils.getAndroidUtils();10androidUtils.executeShell("adb shell am start -n com.android.settings/.Settings");11IAndroidUtils androidUtils = (IAndroidUtils) AndroidUtils.getAndroidUtils();12androidUtils.executeShell("adb shell am start -n com.android.settings/.Settings");13IAndroidUtils androidUtils = (IAndroidUtils) AndroidUtils.getAndroidUtils();
executeShell
Using AI Code Generation
1import com.qaprosoft.carina.core.foundation.utils.android.AndroidUtils;2import com.qaprosoft.carina.core.foundation.utils.android.IAndroidUtils;3import com.qaprosoft.carina.core.foundation.utils.android.AndroidUtils;4public class AndroidShellCommands {5public static void main(String[] args) {6IAndroidUtils androidUtils = new AndroidUtils();7String command = "input keyevent 3";8String output = androidUtils.executeShell(command);9System.out.println("Output: " + output);10}11}12[ 0.000000] Linux version 3.4.0-gf1e8c5b (jenkins@jenkins) (gcc version 4.8 (GCC) ) #1 SMP PREEMPT Thu Mar 26 14:47:01 UTC 201513[ 0.000000] CPU: ARMv7 Processor [412fc090] revision 0 (ARMv7), cr=10c5387d
executeShell
Using AI Code Generation
1String version = AndroidUtils.executeShell("dumpsys package com.qaprosoft.carina.demo | grep versionName");2version = version.split("=")[1];3String version = AndroidUtils.executeShell("dumpsys package com.qaprosoft.carina.demo | grep versionName");4version = version.split("=")[1];5String version = AndroidUtils.executeShell("dumpsys package com.qaprosoft.carina.demo | grep versionName");6version = version.split("=")[1];7String version = AndroidUtils.executeShell("dumpsys package com.qaprosoft.carina.demo | grep
executeShell
Using AI Code Generation
1import com.qaprosoft.carina.core.foundation.utils.android.IAndroidUtils;2public class ExecuteShellCommandOnAndroidDevice {3 public static void main(String[] args) {4 IAndroidUtils androidUtils = (IAndroidUtils) IAndroidUtils.getInstance();5 androidUtils.executeShell("am start -a android.settings.SETTINGS");6 String result = androidUtils.executeShell("dumpsys window windows | grep -E 'mCurrentFocus'");7 String[] resultArray = result.split("/");8 String[] finalResult = resultArray[1].split(" ");9 if (finalResult[0].equals("com.android.settings")) {10 System.out.println("Settings are opened");11 } else {12 System.out.println("Settings are not opened");13 }14 }15}16import com.qaprosoft.carina.core.foundation.utils.ios.IIosUtils;17public class ExecuteShellCommandOnIosDevice {18 public static void main(String[] args) {19 IIosUtils iosUtils = (IIosUtils) IIosUtils.getInstance();20 iosUtils.executeShell("am start -a android.settings.SETTINGS");21 String result = iosUtils.executeShell("dumpsys window windows | grep -E 'mCurrentFocus'");22 String[] resultArray = result.split("/");23 String[] finalResult = resultArray[1].split(" ");24 if (finalResult[0].equals("com.android.settings")) {25 System.out.println("Settings are opened");26 } else {27 System.out.println("Settings are not opened");28 }29 }30}31import com.qaprosoft.carina.core.foundation.utils.windows.IWindowsUtils;
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!!