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

android.AndroidDriverInitializer Maven / Gradle / Ivy

The newest version!
package android;

import io.appium.java_client.MobileDriver;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.service.local.AppiumDriverLocalService;
import io.appium.java_client.service.local.AppiumServiceBuilder;
import org.openqa.selenium.support.ui.FluentWait;
import org.openqa.selenium.support.ui.WebDriverWait;

import static android.AndroidDeviceHandler.deleteAVD;
import static android.AndroidDeviceHandler.startAndroidDevice;

/**
 * Created by Ismail on 1/5/2018.
 * This Class contains methods related to manage MobileDriver
 * to run, initialize and stop MobileDriver instance for Mobile applications
 */
public class AndroidDriverInitializer {
//DONE
    /*************** Class Variables Section ***************/
    // This variable is an AppiumDriverLocalService object to run appium Service
    private static AppiumDriverLocalService appiumDriverLocalService = null;
    // This variable is an AppiumServiceBuilder object to build appium service variants
    private static AppiumServiceBuilder appiumServiceBuilder = null;
    // Define static androidDriver Object
    public static MobileDriver androidDriver = null;
    // Define static android driver wait object
    public static FluentWait wait = null;
    // Define Mobile name value
    public static String mobileName = "";
    // Define Device Type value (real / emulator)
    public static String deviceType = "emulator";
    // Define mobileApp value
    public static String mobileApp = "";
    // Define emulatorSettingsFile value (This value is a file providing full settings for a emulator)
    public static String emulatorSettingsFile = "";

    // Define Mobile Name Parameter name
    public static final String mobileNameParam = "mobileName";
    // Define Device Type Parameter name
    public static final String deviceTypeParam = "deviceType";
    // Define mobileApp Parameter name
    public static final String mobileAppParam = "mobileApp";
    // Define emulatorSettingsFile Parameter name
    public static final String emulatorSettingsFileParam = "emulatorSettingsFile";

    /*************** Class Methods Section ***************/
    // This method to run WebDriver as Mobile Application
    // and open specific Simulator/Emulator for application
    public static void setUp() {
        // Start Appium server session
        startAppiumServer();
        // Create and Running Android Emulator
        startAndroidDevice(appiumDriverLocalService, deviceType.toLowerCase(), mobileName, mobileApp);
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    // This method to close Emulator then delete it and stop Appium server
    public static void tearDown() {
        // Delete Android Emulator
        deleteAVD(mobileName);
        // Stop Appium server session
        stopAppiumServer();
    }

    // This method to configure and run Appium server session
    private static void startAppiumServer() {
        /*************** Method Variables Section ***************/
        // Define APPIUM IP ADDRESS to run the service
        final String APPIUM_IP_ADDRESS = "0.0.0.0";
        // Define APPIUM PORT to run service
        final int APPIUM_PORT = 4723;

        // Build appium service variants
        appiumServiceBuilder = new AppiumServiceBuilder();
        // Assign IP Address for appiumServiceBuilder
        appiumServiceBuilder.withIPAddress(APPIUM_IP_ADDRESS);
        // Assign a free port for appiumServiceBuilder
        appiumServiceBuilder.usingPort(APPIUM_PORT);

        //Start Appium Local Service by initializing an object
        // then assign appiumServiceBuilder param to object
        appiumDriverLocalService = AppiumDriverLocalService.buildService(appiumServiceBuilder);
        // Start appiumDriverLocalService
        appiumDriverLocalService.start();
    }

    // This method to stop Appium server session
    private static void stopAppiumServer() {

    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy