Search code examples
javaappium

Dynamically choose driver type in Appium in order to write 'hybrid' tests


I use Appium with Java to automate tests for mobile applications. It is clear that when I want to write tests for Android I use AndroidDriver<MobileElement> driver = [..] and for iOS I need to use IOSDriver<MobileElement> driver = [..] though with this approach I'd need to write same tests twice for iOS and Android. Is there a way that I could choose type of Appium Driver dynamically based on i.e. some kind of variable to choose between AndroidDriver and iOSDriver? I tried:

if(platform == "Android"){
    //returns AndroidDriver
    AppiumDriver<MobileElement> driver = COMMON.startAndroid(name, id, platform, version);
} else {
    //returns IOSDriver
    AppiumDriver<MobileElement> driver = COMMON.startIOS(name, id, platform, version);
}

but below in Test Eclipse points out that with this approach driver is not defined


Solution

  • Both of those drivers extend WebDriver interface (via inheritance). You can define driver from this type. It is also OOP encapsulation concept

    WebDriver driver;
    if(platform.equals("Android")){
        driver = COMMON.startAndroid(name, id, platform, version);
    } else {
        driver = COMMON.startIOS(name, id, platform, version);
    }