09
Jan

Initializing iOS or Android drivers

public static Appiumdriver driver;

AppiumServiceBuilder serviceBuilder = new AppiumServiceBuilder().usingAnyFreePort();
service=AppiumDriverLocalService.buildService(serviceBuilder);
service.start();
driver = (isTargetAndroid()) ? getAndroidDriver(service.getUrl(), false, false) : getIOSDriver(service.getUrl(), false, false);


where isTargetAndroid() is a private method in my code which returns whether the script was started with android or ios as target. ((PLATFORM_NAME.equals("ios") or PLATFORM_NAME.equals("android"))
the getAndroidDriver and getIOSDriver start the relevant driver.

    public static AppiumDriver getAndroidDriver(URL appiumUrl, boolean fullReset, boolean noReset) {
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION, "**TBD**");
        capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "**TBD**");
        capabilities.setCapability(MobileCapabilityType.APP, appUrl);
        capabilities.setCapability(AndroidMobileCapabilityType.NO_SIGN,true); 
        capabilities.setCapability(AndroidMobileCapabilityType.UNICODE_KEYBOARD,true); //set unicodekeyboard in order to support umlauts 
        capabilities.setCapability(AndroidMobileCapabilityType.RESET_KEYBOARD,true); //reset keyboard after tests
        capabilities.setCapability(AndroidMobileCapabilityType.IGNORE_UNIMPORTANT_VIEWS,true); //speeds up tests, as views that are not important i.e. for accessiblity are not shown on android. Is disabled on demand in tests using driver.ignoreunimporantViews


        //note that setting appPackage and appActivity is not necessary starting from API level 19 (i.e. 4.4) as it is automatically detected
        //should not be necessary anymore, but detected default is omg.abc.SplashActivity which is never shown on certain flavours of the app
        capabilities.setCapability(AndroidMobileCapabilityType.APP_WAIT_ACTIVITY, "omg.abc.android.bdf.MainActivity"); 

        //only on simulator;
        if(simOrEmulator) {
            capabilities.setCapability("language", language);
        }

        if (fullReset) {
            capabilities.setCapability("fullReset", fullReset);
        }
        if (noReset) {
            capabilities.setCapability("noReset", noReset);
        }

        return new AndroidDriver(appiumUrl,capabilities);
    }

    public static AppiumDriver getIOSDriver(URL appiumUrl, boolean fullReset, boolean noReset) {
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION, "9.3");
        capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "iPhone 6");
        capabilities.setCapability(MobileCapabilityType.APP, appUrl);
        capabilities.setCapability("bundleId", "**TBD**"); //todo set from script

        //only on simulator;
        if(simOrEmulator) {
            capabilities.setCapability("language", language);
        }

        capabilities.setCapability("locationServicesAuthorized", true);
        capabilities.setCapability("locationServicesEnabled", true);
        capabilities.setCapability("autoAcceptAlerts", true);

        if (fullReset) {
            capabilities.setCapability("fullReset", fullReset);
        }
        if (noReset) {
            capabilities.setCapability("noReset", noReset);
        }
        return new IOSDriver(appiumUrl,capabilities);
    }