0

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

Uliysess
  • 579
  • 1
  • 8
  • 19

2 Answers2

1

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);
}
Guy
  • 46,488
  • 10
  • 44
  • 88
0
public class AppiumController {


public static OS executionOS = OS.ANDROID;
public AppiumDriver<MobileElement> driver;
DesiredCapabilities capabilities = new DesiredCapabilities();

public enum OS {
    ANDROID,
    IOS,
    MOBILEBROWER_IOS,
    MOBILEBROWER_ANDROID
}

public void start() {

    if (driver != null) {
        return;
    }

    switch(executionOS){
    case ANDROID: 
        // set android caps
        driver = new AndroidDriver<MobileElement>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);

    case IOS:
        // set ios caps
        driver = new IOSDriver<MobileElement>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);

    case MOBILEBROWER_IOS:
        // set ios browser caps
        driver = new IOSDriver<MobileElement>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);

    case MOBILEBROWER_ANDROID:
        // set android browser caps
        driver = new AndroidDriver<MobileElement>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);

    }
}

public void stop() {
    if (driver != null) {
        driver.quit();
        driver = null;
    }
}

}
Amit Jain
  • 4,389
  • 2
  • 18
  • 21