How to iterate a Global Variable on every run?

Hi guys, I wonder to know-how increase the value inside my global variable “Email”. Now I’m running my test suit to create the entire user login, with the same Gmail account to receive the confirmation emails.
For example:
Rodrigo+realtor+1@gmail.com
Rodrigo+realtor+2@gmail.com
.
.
.
Rodrigo+realtor+n@gmail.com

But my problem is that I have to change this ‘n’ manually, Is any way to increase this value inside the variable from the @SetupTestCase or @SetUp?
(but only increase the value on the profile I choose to execute)

PD: I’m new with automation so I’m open to hearing new ways to automate this task. I thought to use this variable as a TestData object but that is when you have to create a bunch of users on the same Run, right?

Guys @Russ_Thomas @Brandon_Hein, I didn’t find on the forum any post related to this. Could you give a hand here please?

I just tried the following in the SUIT script mode:

@SetUp(skipped = false)
String email = GlobalVariable.RealtorEmail
String newEmail= “Rodrigo+realtor+” + (Integer.parseInt(GlobalVariable.RealtorEmail.substring(17,email.length()))+1) + “@gmail.com”;
GlobalVariable.RealtorEmail= newEmail

@rodrigocalabretta

It seems like what you want, does it work ? What’s the test script and the consoel output ?

Hi @ThanhTo, not at all. Not sure if this is the way to do it.

Also when I store the global variable into the string variable “email” I think I’m storing only the name of that variable and not his string value

This is my log after run that thing :persevere::

If you want to automate this how would you do it

@rodrigocalabretta

Please post the console log on the Console Tab, it has more information regarding why it fails.

@ThanhTo

The value of RealtorEmail variable is “lemonbrewqa+realtor49@gmail.com

@rodrigocalabretta

Please create a copy of your project, download the latest version and execute on the copied project. I think in the latest version we’ve logged more elaborated error logs.

Also, does any of the input data is 49@gmail.com ? You use substring(19, email.length()) so there might be an offset problem.

@ThanhTo I could do it! Check my code:

def setUp() {
String number = GlobalVariable.RealtorEmail.split(‘realtor’)[1].substring(0,2).trim()
Integer numberInt = Integer.parseInt(number)+1
String newEmail1= “lemonbrewqa+realtor” + numberInt.toString() + “@gmail.com”;
GlobalVariable.RealtorEmail= newEmail1
}

I’m increasing that number from 49 to 50, but my problem is the last line where I want to store those 50, so in the next Race increase to 51 is not working. Is possible to reassign a new value, right?

I tried and it is possible, here’s my set up.

A profile contains a Number variable called globalNum

Test case:

def globalVar = GlobalVariable.globalNum

println globalVar;

Test Suite contains 6 instances of Test case. In the Script tab of the Test suite, I have this:

/**
 * Run before each test case starts.
 */
@SetupTestCase(skipped = false) // Please change skipped to be false to activate this method.
def setupTestCase() {
	GlobalVariable.globalNum = GlobalVariable.globalNum + 1;
}

The console log prints

2019-11-27 11:37:59.489 INFO  c.k.katalon.core.main.TestSuiteExecutor  - START Test Suites/New Test Suite
2019-11-27 11:37:59.549 INFO  c.k.katalon.core.main.TestSuiteExecutor  - hostName = thanhto - 192.168.37.118
2019-11-27 11:37:59.550 INFO  c.k.katalon.core.main.TestSuiteExecutor  - os = Mac OS X 64bit
2019-11-27 11:37:59.553 INFO  c.k.katalon.core.main.TestSuiteExecutor  - hostAddress = 192.168.37.118
2019-11-27 11:37:59.555 INFO  c.k.katalon.core.main.TestSuiteExecutor  - katalonVersion = 7.1.0.1
2019-11-27 11:37:59.878 INFO  c.k.katalon.core.main.TestCaseExecutor   - --------------------
2019-11-27 11:37:59.880 INFO  c.k.katalon.core.main.TestCaseExecutor   - START Test Cases/New Test Case
2019-11-27 11:37:59.922 INFO  com.kms.katalon.core.util.KeywordUtil    - [SAUCELABS] Current run configuration: Chrome
2019-11-27 11:37:59.927 INFO  com.kms.katalon.core.util.KeywordUtil    - [SAUCELABS] Saucelabs Plugin will not attempt to auto update job status and information !
2019-11-27 11:38:00.588 DEBUG testcase.New Test Case                   - 1: globalVar = globalNum
2019-11-27 11:38:00.590 DEBUG testcase.New Test Case                   - 2: println(globalVar)
1
2019-11-27 11:38:00.602 INFO  com.kms.katalon.core.util.KeywordUtil    - [SAUCELABS] Current run configuration: Chrome
2019-11-27 11:38:00.603 INFO  c.k.katalon.core.main.TestCaseExecutor   - END Test Cases/New Test Case
2019-11-27 11:38:00.608 INFO  c.k.katalon.core.main.TestCaseExecutor   - =================== Smart XPath Report =======================
2019-11-27 11:38:00.608 INFO  c.k.katalon.core.main.TestCaseExecutor   - Refer to the link below to preview and approve auto healing on broken test objects.
2019-11-27 11:38:00.609 INFO  c.k.katalon.core.main.TestCaseExecutor   - https://docs.katalon.com/katalon-studio/docs/auto-healing-smart-xpath.html
2019-11-27 11:38:00.610 INFO  c.k.katalon.core.main.TestCaseExecutor   - ===============================================================
2019-11-27 11:38:00.615 INFO  c.k.katalon.core.main.TestCaseExecutor   - --------------------
2019-11-27 11:38:00.617 INFO  c.k.katalon.core.main.TestCaseExecutor   - START Test Cases/New Test Case
2019-11-27 11:38:00.625 INFO  com.kms.katalon.core.util.KeywordUtil    - [SAUCELABS] Current run configuration: Chrome
2019-11-27 11:38:00.626 INFO  com.kms.katalon.core.util.KeywordUtil    - [SAUCELABS] Saucelabs Plugin will not attempt to auto update job status and information !
2019-11-27 11:38:00.632 DEBUG testcase.New Test Case                   - 1: globalVar = globalNum
2019-11-27 11:38:00.633 DEBUG testcase.New Test Case                   - 2: println(globalVar)
2
2019-11-27 11:38:00.635 INFO  com.kms.katalon.core.util.KeywordUtil    - [SAUCELABS] Current run configuration: Chrome
2019-11-27 11:38:00.635 INFO  c.k.katalon.core.main.TestCaseExecutor   - END Test Cases/New Test Case
2019-11-27 11:38:00.644 INFO  c.k.katalon.core.main.TestCaseExecutor   - =================== Smart XPath Report =======================
2019-11-27 11:38:00.645 INFO  c.k.katalon.core.main.TestCaseExecutor   - Refer to the link below to preview and approve auto healing on broken test objects.
2019-11-27 11:38:00.646 INFO  c.k.katalon.core.main.TestCaseExecutor   - https://docs.katalon.com/katalon-studio/docs/auto-healing-smart-xpath.html
2019-11-27 11:38:00.646 INFO  c.k.katalon.core.main.TestCaseExecutor   - ===============================================================
2019-11-27 11:38:00.650 INFO  c.k.katalon.core.main.TestCaseExecutor   - --------------------
2019-11-27 11:38:00.650 INFO  c.k.katalon.core.main.TestCaseExecutor   - START Test Cases/New Test Case
2019-11-27 11:38:00.660 INFO  com.kms.katalon.core.util.KeywordUtil    - [SAUCELABS] Current run configuration: Chrome
2019-11-27 11:38:00.661 INFO  com.kms.katalon.core.util.KeywordUtil    - [SAUCELABS] Saucelabs Plugin will not attempt to auto update job status and information !
2019-11-27 11:38:00.668 DEBUG testcase.New Test Case                   - 1: globalVar = globalNum
2019-11-27 11:38:00.669 DEBUG testcase.New Test Case                   - 2: println(globalVar)
3
2019-11-27 11:38:00.674 INFO  com.kms.katalon.core.util.KeywordUtil    - [SAUCELABS] Current run configuration: Chrome
2019-11-27 11:38:00.675 INFO  c.k.katalon.core.main.TestCaseExecutor   - END Test Cases/New Test Case
2019-11-27 11:38:00.676 INFO  c.k.katalon.core.main.TestCaseExecutor   - =================== Smart XPath Report =======================
2019-11-27 11:38:00.676 INFO  c.k.katalon.core.main.TestCaseExecutor   - Refer to the link below to preview and approve auto healing on broken test objects.
2019-11-27 11:38:00.676 INFO  c.k.katalon.core.main.TestCaseExecutor   - https://docs.katalon.com/katalon-studio/docs/auto-healing-smart-xpath.html
2019-11-27 11:38:00.676 INFO  c.k.katalon.core.main.TestCaseExecutor   - ===============================================================
2019-11-27 11:38:00.680 INFO  c.k.katalon.core.main.TestCaseExecutor   - --------------------
2019-11-27 11:38:00.680 INFO  c.k.katalon.core.main.TestCaseExecutor   - START Test Cases/New Test Case
2019-11-27 11:38:00.689 INFO  com.kms.katalon.core.util.KeywordUtil    - [SAUCELABS] Current run configuration: Chrome
2019-11-27 11:38:00.695 INFO  com.kms.katalon.core.util.KeywordUtil    - [SAUCELABS] Saucelabs Plugin will not attempt to auto update job status and information !
2019-11-27 11:38:00.700 DEBUG testcase.New Test Case                   - 1: globalVar = globalNum
2019-11-27 11:38:00.700 DEBUG testcase.New Test Case                   - 2: println(globalVar)
4
2019-11-27 11:38:00.710 INFO  com.kms.katalon.core.util.KeywordUtil    - [SAUCELABS] Current run configuration: Chrome
2019-11-27 11:38:00.711 INFO  c.k.katalon.core.main.TestCaseExecutor   - END Test Cases/New Test Case
2019-11-27 11:38:00.712 INFO  c.k.katalon.core.main.TestCaseExecutor   - =================== Smart XPath Report =======================
2019-11-27 11:38:00.712 INFO  c.k.katalon.core.main.TestCaseExecutor   - Refer to the link below to preview and approve auto healing on broken test objects.
2019-11-27 11:38:00.713 INFO  c.k.katalon.core.main.TestCaseExecutor   - https://docs.katalon.com/katalon-studio/docs/auto-healing-smart-xpath.html
2019-11-27 11:38:00.713 INFO  c.k.katalon.core.main.TestCaseExecutor   - ===============================================================
2019-11-27 11:38:00.716 INFO  c.k.katalon.core.main.TestCaseExecutor   - --------------------
2019-11-27 11:38:00.717 INFO  c.k.katalon.core.main.TestCaseExecutor   - START Test Cases/New Test Case
2019-11-27 11:38:00.725 INFO  com.kms.katalon.core.util.KeywordUtil    - [SAUCELABS] Current run configuration: Chrome
2019-11-27 11:38:00.726 INFO  com.kms.katalon.core.util.KeywordUtil    - [SAUCELABS] Saucelabs Plugin will not attempt to auto update job status and information !
2019-11-27 11:38:00.732 DEBUG testcase.New Test Case                   - 1: globalVar = globalNum
2019-11-27 11:38:00.733 DEBUG testcase.New Test Case                   - 2: println(globalVar)
5
2019-11-27 11:38:00.735 INFO  com.kms.katalon.core.util.KeywordUtil    - [SAUCELABS] Current run configuration: Chrome
2019-11-27 11:38:00.737 INFO  c.k.katalon.core.main.TestCaseExecutor   - END Test Cases/New Test Case
2019-11-27 11:38:00.737 INFO  c.k.katalon.core.main.TestCaseExecutor   - =================== Smart XPath Report =======================
2019-11-27 11:38:00.737 INFO  c.k.katalon.core.main.TestCaseExecutor   - Refer to the link below to preview and approve auto healing on broken test objects.
2019-11-27 11:38:00.738 INFO  c.k.katalon.core.main.TestCaseExecutor   - https://docs.katalon.com/katalon-studio/docs/auto-healing-smart-xpath.html
2019-11-27 11:38:00.738 INFO  c.k.katalon.core.main.TestCaseExecutor   - ===============================================================
2019-11-27 11:38:00.744 INFO  c.k.katalon.core.main.TestCaseExecutor   - --------------------
2019-11-27 11:38:00.746 INFO  c.k.katalon.core.main.TestCaseExecutor   - START Test Cases/New Test Case
2019-11-27 11:38:00.762 INFO  com.kms.katalon.core.util.KeywordUtil    - [SAUCELABS] Current run configuration: Chrome
2019-11-27 11:38:00.762 INFO  com.kms.katalon.core.util.KeywordUtil    - [SAUCELABS] Saucelabs Plugin will not attempt to auto update job status and information !
2019-11-27 11:38:00.766 DEBUG testcase.New Test Case                   - 1: globalVar = globalNum
2019-11-27 11:38:00.766 DEBUG testcase.New Test Case                   - 2: println(globalVar)
6
2019-11-27 11:38:00.775 INFO  com.kms.katalon.core.util.KeywordUtil    - [SAUCELABS] Current run configuration: Chrome
2019-11-27 11:38:00.775 INFO  c.k.katalon.core.main.TestCaseExecutor   - END Test Cases/New Test Case
2019-11-27 11:38:00.776 INFO  c.k.katalon.core.main.TestCaseExecutor   - =================== Smart XPath Report =======================
2019-11-27 11:38:00.777 INFO  c.k.katalon.core.main.TestCaseExecutor   - Refer to the link below to preview and approve auto healing on broken test objects.
2019-11-27 11:38:00.777 INFO  c.k.katalon.core.main.TestCaseExecutor   - https://docs.katalon.com/katalon-studio/docs/auto-healing-smart-xpath.html
2019-11-27 11:38:00.778 INFO  c.k.katalon.core.main.TestCaseExecutor   - ===============================================================
2019-11-27 11:38:00.781 INFO  c.k.katalon.core.main.TestCaseExecutor   - --------------------
2019-11-27 11:38:00.781 INFO  c.k.katalon.core.main.TestCaseExecutor   - START Test Cases/New Test Case
2019-11-27 11:38:00.789 INFO  com.kms.katalon.core.util.KeywordUtil    - [SAUCELABS] Current run configuration: Chrome
2019-11-27 11:38:00.790 INFO  com.kms.katalon.core.util.KeywordUtil    - [SAUCELABS] Saucelabs Plugin will not attempt to auto update job status and information !
2019-11-27 11:38:00.793 DEBUG testcase.New Test Case                   - 1: globalVar = globalNum
2019-11-27 11:38:00.794 DEBUG testcase.New Test Case                   - 2: println(globalVar)
7
2019-11-27 11:38:00.796 INFO  com.kms.katalon.core.util.KeywordUtil    - [SAUCELABS] Current run configuration: Chrome
2019-11-27 11:38:00.797 INFO  c.k.katalon.core.main.TestCaseExecutor   - END Test Cases/New Test Case
2019-11-27 11:38:00.797 INFO  c.k.katalon.core.main.TestCaseExecutor   - =================== Smart XPath Report =======================
2019-11-27 11:38:00.798 INFO  c.k.katalon.core.main.TestCaseExecutor   - Refer to the link below to preview and approve auto healing on broken test objects.
2019-11-27 11:38:00.798 INFO  c.k.katalon.core.main.TestCaseExecutor   - https://docs.katalon.com/katalon-studio/docs/auto-healing-smart-xpath.html
2019-11-27 11:38:00.798 INFO  c.k.katalon.core.main.TestCaseExecutor   - ===============================================================
2019-11-27 11:38:01.074 INFO  com.kms.katalon.core.util.KeywordUtil    - Start generating HTML report folder at: /Users/thanhto/Katalon Studio/Test projects/Katalon Studio Features/Reports/20191127_113756/New Test Suite/20191127_113756...
2019-11-27 11:38:01.105 INFO  com.kms.katalon.core.util.KeywordUtil    - HTML report generated
2019-11-27 11:38:01.122 INFO  com.kms.katalon.core.util.KeywordUtil    - Start generating CSV report folder at: /Users/thanhto/Katalon Studio/Test projects/Katalon Studio Features/Reports/20191127_113756/New Test Suite/20191127_113756...
2019-11-27 11:38:01.155 INFO  com.kms.katalon.core.util.KeywordUtil    - CSV report generated
2019-11-27 11:38:01.375 INFO  c.k.katalon.core.main.TestSuiteExecutor  - --------------------
2019-11-27 11:38:01.376 INFO  c.k.katalon.core.main.TestSuiteExecutor  - END Test Suites/New Test Suite
2019-11-27 11:38:01.376 INFO  c.k.katalon.core.main.TestSuiteExecutor  - ====================

Please try the simple scenario of incrementing a number in global variable, then gradually switch to concatenating it with a string.

1 Like

Yeap I follow you, but that Number variable I set on 0 in my Execution profile is still on 0 after runs end. Your globalVar after increasing 6 times, in your profile execution that variable was reassigned to 6?

@rodrigocalabretta

Okay I think I finally catch what you mean. Katalon has no way to mutate the global variable permanently, only in the context of an execution does the change effect. Please refer to this:

and see if it makes more sense.

1 Like

Wow thanks for that approach… It’s more complex than I thought, but let me know if I understand correctly. I can set in the TearDown function, my code to increase the new value of the variable (which is now working) and use this keyword to update my current execution profile with the new value increased, right?

1 Like

Yes, that is correct. Please try and see if it works for you.

@ThanhTo thanks mate you made my day!

It’s working and increasing perfectly well

My Code
@TearDown(skipped = false) // Please change skipped to be false to activate this method.
def tearDown() {
	//Put your code here.
	String number = GlobalVariable.RealtorEmail.split('realtor')[1].substring(0,2).trim()
	Integer numberInt = Integer.parseInt(number)+1
	String newEmail1= "lemonbrewqa+realtor" + numberInt.toString() + "@gmail.com";
	
	'Assign new value in real time (not updates the profile)'
	//GlobalVariable.RealtorEmail=newEmail1
	
	'Assign new value in real time (updates the profile with new value)'
	CustomKeywords.'lemonbrew.GlobalVariableUpdater.updatePermanently'("Agent", "RealtorEmail" , newEmail1)
}

A detail commented by @Marek_Melocik is that ** Katalon does not look for updates in the execution profile ** until the executions are finished. For me it is not a problem because I configured this code in TearDown and it works, the only thing that was freaking me out was that the profile wasn’t updating automatically in the IDE (but is updating in the folder file). To see that the value increased, you have to close the profile tab and open it again after Katalon finishes loading and sending the reports.

Thanks again for your help!!

1 Like

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)

2 Likes