Best Carina code snippet using com.qaprosoft.carina.core.foundation.webdriver.device.Device.installApp
Source:Device.java
...351 }352 }353 return packageName;354 }355 public void uninstallApp(String packageName) {356 if (isNull())357 return;358 // adb -s UDID uninstall com.myfitnesspal.android359 String[] cmd = CmdLine.insertCommandsAfter(executor.getDefaultCmd(), "-s", getAdbName(), "uninstall", packageName);360 executor.execute(cmd);361 }362 public void installApp(String apkPath) {363 if (isNull())364 return;365 // adb -s UDID install com.myfitnesspal.android366 String[] cmd = CmdLine.insertCommandsAfter(executor.getDefaultCmd(), "-s", getAdbName(), "install", "-r", apkPath);367 executor.execute(cmd);368 }369 public synchronized void installAppSync(String apkPath) {370 if (isNull())371 return;372 // adb -s UDID install com.myfitnesspal.android373 String[] cmd = CmdLine.insertCommandsAfter(executor.getDefaultCmd(), "-s", getAdbName(), "install", "-r", apkPath);374 executor.execute(cmd);375 }376 /*377 * public void reinstallApp() {378 * if (!Configuration.getPlatform().equalsIgnoreCase(SpecialKeywords.ANDROID)) {379 * return;380 * }381 * 382 * if (isNull())383 * return;384 * 385 * String mobileApp = Configuration.getMobileApp();386 * String oldMobileApp = Configuration.get(Parameter.MOBILE_APP_PREUPGRADE);387 * 388 * if (!oldMobileApp.isEmpty()) {389 * //redefine strategy to do upgrade scenario390 * R.CONFIG.put(Parameter.MOBILE_APP_UNINSTALL.getKey(), "true");391 * R.CONFIG.put(Parameter.MOBILE_APP_INSTALL.getKey(), "true");392 * }393 * 394 * if (Configuration.getBoolean(Parameter.MOBILE_APP_UNINSTALL)) {395 * // explicit reinstall the apk396 * String[] apkVersions = getApkVersion(mobileApp);397 * if (apkVersions != null) {398 * String appPackage = apkVersions[0];399 * 400 * String[] apkInstalledVersions = getInstalledApkVersion(appPackage);401 * 402 * LOGGER.info("installed app: " + apkInstalledVersions[2] + "-" + apkInstalledVersions[1]);403 * LOGGER.info("new app: " + apkVersions[2] + "-" + apkVersions[1]);404 * 405 * if (apkVersions[1].equals(apkInstalledVersions[1]) && apkVersions[2].equals(apkInstalledVersions[2]) && oldMobileApp.isEmpty()) {406 * LOGGER.info(407 * "Skip application uninstall and cache cleanup as exactly the same version is already installed.");408 * } else {409 * uninstallApp(appPackage);410 * clearAppData(appPackage);411 * isAppInstalled = false;412 * if (!oldMobileApp.isEmpty()) {413 * LOGGER.info("Starting sync install operation for preupgrade app: " + oldMobileApp);414 * installAppSync(oldMobileApp);415 * }416 * 417 * if (Configuration.getBoolean(Parameter.MOBILE_APP_INSTALL)) {418 * // install application in single thread to fix issue with gray Google maps419 * LOGGER.info("Starting sync install operation for app: " + mobileApp);420 * installAppSync(mobileApp);421 * }422 * }423 * }424 * } else if (Configuration.getBoolean(Parameter.MOBILE_APP_INSTALL) && !isAppInstalled) {425 * LOGGER.info("Starting install operation for app: " + mobileApp);426 * installApp(mobileApp);427 * isAppInstalled = true;428 * }429 * }430 */431 public String[] getInstalledApkVersion(String packageName) {432 // adb -s UDID shell dumpsys package PACKAGE | grep versionCode433 if (isNull())434 return null;435 String[] res = new String[3];436 res[0] = packageName;437 String[] cmd = CmdLine.insertCommandsAfter(executor.getDefaultCmd(), "-s", getAdbName(), "shell", "dumpsys", "package", packageName);438 List<String> output = executor.execute(cmd);439 for (String line : output) {440 LOGGER.debug(line);441 if (line.contains("versionCode")) {442 // versionCode=17040000 targetSdk=25443 LOGGER.info("Line for parsing installed app: " + line);444 String[] outputs = line.split("=");445 String tmp = outputs[1]; // everything after '=' sign446 res[1] = tmp.split(" ")[0];447 }448 if (line.contains("versionName")) {449 // versionName=8.5.0450 LOGGER.info("Line for parsing installed app: " + line);451 String[] outputs = line.split("=");452 res[2] = outputs[1];453 }454 }455 if (res[0] == null && res[1] == null && res[2] == null) {456 return null;457 }458 return res;459 }460 public String[] getApkVersion(String apkFile) {461 // aapt dump badging <apk_file> | grep versionCode462 // aapt dump badging <apk_file> | grep versionName463 // output:464 // package: name='com.myfitnesspal.android' versionCode='9025' versionName='develop-QA' platformBuildVersionName='6.0-2704002'465 String[] res = new String[3];466 res[0] = "";467 res[1] = "";468 res[2] = "";469 String[] cmd = CmdLine.insertCommandsAfter("aapt dump badging".split(" "), apkFile);470 List<String> output = executor.execute(cmd);471 // parse output command and get appropriate data472 for (String line : output) {473 if (line.contains("versionCode") && line.contains("versionName")) {474 LOGGER.debug(line);475 String[] outputs = line.split("'");476 res[0] = outputs[1]; // package477 res[1] = outputs[3]; // versionCode478 res[2] = outputs[5]; // versionName479 }480 }481 return res;482 }483 public List<String> execute(String[] cmd) {484 return executor.execute(cmd);485 }486 public void setProxy(final String host, final String port, final String ssid, final String password) {487 if (!getOs().equalsIgnoreCase(DeviceType.Type.ANDROID_PHONE.getFamily())) {488 LOGGER.error("Proxy configuration is available for Android ONLY");489 throw new RuntimeException("Proxy configuration is available for Android ONLY");490 }491 if (!isAppInstall(SpecialKeywords.PROXY_SETTER_PACKAGE)) {492 final String proxySetterFileName = "./proxy-setter-temp.apk";493 File targetFile = new File(proxySetterFileName);494 downloadFileFromJar(SpecialKeywords.PROXY_SETTER_RES_PATH, targetFile);495 installApp(proxySetterFileName);496 }497 String deviceUdid = getAdbName();498 LOGGER.debug("Device udid: ".concat(deviceUdid));499 String[] cmd = CmdLine.createPlatformDependentCommandLine("adb", "-s", deviceUdid, "shell", "am", "start", "-n",500 "tk.elevenk.proxysetter/.MainActivity", "-e", "host", host, "-e", "port", port, "-e", "ssid", ssid, "-e", "key", password);501 LOGGER.debug("Following cmd will be executed: " + Arrays.toString(cmd));502 executor.execute(cmd);503 }504 private void downloadFileFromJar(final String path, final File targetFile) {505 InputStream initialStream = Device.class.getClassLoader().getResourceAsStream(path);506 try {507 FileUtils.copyInputStreamToFile(initialStream, targetFile);508 } catch (IOException e) {509 LOGGER.error("Error during copying of file from the resources. ".concat(e.getMessage()));510 }511 }512 public String getAdbName() {513 if (!StringUtils.isEmpty(getRemoteURL())) {514 return getRemoteURL();515 } else if (!StringUtils.isEmpty(getUdid())) {516 return getUdid();517 } else {518 return "";519 }520 }521 /**522 * Related apps will be uninstall just once for a test launch.523 */524 public void uninstallRelatedApps() {525 if (getOs().equalsIgnoreCase(Type.ANDROID_PHONE.getFamily()) && Configuration.getBoolean(Parameter.UNINSTALL_RELATED_APPS)526 && !clearedDeviceUdids.contains(getUdid())) {527 String mobileApp = Configuration.getMobileApp();528 LOGGER.debug("Current mobile app: ".concat(mobileApp));529 String tempPackage;530 try {531 tempPackage = getApkPackageName(mobileApp);532 } catch (Exception e) {533 LOGGER.info("Error during extraction of package using aapt. It will be extracted from config");534 tempPackage = R.CONFIG.get(SpecialKeywords.MOBILE_APP_PACKAGE);535 }536 final String mobilePackage = tempPackage;537 LOGGER.debug("Current mobile package: ".concat(mobilePackage));538 // in general it has following naming convention:539 // com.projectname.app540 // so we need to remove all apps realted to 1 project541 String projectName = mobilePackage.split("\\.")[1];542 LOGGER.debug("Apps related to current project will be uninstalled. Extracted project: ".concat(projectName));543 List<String> installedPackages = getInstalledPackages();544 // extracted package syntax: package:com.project.app545 installedPackages.parallelStream()546 .filter(packageName -> (packageName.matches(String.format(".*\\.%s\\..*", projectName))547 && !packageName.equalsIgnoreCase(String.format("package:%s", mobilePackage))))548 .collect(Collectors.toList()).forEach((k) -> uninstallApp(k.split(":")[1]));549 clearedDeviceUdids.add(getUdid());550 LOGGER.debug("Udids of devices where applciation was already reinstalled: ".concat(clearedDeviceUdids.toString()));551 } else {552 LOGGER.debug("Related apps had been already uninstalled or flag uninstall_related_apps is disabled.");553 }554 }555 556 /**557 * Save xml layout of the application 558 * @param screenshotName - png file name to generate appropriate uix 559 * @return saved file560 */561 public File generateUiDump(String screenshotName) {562 if (isNull()) {...
installApp
Using AI Code Generation
1import com.qaprosoft.carina.core.foundation.webdriver.device.Device;2import com.qaprosoft.carina.core.foundation.webdriver.device.DevicePool;3import com.qaprosoft.carina.core.foundation.webdriver.device.DeviceType;4import com.qaprosoft.carina.core.foundation.webdriver.device.IDevice;5import io.appium.java_client.android.AndroidDriver;6import io.appium.java_client.android.AndroidElement;7import org.openqa.selenium.remote.DesiredCapabilities;8public class Example {9 public static void main(String[] args) throws Exception {10 DevicePool.init(DeviceType.ANDROID, 2);11 IDevice device = DevicePool.getDevice();12 device.installApp("
installApp
Using AI Code Generation
1import com.qaprosoft.carina.core.foundation.webdriver.device.Device;2Device device = new Device();3device.installApp("src/test/resources/AndroidTestApp.apk");4device.uninstallApp("com.qaprosoft.carina.demo");5device.launchApp("com.qaprosoft.carina.demo");6device.stopApp("com.qaprosoft.carina.demo");7boolean isInstalled = device.isAppInstalled("com.qaprosoft.carina.demo");8boolean isRunning = device.isAppRunning("com.qaprosoft.carina.demo");9List<String> installedApps = device.getInstalledApps();
installApp
Using AI Code Generation
1Device device = DevicePool.getDevice();2File app = new File("src/main/resources/demo.apk");3device.installApp(app);4String packageName = device.getPackageName(app);5String activityName = device.getActivityName(app);6device.startApp(packageName, activityName);7device = DevicePool.getDevice()8app = File("src/main/resources/demo.apk")9device.installApp(app)10packageName = device.getPackageName(app)11activityName = device.getActivityName(app)12device.startApp(packageName, activityName)13$device = DevicePool::getDevice();14$app = new File("src/main/resources/demo.apk");15$device->installApp($app);16$packageName = $device->getPackageName($app);17$activityName = $device->getActivityName($app);18$device->startApp($packageName, $activityName);19device := DevicePool.getDevice()20app := new File("src/main/resources/demo.apk")21device.installApp(app)
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!!