No guarantee but you can try the below. Just add it below the current code from Marek. This overloads his method to work on a Map variable.
I ran it with:
println("Initial Map " + GlobalVariable.whatnot);
//(GlobalVariable.whatnot as Map).put('home', "blah");
(GlobalVariable.whatnot as Map).put('home', "mona lisa");
println("Updated Map " + GlobalVariable.whatnot);
// change selected profile
CustomKeywords.'com.GlobalVariableUpdater.updatePermanently'("test", "whatnot", GlobalVariable.whatnot as Map)
Edit: this is based on a Map I created as in my post above:
<GlobalVariableEntity>
<description></description>
<initValue>[('bill') : 'debit', ('home') : 'debit', ('money') : 'credit']</initValue>
<name>whatnot</name>
</GlobalVariableEntity>
Make sure you have a backup copy of your Profiles before “playing” with the below.
Code:
import java.nio.file.Files
import java.nio.file.Paths
import java.lang.StringBuilder
import javax.xml.parsers.DocumentBuilder
import javax.xml.parsers.DocumentBuilderFactory
import javax.xml.transform.OutputKeys
import javax.xml.transform.Transformer
import javax.xml.transform.TransformerFactory
import javax.xml.transform.dom.DOMSource
import javax.xml.transform.stream.StreamResult
import org.apache.commons.lang3.StringUtils
import org.w3c.dom.Document
import org.w3c.dom.Element
import org.w3c.dom.NodeList
import com.kms.katalon.core.configuration.RunConfiguration
import com.kms.katalon.core.util.KeywordUtil
import com.kms.katalon.core.annotation.Keyword
/** WARNING - Permanently updates a Map variable in specified execution profile.<br>
* Original value will be replaced with new value and cannot be restored.
* @param envName
* @param varName
* @param newValue
*/
@Keyword
public void updatePermanently(String envName, String varName, Map newValue) {
File inputFile = new File(RunConfiguration.getProjectDir() + "//Profiles//" + envName + ".glbl")
if(!Files.exists(Paths.get(inputFile.getAbsolutePath()))) {
KeywordUtil.markFailed("A file with profile was not found - check path: " + inputFile.getAbsolutePath())
}
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance()
DocumentBuilder builder = factory.newDocumentBuilder()
Document document = builder.parse(inputFile)
NodeList elems = document.getDocumentElement().getElementsByTagName("GlobalVariableEntity")
for (Element elem in elems) {
if (elem.getElementsByTagName("name").item(0).getTextContent() == varName) {
StringBuilder myStr = new StringBuilder();
myStr.append("[")
// Traversing through Map using for-each loop
for (Map.Entry<String, String> me : newValue.entrySet()) {
myStr.append("('")
myStr.append(me.getKey())
myStr.append("') : '")
myStr.append(me.getValue())
myStr.append("',")
}
//println("so far it is " + myStr.toString())
myStr.deleteCharAt(myStr.length() - 1)
//println("now it is " + myStr.toString())
myStr.append("]")
//println("finally it is " + myStr.toString())
elem.getElementsByTagName("initValue").item(0).setTextContent(myStr.toString())
// elem.getElementsByTagName("initValue").item(0).setTextContent("'" + newValue + "'")
document.getDocumentElement().normalize()
Transformer transformer = TransformerFactory.newInstance().newTransformer()
DOMSource source = new DOMSource(document)
StreamResult result = new StreamResult(inputFile)
transformer.setOutputProperty(OutputKeys.INDENT, "yes")
transformer.transform(source, result)
return
}
}
KeywordUtil.markWarning("Global variable with name " + varName + " was not found.")
}