I just want to add a new update for this solution with the following improvement:
This improves for any string email so you don’t need to type it and concatenate (the code split the string by the @, so must be that character)
Script
String number = GlobalVariable.RealtorEmail.split('@')[0].replaceAll("[a-z, +]", "")
String emailP1 = GlobalVariable.RealtorEmail.split('@')[0].replaceAll("[0-9]", "")
String emailP2 = GlobalVariable.RealtorEmail.split('@')[1]
if (number != ""){
number = Integer.parseInt(number)+1
}
else{
number = 1
}
String nextEmail= emailP1 + number.toString() + "@" + emailP2
'Assign new value in real time (not updates the profile)'
//GlobalVariable.RealtorEmail=nextEmail
'Assign new value in real time (updates the profile with new value)'
CustomKeywords.'lemonbrew.GlobalVariableUpdater.updatePermanently'("Agent", "RealtorEmail" , nextEmail)
Import the custom keyword “GlobalVariableUpdater.updatePermanently” above from this link How to update GlobalVariable permanently
Here I adapt my script logic above creating this new GlobalVariableUpdater.increaseEmailVariablecustom keyboard below using the custom keyboards created by @Marek_Melocik
Keyword
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.annotation.Keyword
import com.kms.katalon.core.configuration.RunConfiguration
import com.kms.katalon.core.util.KeywordUtil
import internal.GlobalVariable
public class GlobalVariableUpdater {
/** 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 varEmailName
*/
@Keyword
public static void increaseEmailVariable(String envName, String varEmailName) {
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() == varEmailName) {
String email = elem.getElementsByTagName("initValue").item(0).getTextContent();
String number = email.split('@')[0].replaceAll("[a-z, +,']", "");
String emailP1 = email.split('@')[0].replaceAll("[0-9]", "");
String emailP2 = email.split('@')[1];
if (number != ""){
number = Integer.parseInt(number)+1;
}
else{
number = 1;
}
String nextEmail= emailP1 + "$number@" + emailP2
elem.getElementsByTagName("initValue").item(0).setTextContent(nextEmail)
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 " + varEmailName + " was not found.")
}
}
NOTE: If you use this any of this keywords in a test Case you probably have an error after every test because the project doesn’t refresh all the files like when a Test Suite ends, so take that in mind. Maybe you can try to refresh it manually going to Project > Refresh (Crtl+F5)