javaautomated-testsappiumvisual-studio-app-center-test

Appium on App Center can't switch between iOS and Android (Java)


When I write appium tests specifically to run on App Center (and therefore must use the custom 'Enhanced Driver'), it looks like I can only declare driver as either an EnhancedAndroidDriver type, or an EnhancedIOSDriver type.

    public EnhancedAndroidDriver<MobileElement> driver;
//    public EnhancedIOSDriver<MobileElement> driver; <----- can't declare same variable twice

public AppiumDriver<MobileElement> getDriver() throws IOException {
        String PLATFORM_NAME = System.getenv("XTC_PLATFORM");
        if (PLATFORM_NAME.equals("Android")) {
            EnhancedAndroidDriver<MobileElement> androiddriver = Factory.createAndroidDriver(new URL("http://localhost:4723/wd/hub"), caps);
            driver = androiddriver;
        } else if (PLATFORM_NAME.equals("iOS")) {
            EnhancedIOSDriver<MobileElement> iosdriver = Factory.createIOSDriver(new URL("http://localhost:4723/wd/hub"), caps);
            driver = iosdriver;  <---- compiler error, wrong type
        }
        return driver;
    }

I want to run a simple test in a single file that will run on both platforms, but it seems I must choose either android or ios for that file to run on. How do I avoid duplicating all my test files? I am using react native and my app is basically identical on both platforms. I have done something similar with the regular AppiumDriver.

Any suggestions to programmatically switch which type 'EnhancedIOS/AndroidDriver' the variable driver refers to in Java??

With the regular AppiumDriver, I can do this:

private static AppiumDriver<MobileElement> driver;

public AppiumDriver<MobileElement> getDriver() throws IOException {
    if (PLATFORM_NAME.equals("Android")) {
       driver = new AndroidDriver<MobileElement>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
    } else if (PLATFORM_NAME.equals("iOS")) {
       driver = new IOSDriver<MobileElement>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
    }
    return driver;
}

And then use the driver in the tests generically. But it seems impossible to take this approach with the App Center Enhanced drivers because they don't share a common type (or I don't know enough about Java to figure it out). Is there any way to work around this??


Solution

  • I ended up writing an Interface that wrapped the driver, which instantiated and operated on the platform specific driver for each driver method I use, and allowed me to use the common name driver to refer to it throughout the rest of the test framework.

    I passed in the platform name to the Interface when I instantiated the interface. The platform name was read as an environment variable I set in the App Center branch build settings.