All Downloads are FREE. Search and download functionalities are using the official Maven repository.

android.AndroidDeviceHandler Maven / Gradle / Ivy

There is a newer version: 1.3.0
Show newest version
package android;

import core.asserts.AssertString;
import core.os_ops.Services;
import core.reports.TestReporter;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.AndroidMobileCapabilityType;
import io.appium.java_client.remote.MobileCapabilityType;
import io.appium.java_client.service.local.AppiumDriverLocalService;
import org.openqa.selenium.net.PortProber;
import org.openqa.selenium.remote.DesiredCapabilities;

import static core.files.StringHandler.cleanStringFromReturn;
import static android.AndroidDriverInitializer.androidDriver;

/**
 * Created by Ismail on 1/9/2018.
 * This class contains methods that creates and configure
 * Android emulator to be used in running mobileType Android test cases
 */
public class AndroidDeviceHandler {

    /*************** Class Variables Section ***************/
    private static Boolean avdCreationStatus = false;
    private static int avdRunningPort = 0;

    /*************** Class Methods Section ***************/
    // This method to Create a specific Emulator to be used in running Android mobileType test cases on virtual devices
    public static void createAVD(String emulatorName) {
        // Save Package level to use in creating AVD
        String packageLevel = Services.getProcessOutput("avdmanager create avd -n " + emulatorName, "android-P;google_apis;x86");
        // Check packageLevel is empty or has a value
        AssertString.assertNotEmptyString(packageLevel, "Error: Package path (-k) not specified.");
        // Create a new AVD with Services.getProcessOutput command
        Services.getProcessOutput("echo no | avdmanager create avd -n " + emulatorName + " -k " + packageLevel + " -d \"Nexus 5X\" -f");
        // Mark avdCreationStatus as true to delete any created AVD
        avdCreationStatus = false;
    }

    // This method to launch Emulator or Real Device to run Android app test cases
    public static void startAndroidDevice(AppiumDriverLocalService appiumDriverLocalService, String deviceType, String mobileName, String mobileApp) {
        // Check if mobileName is a emulator and not exist to create
        if (!isRealDevice(deviceType) && !isExistingEmulator(mobileName)) {
            // If emulator isn't exist then create one
            createAVD(mobileName);
        }
        // Assign Capabilities for Android
        DesiredCapabilities desiredCapabilities = assignCapabilities(deviceType, mobileName, mobileApp);
        // Enable show touches setting
        enableAVDSettings("show_touches 1");
        // Start Android Driver and setUp app on the device
        try {
            androidDriver = new AndroidDriver<>(appiumDriverLocalService.getUrl(), desiredCapabilities);
        } catch (Throwable throwable) {
            TestReporter.error("Something went wrong while initializing Android Driver, Please check Message: 
" + throwable.getMessage(), true); } } private static void enableAVDSettings(String... settings) { for (String setting : settings) { Services.getProcessOutput("adb shell settings put system " + setting); } } // This method to delete a specific Emulator public static void deleteAVD(String emulatorName) { if (!isRealDevice(emulatorName) && avdCreationStatus == true) { // Stop emulator Services.getProcessOutput("adb -s " + "emulator-" + avdRunningPort + " emu kill"); // Delete emulator Services.getProcessOutput("avdmanager delete avd -n " + emulatorName); } // Change avdCreationStatus to false avdCreationStatus = false; } // This method to check if Emulator is already exists private static Boolean isExistingEmulator(String emulatorName) { // Return Existing Emulators in the system String emulatorsString = Services.getProcessOutput("emulator -list-avds"); // Check If emulatorsString not empty then convert emulators name to array else will assign empty array to emulators array String[] emulators = !emulatorsString.isEmpty() ? emulatorsString.split("\\n") : new String[]{}; // Check array items and verify if emulatorName exist to return true for (int counter = 0; counter < emulators.length; counter++) { if (emulators[counter].equalsIgnoreCase(emulatorName)) return true; } // If the emulatorName isn't exist then return false return false; } // This method to check if mobileName is real device or emulator private static Boolean isRealDevice(String deviceType) { return deviceType.equalsIgnoreCase("real") ? true : false; } // This method to assign Capabilities for mobileType private static DesiredCapabilities assignCapabilities(String deviceType, String mobileName, String mobileApp) { // Check required params is valid AssertString.assertNotEmptyString(deviceType, "Please provide Device Type value as Real or Emulator"); AssertString.assertNotEmptyString(mobileName, "Please provide Mobile Name value"); AssertString.assertNotEmptyString(mobileApp, "Please provide Mobile App path value"); // Initialize desiredCap and start assign to DesiredCapabilities desiredCapabilities = new DesiredCapabilities(); // Assign PLATFORM_NAME as Android desiredCapabilities.setCapability(AndroidMobileCapabilityType.PLATFORM_NAME, "android"); // Assign DEVICE_NAME with provided mobile name desiredCapabilities.setCapability(MobileCapabilityType.DEVICE_NAME, mobileName); // Flag device to clear system files desiredCapabilities.setCapability(MobileCapabilityType.CLEAR_SYSTEM_FILES, false); // Flag device with FULL_RESET option desiredCapabilities.setCapability(MobileCapabilityType.FULL_RESET, false); // Assign Mobile app desiredCapabilities.setCapability(MobileCapabilityType.APP, mobileApp); // Assign application package name desiredCapabilities.setCapability(AndroidMobileCapabilityType.APP_PACKAGE, getAndroidAppPackage(mobileApp)); // Assign application activity name desiredCapabilities.setCapability(AndroidMobileCapabilityType.APP_ACTIVITY, getAndroidAppActivity(mobileApp)); // Check if device running then no need for new port if (!isExistingEmulator(mobileName)) desiredCapabilities.setCapability(AndroidMobileCapabilityType.ADB_PORT, avdRunningPort = PortProber.findFreePort()); // If device provided is emulator then flag it as emulator if (deviceType.equalsIgnoreCase("emulator")) desiredCapabilities.setCapability("avd", mobileName); // Return DesiredCapabilities variable return desiredCapabilities; } // This method return AppPackage value from Android application APK private static String getAndroidAppPackage(String mobileApp) { // Define String variable to save AppPackage value String appPackage; // Define Command String that will extract appPackage from APK file view command line String commandString = "aapt dump badging \"" + mobileApp + "\" | grep package | awk '{ print $2 }'"; // Save appPackage value from APK file appPackage = Services.getProcessOutput(commandString); // Cleaning appPackage value appPackage = cleanStringFromReturn(appPackage); appPackage = appPackage.replace("name=", "").replaceAll("'", ""); // Return appPackage value return appPackage; } // This method return AppActivity value from Android application APK private static String getAndroidAppActivity(String mobileApp) { // Define String variable to save AppActivity value String AppActivity; // Define Command String that will extract AppActivity from APK file view command line String commandString = "aapt dump badging \"" + mobileApp + "\" | grep activity | awk '{ print $2 }'"; // Save AppActivity value from APK file AppActivity = Services.getProcessOutput(commandString); // Cleaning AppActivity value AppActivity = cleanStringFromReturn(AppActivity); AppActivity = AppActivity.replace("name=", "").replaceAll("'", ""); // Return AppActivity value return AppActivity; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy