Hi team,
Our team are trying to create a variable to count the failed scripts by bugs then pass it to the Email Template on Project → Settings → Email → Templates. We have tried with 2 options:
- Create Global Variable then pass it to Email Template: When the test
suite completed executing then the variable is reset to the original value
- Create a variable in Test Listener: We have tried to create a variable in a Test Listener then tried to call it in the Email Template but we got the error “No such property” when the Katalon converting the email to send it.
Is there any ways to keep the Global Variable value after executing the test suite or pass the variable from Test Listener to the Emal Template of Katalon? Thanks
2 Likes
Hi,
I found this following guide: Share test reports via email in Katalon Studio | Katalon Docs. Can you please follow it? Thank you!
Thanks for the response. We tried that way but the issue but the issue we got is after the Test Suite complete executing, the value of GlobalVariables is reset to original value, so we cannot pass the latest value to the Email body
As an alternative: Did you try creating a custom keyword which you can store data to a file and then fetch it if this other solution is not working, like the underneeth code example leveraging on file manipulation
package com.mycompany.testautomation.util
import com.kms.katalon.core.annotation.Keyword
import java.io.File
import java.io.FileInputStream
import java.io.FileOutputStream
import java.util.Properties
public class PropertiesFileManager {
@Keyword
def writeValue(String key, int value) {
Properties properties = new Properties()
String filePath = "path/to/your/file.properties" // Adjust this path as necessary
File file = new File(filePath)
if (file.exists()) {
properties.load(new FileInputStream(file))
}
properties.setProperty(key, value.toString())
properties.store(new FileOutputStream(file), null)
}
@Keyword
def int readValue(String key) {
Properties properties = new Properties()
String filePath = "path/to/your/file.properties" // Adjust this path as necessary
File file = new File(filePath)
if (!file.exists()) {
return 0 // Default value if file or key does not exist
}
properties.load(new FileInputStream(file))
return properties.getProperty(key, "0").toInteger()
}
}
then import it and use the writevalue and readvalue functions within your testcase
import com.mycompany.testautomation.util.PropertiesFileManager
int finalFailedCount = PropertiesFileManager.readValue('failedCount')
good luck
Thanks for your helping but I wonder how to call the variable finalFailedCount in the Email body on Project Settings → Email → Template? I tried to call but the system seems to be not found the variable. Thanks
I have a solution for it now via a custom function. However this is very tricky and use it at your own risk
@Keyword
def updateGlobalVariableInGlbl(String variableName, String newValue) {
// Specify the path to your default.glbl file
String currentDir = new File(".").getAbsolutePath().substring(0, new File(".").getAbsolutePath().length() - 1)
String glblFilePath = currentDir + "/Profiles/default.glbl"
File glblFile = new File(glblFilePath)
if (!glblFile.exists()) {
println("GLBL file not found.")
return
}
// Parse the GLBL file
def glblXml = new XmlParser().parse(glblFile)
// Find the GlobalVariableEntity with the given name
def variableNode = glblXml.'**'.find { node ->
node.name() == 'GlobalVariableEntity' && node.name.text() == variableName
}
if (variableNode) {
// Ensure the new value is wrapped in single quotes
String formattedNewValue = "'${newValue}'"
// Update the initValue of the variable
variableNode.initValue[0].value = formattedNewValue
// Save the updated XML back to the GLBL file
XmlUtil.serialize(glblXml, new FileOutputStream(glblFile))
println("Global Variable '$variableName' updated to '$formattedNewValue'.")
} else {
println("Variable '$variableName' not found.")
}
}
use this function at your afterTestsuite but please at your own risk, It updates the xml of your profile global property