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

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:


AndroidDriver<MobileElement> driver = [..]


IOSDriver<MobileElement> driver = [..]


AndroidDriver


iOSDriver


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


driver




2 Answers
2



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


WebDriver


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


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;









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.