Hello,
there were few requests to add an ability to change GlobalVariable permanently.
I wrote a custom keyword which can do it.
Works with Katalon Studio 6.3.0
Disclaimer: I am not responsible for any damages caused by this code. This method modifies profile file in your project folder and changes are permanent. Make sure you have proper backup of all your files in any case.
Code:
Click me
import java.nio.file.Files
import java.nio.file.Paths
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.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
/**
* @author mmelocik
*
*/
public class GlobalVariableUpdater {
/** WARNING - Permanently updates a variable in currently used execution profile.<br>
* Original value will be replaced with new value and cannot be restored.
* @param varName
* @param newValue
*/
public static void updatePermanently(String varName, String newValue) {
updatePermanently(RunConfiguration.getExecutionProfile(), varName, newValue)
}
/** WARNING - Permanently updates a variable in specified execution profile.<br>
* Original value will be replaced with new value and cannot be restored.
* @param envName
* @param varName
* @param newValue
*/
public static void updatePermanently(String envName, String varName, String 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) {
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.")
}
}
Usage:
// change selected profile
GlobalVariableUpdater.updatePermanently("myProfileName", "VariableName", "new value")
// change current profile
GlobalVariableUpdater.updatePermanently("VariableName", "new value")