Inside @BeforeTest annotation create objects for all of your framework classes for.eg inside code below i have a class named automationdriver inside constructor of which i initialize objects of all of my framework class
Like:
public class AutomationFactory
{
HashMap hmConfiguration=new HashMap();
public AppiumDriver driver;
public BaseInitializerHub objBaseInitializerHub;
public ObserverSubject objSubject;
public AutomationDriver automationDriver;
@BeforeTest/**/Here you create objects of framework classes, store them in a hashmap also here you attach objects with the resp. subject using observer pattern**
public void beforeTest()
{
attachSubjects(testContext);
new AppiumSessionManager().startAppiumServer(IP,PORT,deviceName,logfilepath,bootstrapport);
AutomationDriver automationDriver=initializeAutomationDriver(os,logpath, PORT,appendername,configurationMap.get(InitializerConfig_),UDID_,deviceGroup,InitializerConfig_);
hmConfiguration.put(configuration, automationDriver);
}
public void attachSubjects(ITestContext testContext)
{
objSubject=new ObserverSubject();//make these global
for (ITestNGMethod method : testContext.getAllTestMethods()) {
method.setRetryAnalyzer(new MyRetryAnalyzer(objSubject));
}
new AppiumHelper(objSubject);//make these global
new Log(objSubject);//make these global}
}
@BeforeMethod//Here you load the configuration which you want to use
public void beforemethod()
{
automationDriver=(AutomationDriver) hmConfiguration.get(confiration);
driver=new AppiumSessionManager().initializeAppiumDriver(deviceName_, UDID_, platformVersion_, URL_,hubHost,hubPort,os,bundleId);
automationDriver.appiumDriver=driver;
objSubject.setAutomationDriverState(automationDriver);
}
In case you create all framework objects in your test scripts than you do not need these but in case you have helpers which internally use appiumdriver than this way you can achieve parallel execution.