So in pursuing ways to allow the sharing of variable values between one test case to another within a Test Suite, my colleagues and I have found two different methodologies and are wondering if there is any specific advantage of one over the other.
The first method tried would be using a metaclass, where we have a Keyword method setup for instantiating the class within the “Common_Functions” Keyword:
public void create_global_variable(String name, def value) {
GroovyShell shell = new GroovyShell()
MetaClass mc = shell.evaluate("internal.GlobalVariable").metaClass
String getterName = "get" + name.capitalize()
mc.'static'."$getterName" = { -> return value }
mc.'static'."$name" = value
}
and it’s corresponding usage in the Test Suite :
def setupTestCase() {
KeywordUtil log = new KeywordUtil()
Common_Functions sites = new Common_Functions()
//Establish Run Variables
String user_login_id = 'myID'
String site_name = 'mySite'
//Establish Global Variables
sites.create_global_variable('site_status', 'not set')
sites.create_global_variable('user_login', user_login_id)
sites.create_global_variable('current_site', site_name)
//Perform Login Action
Login user_login = new Login()
user_login.login(GlobalVariable.metaClass.getProperty(GlobalVariable, 'user_login'), System.getenv('My_Password'))
Navigation menu = new Navigation()
String site_set = menu.select_site( GlobalVariable.metaClass.getProperty(GlobalVariable, 'current_site'))
sites.create_global_variable('site_status', 'site_set')
}
On the other hand we have a Map variable in Keyword Shared_Vars:
Map glblvars = ['site_name':'','user_name':'','object_timeout':'', 'site_status':'']
and its setup logic:
def setupTestCase() {
Shared_Vars myvars = new Shared_Vars()
KeywordUtil log = new KeywordUtil()
//Establish Global Variables
myvars.glblvars.put('site_name', 'MySite')
myvars.glblvars.put('user_name', 'MyUserName')
myvars.glblvars.put('site_status', 'not set')
//Perform Login Action
Login user_login = new Login()
user_login.login(myvars.glblvars.get('user_name'), System.getenv('MyPass'))
Navigation menu = new Navigation()
String site_set = menu.select_site(myvars.glblvars.get('site_name'))
myvars.glblvars.put('Global site_status 2: ', site_set)
log.logInfo("site_status: " + myvars.glblvars.get('site_status'))
}
So the question is if - other than personal preference - there would be a fundamental reason to prefer a specific methodology?
In both cases the understanding is that the respective Keyword would need to be present in all leveraging functions, and that relatively speaking the difference in coding is fairly miniscule.