I am automating Android application using Appium, I have One Base Class with Setup and Tear down (In setup initialization appium session and in teardown destroying session )
I have implemented this approach using Singlton design pattern here is approach:
public class SingltonFactory{
private static SingltonFactory instance = new SingltonFactory();
private static AppiumDriver driver;
private SingltonFactory() {
}
// Get the only object available
public static SingltonFactory getInstance() {
return instance;
}
// Get the only object available
public void setDriver(AppiumDriver driver1) {
driver = driver1;
}
public AppiumDriver getAppiumDriver() {
return driver;
}
}
Add initialize SingltonFactory in your before test cases and assign driver object like below:
AppiumFactory appiumFactory = AppiumFactory.getInstance();
if(appiumFactory.getAppiumDriver() == null) {
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), cap);
}
else{
driver = appiumFactory.getAppiumDriver();
}