Unable to pass variable of One TC to Another TC

I have tried to pass the variable of one TC to another TC by stating the previous variable as GlobalVariable but unable to process. Getting the below error message:

10-21-2019 09:24:01 AM Test Cases/SimpleTest/Sample11

Elapsed time: 0.677s

Test Cases/SimpleTest/Sample11 FAILED.
Reason:
groovy.lang.MissingPropertyException: No such property: x for class: internal.GlobalVariable
at Sample11.run(Sample11:23)
at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)
at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)
at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:337)
at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:328)
at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:307)
at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:299)
at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:233)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:114)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:105)
at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)
at TempTestCase1571639037221.run(TempTestCase1571639037221.groovy:21)

hi,

could you show script how this call testcase is done

Hello,

I have done by a simple script as in the below format:

Sample Script:
image

Another Script:

If I have done any wrong please suggest

Sample Test : Remove x=20
Sample11 : Use println(GlobalVariable.x)

I tried the same as shown below, but still getting the below error:

Getting the below error:
10-22-2019 10:38:33 AM Test Cases/SimpleTest/Sample11

Elapsed time: 0.815s

Test Cases/SimpleTest/Sample11 FAILED.
Reason:
groovy.lang.MissingPropertyException: No such property: x for class: internal.GlobalVariable
at Sample11.run(Sample11:18)
at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)
at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)
at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:337)
at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:328)
at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:307)
at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:299)
at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:233)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:114)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:105)
at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)
at TempTestCase1571729909090.run(TempTestCase1571729909090.groovy:21)

You have to run ‘Sample Test’ and ‘Sample11’ in a TestSuite

Thanks.Its working fine.

But still I have a question, Now I have passed the value as fixed value in the keyword, how can I do this for Random value generated during the run time in one TC and pass it in the next TC.

I am capturing the value in a Variable for the value generated in first TC’s.

Same but replace “x” by the name of the variable you want, and “20” by your random Value

Hi,
I need a help. I am getting the value into the variable as shown below and I am trying to pass the same variable to the specified Keyword. Do you have any way to pass the variable value to the keyword. So that I can use the variable name in the other case and pass the value


could you please help me in this regard

I don’t understand what you want to do.
Once you use [...]addGlobalVariable('New_Contract', result) you can call GlobalVariable.New_Contract
What is on the screenshot should work.

I tried as below but again, I am getting error as below:(TC 1)

Test Case 2:

Error Message:
10-22-2019 12:38:42 PM Test Cases/SimpleTest/Sample11

Elapsed time: 0.345s

Test Cases/SimpleTest/Sample11 FAILED.
Reason:
groovy.lang.MissingPropertyException: No such property: New_Contract for class: internal.GlobalVariable
at Sample11.run(Sample11:17)
at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)
at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)
at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:337)
at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:328)
at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:307)
at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:299)
at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:233)
at com.kms.katalon.core.main.TestSuiteExecutor.accessTestCaseMainPhase(TestSuiteExecutor.java:169)
at com.kms.katalon.core.main.TestSuiteExecutor.accessTestSuiteMainPhase(TestSuiteExecutor.java:142)
at com.kms.katalon.core.main.TestSuiteExecutor.execute(TestSuiteExecutor.java:91)
at com.kms.katalon.core.main.TestCaseMain.startTestSuite(TestCaseMain.java:157)
at com.kms.katalon.core.main.TestCaseMain$startTestSuite$0.call(Unknown Source)
at TempTestSuite1571736827798.run(TempTestSuite1571736827798.groovy:35)

The 2 Test Cases are in one Test Suite and you run the Test Suite ?

Yes Both the TCs are in the same suite(i.e., SimpleTest of the above screenshot).

My Hierarchy of TCs is in the below format:
TC1: It has a major case with the following sub cases:

TC2: I am calling the variable of Create Contract Page 3 in the New case separately which is in another folder:

Accessing the Variable in Create Contract Page3 to Sample11. By creating a single suite for the access of variable of the above.

Looks like your code doesn’t reach the else statement from “Create Contract Page 3”.

Hi,

It is passing through the else Statement. Please find the screenshot for reference.

But it is failing here in the TC - 2:

What if you create an empty global variable named New_Contract into the profile ?
Rather than the custom keyword addGlobalVariable, use GlobalVariable.New_Contract = result

Thank you…Helene:) I will work now as you have suggested.

But if possible can you please let me know how can we go and use by having it in the form of Keyword.

I’m sorry but I don’t know.
I never used the keyword @Timo_Kuisma gave you in your other thread, and it doesn’t seem to work in your case.

Hello,

TESTCASE1
import static com.kms.katalon.core.checkpoint.CheckpointFactory.findCheckpoint
import static com.kms.katalon.core.testcase.TestCaseFactory.findTestCase
import static com.kms.katalon.core.testdata.TestDataFactory.findTestData
import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import com.kms.katalon.core.checkpoint.Checkpoint as Checkpoint
import com.kms.katalon.core.cucumber.keyword.CucumberBuiltinKeywords as CucumberKW
import com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords as Mobile
import com.kms.katalon.core.model.FailureHandling as FailureHandling
import com.kms.katalon.core.testcase.TestCase as TestCase
import com.kms.katalon.core.testdata.TestData as TestData
import com.kms.katalon.core.testobject.TestObject as TestObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
import internal.GlobalVariable as GlobalVariable

import org.openqa.selenium.By as By
import org.openqa.selenium.WebDriver as WebDriver
import org.testng.Assert as Assert
import com.kms.katalon.core.webui.driver.DriverFactory as DriverFactory

import org.openqa.selenium.WebDriver
import org.openqa.selenium.chrome.ChromeDriver
import org.openqa.selenium.chrome.ChromeOptions
import com.kms.katalon.core.webui.driver.DriverFactory

import helper.HelperUtils

CustomKeywords.'helper.HelperUtils.addGlobalVariable'('testURL', 'katalon.com')
println GlobalVariable.testURL

TESTCASE2
import static com.kms.katalon.core.checkpoint.CheckpointFactory.findCheckpoint
import static com.kms.katalon.core.testcase.TestCaseFactory.findTestCase
import static com.kms.katalon.core.testdata.TestDataFactory.findTestData
import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import com.kms.katalon.core.checkpoint.Checkpoint as Checkpoint
import com.kms.katalon.core.cucumber.keyword.CucumberBuiltinKeywords as CucumberKW
import com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords as Mobile
import com.kms.katalon.core.model.FailureHandling as FailureHandling
import com.kms.katalon.core.testcase.TestCase as TestCase
import com.kms.katalon.core.testdata.TestData as TestData
import com.kms.katalon.core.testobject.TestObject as TestObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
import internal.GlobalVariable as GlobalVariable

import helper.HelperUtils

def testUrl = GlobalVariable.testURL
println("TestUrl given in testcase 1 "+testUrl)
//give new value
CustomKeywords.'helper.HelperUtils.addGlobalVariable'('testURL', 'kuisma.com')
println ("variable value changed "+GlobalVariable.testURL)


KEYWORD
package helper

import static com.kms.katalon.core.checkpoint.CheckpointFactory.findCheckpoint
import static com.kms.katalon.core.testcase.TestCaseFactory.findTestCase
import static com.kms.katalon.core.testdata.TestDataFactory.findTestData
import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import static com.kms.katalon.core.testobject.ObjectRepository.findWindowsObject

import com.kms.katalon.core.annotation.Keyword
import com.kms.katalon.core.checkpoint.Checkpoint
import com.kms.katalon.core.cucumber.keyword.CucumberBuiltinKeywords as CucumberKW
import com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords as Mobile
import com.kms.katalon.core.model.FailureHandling
import com.kms.katalon.core.testcase.TestCase
import com.kms.katalon.core.testdata.TestData
import com.kms.katalon.core.testobject.TestObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
import com.kms.katalon.core.windows.keyword.WindowsBuiltinKeywords as Windows

import internal.GlobalVariable

public class HelperUtils {

	@Keyword
	void addGlobalVariable(String name, def value) {
		GroovyShell shell1 = new GroovyShell()
		MetaClass mc = shell1.evaluate("internal.GlobalVariable").metaClass
		String getterName = "get" + name.capitalize()
		mc.'static'."$getterName" = { -> return value }
		mc.'static'."$name" = value
	}
}

EXECUTED IN TESTCASE LEVEL
2019-10-22 17:35:23.457 INFO  k.k.c.m.CustomKeywordDelegatingMetaClass - helper.HelperUtils.addGlobalVariable is PASSED
katalon.com
TestUrl given in testcase 1 katalon.com
2019-10-22 17:35:41.839 INFO  k.k.c.m.CustomKeywordDelegatingMetaClass - helper.HelperUtils.addGlobalVariable is PASSED
variable value changed book.com

EXECUTED IN TESTSUITECOLLECTION LEVEL
2019-10-22 17:41:09.634 INFO  k.k.c.m.CustomKeywordDelegatingMetaClass - helper.HelperUtils.addGlobalVariable is PASSED
katalon.com
TestUrl given in testcase 1 katalon.com
2019-10-22 17:41:38.858 INFO  k.k.c.m.CustomKeywordDelegatingMetaClass - helper.HelperUtils.addGlobalVariable is PASSED
variable value changed book.com