Is there are any possibility to initialize a string variable with test case id?
for example, I want to do smth like this
In TestCase: def currentTestCaseId = this.id
and then I want to create a method and send there my current testCaseId
–
I tried to use Test Listener, but it could write ID in log only. I cant initialize and return ID as a value (return in a global value is not an option, because test cases could be run in test suite in parallel mode)
GlobalVariable is allocated per each TestSuite run. If you run 5 TestSuites in a TestSuiteCollection in parallel mode, each TestSuite will have its own set of GlobalVariables.
See https://github.com/kazurayam/KatalonDiscussion9725. There I tried to prove my thought. Clone it out and execute the TestSuiteCollection TSC. It will run successful. You will not see any IllegalStateException raised.
GlobalVariable is allocated per each TestSuite run. If you run 5 TestSuites in a TestSuiteCollection in parallel mode, each TestSuite will have its own set of GlobalVariables.
See https://github.com/kazurayam/KatalonDiscussion9725. There I tried to prove my thought. Clone it out and execute the TestSuiteCollection TSC. It will run successful. You will not see any IllegalStateException raised.
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
WebUI.comment("${this.getTestCaseId()}")
When I ran it, I got the following error message:
09-19-2018 03:04:30 PM - [ERROR] - Test Cases/GetTestCaseId FAILED because (of) (Stack trace: groovy.lang.MissingMethodException: No signature of method: Script1537337010963.getTestCaseId() is applicable for argument types: () values: []
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:58)
at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.callCurrent(PogoMetaClassSite.java:81)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:52)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:154)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:158)
at Script1537337010963.run(Script1537337010963.groovy:16)
at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:183)
at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:108)
at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:294)
at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:285)
at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:264)
at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:256)
at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:200)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:99)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:90)
at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:149)
at TempTestCase1537337065278.run(TempTestCase1537337065278.groovy:22)
at groovy.lang.GroovyShell.runScriptOrMainOrTestOrRunnable(GroovyShell.java:263)
at groovy.lang.GroovyShell.run(GroovyShell.java:518)
at groovy.lang.GroovyShell.run(GroovyShell.java:507)
at groovy.ui.GroovyMain.processOnce(GroovyMain.java:653)
at groovy.ui.GroovyMain.run(GroovyMain.java:384)
at groovy.ui.GroovyMain.process(GroovyMain.java:370)
at groovy.ui.GroovyMain.processArgs(GroovyMain.java:129)
at groovy.ui.GroovyMain.main(GroovyMain.java:109)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.codehaus.groovy.tools.GroovyStarter.rootLoader(GroovyStarter.java:109)
at org.codehaus.groovy.tools.GroovyStarter.main(GroovyStarter.java:131)
)
What is the appropriate way to call getTestCaseId() ?
But why can’t I use TestCaseContext.getTestCaseId() outside of a test listener is still a mystery to me.
Basically, listeners are “hooks” which are called at a specific time during test case “startup”. After that time, the context in which they existed no longer exists.
Make sense?
Under the current regime, (as I’m sure you know, Mate) you need to catch the Id at that hook point and store it in a global you can use later/elsewhere.
Proposal: Perhaps the (java) Runtime object could be extended to add a test case context object reference?
Basically, listeners are “hooks” which are called at a specific time during test case “startup”. After that time, the context in which they existed no longer exists.
Make sense?
Yes, perfect. Thanks!
Under the current regime, (as I’m sure you know, Mate) you need to catch the Id at that hook point and store it in a global you can use later/elsewhere.
Thanks, Kazurayam, it works fine
I just thought, that ID parameter is an attribute of test case and it had initialized when I created it. So i thought, there is a way one could get it without running a test case.
But anyway global variable is an option