问题
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 ).
This Base Class I inherited in all testng classes, now for each test class Appium new session generated.
So My question is that How we maintain appium session through out the all class once it generate for any class.
Thanks Sadik
回答1:
I have implemented this approach using Singlton design pattern here is approach:
public class SingltonFactory{
private static SingltonFactory instance = new SingltonFactory();
private static AppiumDriver<MobileElement> driver;
private SingltonFactory() {
}
// Get the only object available
public static SingltonFactory getInstance() {
return instance;
}
// Get the only object available
public void setDriver(AppiumDriver<MobileElement> driver1) {
driver = driver1;
}
public AppiumDriver<MobileElement> 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<MobileElement>(new URL("http://127.0.0.1:4723/wd/hub"), cap);
}
else{
driver = appiumFactory.getAppiumDriver();
}
回答2:
You can achieve the same without inheriting base class.
Step1 -> Initialize driver object in your set up i.e your setup method as driver=new AndroidDriver(url,caps);
Step 2-> If you have any Utility or Constant class, call its constructor and pass driver as a parameter e.g. new Constants(driver);
Note: You can mark driver as a static variable if you only need one driver instance.
Step 3-> Whenever you wish to access driver, access it as Constant.driver or initialize driver object at class level.
来源:https://stackoverflow.com/questions/34743214/how-to-preserve-appium-session-between-multiple-testng-class