How to preserve Appium session between multiple testng class

后端 未结 3 1392
北荒
北荒 2021-01-03 16:18

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 )

3条回答
  •  孤城傲影
    2021-01-03 16:57

    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();
    }
    

提交回复
热议问题