How to initialize the driver so it can be used by all classes

Hi All, I am new to Appium,

I am writing a framework in JAVA using Appium, Selenium and Cucumber.

I start off by declaring an Appium Driver in one of my test step files and then this gets cast to an Android Driver or iOS Driver depending on the app under test.

I need some help please - I need all of my class files to have access to this instance of the driver but I’m not sure how to do this. The test is driven from the feature file and some of the test steps are in different class files so how can they all access this instance of the driver?


@Matt882 start with AppiumDriver. Which you cast later to iOS or Android driver. e.g.:

    protected AppiumDriver driver = null;
    AndroidDriver androidDriver = (AndroidDriver) driver;
    IOSDriver iosDriver = (IOSDriver) driver;

    // or assign correct (iOS/Android) driver with driver start
    driver = new IOSDriver<>(new URL(url), capabilities);

Thanks Aleksei,

I want the driver to always be called driver as I don’t want to write separate code for IOS or Android…can I just cast the the initial AppiumDriver driver to AndroidDriver or iOSDriver?

Yes. Use AppiumDriver which assign iOSDriver or Android driver during initialize.

Thanks Aleksei.

I have tried protected AppiumDriver driver = null; but when I then try to access AppiumDriver from other classes (in other packages) it is not recognised.

If I used public static AppiumDriver driver = null;

I can then access driverfrom other classes by importing the class (called for example InitialiseDriver) and then using InitialiseDriver.driver

For info, the way I ended up doing this is creating a new class called FrameworkInitialize which declares and instantiates an AppiumDriver called driver.

public class FrameworkInitialize {
public static AppiumDriver driver = null;

I can then use driver in other classes by either extending the class:
public class MattsTestSteps extends FrameworkInitialize {

or if you can’t extend as the class is already extended, create a new instance of the driver which is a copy of the original driver as shown below:

private AppiumDriver driver = FrameworkInitialize.driver;


  • You should have base class for test code where you have this driver.
  • All test classes should extend base class.

With page objects you should pass driver variable to it.

1 Like

Hi Aleksei,
It is not clear to me how can I achieve this.
I am working with Appium 2.0.0-beta.57, java-client 8.3.0, selenium-java 4.8.1.
Let me put an example:

AppiumDriver driver;
driver.context("NATIVE_APP"); ->  error: cannot find symbol
((IOSDriver)driver).context("NATIVE_APP");  -> works
((AndroidDriver)driver).context("NATIVE_APP");  -> works

I would like to write the same code for the IOSDriver and AndroidDriver, not needing to downcast every time and duplicate code.
Before upgrading to java-client 8.X, I could just use an AppiumDriver instance.
Is there any way I could achieve this?

I just saw this: Switch between Android Driver and iOS Driver in a single test (Appium)