Trying to append a string variable to Global variable

Hi,
This could be a weird case but I want to know if this is possible. I have two pages that have the same fields. I need to use excel to save data and enter in those fields. In the script, I am trying to reuse the same code but replacing the variable that read from the excel.

reading excel script
getExcelData(String weekNum){
String[] dataValues
dataValues = [weekNum+‘partyID’, weekNum+‘firstName’, weekNum+‘lastName’]
returnDatavalues = CustomKeywords.‘readExcel.excelData.getdata’(1, dataValues, ‘claimant_info’)

    for (int i = 0; i < dataValues.length; i++) {
        addGlobalVariable(dataValues[i], returnDatavalues[i])
    }

}

in the script
String weekno=‘wk_1_’
getExcelData(weekno){
enterWeekdetails(weekno)
weekno=‘wk_2_’
getExcelData(weekno){
enterWeekdetails(weekno)

static enterWeekdetails(String weekno){
WebUI.click(findTestObject(‘Object Repository/Page_Unemployment Insurance/radio_btn’, [(‘questionName’) : ‘Select Party Id’
, (‘radio_option’) : GlobalVariable.weekno+partyID]))
}

Thanks

I’m not looking too deeply here, but you can store “anything” in a GlobalVariable - even a Map, which I think is what you have (disclaimer: I don’t use DDT or Excel)

GlobalVariable.myMap = readExcel.excelData...

If your excel reader returns a Map, you’re good to go.

@Russ_Thomas, I saw your solution for similar thing here link

how can I create globalvariable for map type at runtime. Currently I am using the following script to create globalvariable for string

static void addGlobalVariable(String name, def value) {
GroovyShell shell1 = new GroovyShell()
MetaClass mc = shell1.evaluate(“internal.GlobalVariable”).metaClass
String getterName = “get” + name.capitalize()
String setterName = “set” + name.capitalize()
mc.‘static’."$getterName" = { -> return value }
mc.‘static’."$setterName" = { newValue -> value = newValue }
mc.‘static’."$name" = value
}

First create a bare Map in your profile settings.

Then, in code, assign the excel data (map) at runtime.

@Russ_Thomas, thanks for your reply,
I have created the execution profile and added a new Map variable. When I try to print I am getting nullpointer exception when specify GlobalVariable.

If I try without GlobalVariable then I am getting MissingProperty
I think I am missing something

What does this do?

    Map myMap = ["Name": "Russ", "Current_State": "Confused"]
    
    println "*********************************" + myMap["Name"] + " is " + myMap["Current_State"]

Place that right before the problem line.

ok, this one worked and printed as follows
2020-06-05 21:10:03.602 DEBUG testcase.testData_readexcel - 4: println("*********************************" + myMap[“Name”] + " is " + myMap[“Current_State”])
*********************************Russ is Confused
2020-06-05 21:10:03.618 DEBUG testcase.testData_readexcel - 5:

Thanks so much this one helps me a lot of places

1 Like