How to preserve Appium session between multiple testng class

后端 未结 3 1393
北荒
北荒 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<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();
    }
    
    0 讨论(0)
  • 2021-01-03 17:03

    I also have same requirement, its a Gradle project and i use gradle script to execute my testcases, Requirement: Have to preserve or have to retrieve an Appium session (driver) using session ID So that we can avoid re initializing each time between testcases

    Environment: Multiple test cases(>50 scenarios) run based on the source tag in sequence. Gradle script include parameters App on android dos`t need a re-initialize each time

    What did i try: 1. Tried to Write driver info to a file and retrive each time when i start a new scenario(setSessionID class in private in Appium)

    Create Appium(local) class that can use session id + Appium APIs , but that is hectic and will end up maintaining something already is available

       DesiredCapabilities dc = new DesiredCapabilities();
        //migrate to gradle params later after demo
        dc.setCapability("deviceName", "XX30");
        dc.setCapability("udid", System.getProperty("hubUdid"));
        dc.setCapability("appPackage", "com.bbbbb.bbb.bbb.primary");
        dc.setCapability("appActivity", "..Activity");
        dc.setCapability("autoLaunch",false);
        dc.setCapability("dontStopAppOnReset",true);
        dc.setCapability("noReset", "true");
        driver_hub = Android.initialize(new URL("http://127.0.0.1:4723/wd/hub"), dc);
    
    0 讨论(0)
  • 2021-01-03 17:12

    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.

    0 讨论(0)
提交回复
热议问题