Calling a Groovy from a Groovy in SoapUI
Recently, I started to work with the tool SoapUI-NG (Ready-API) for a project related concern in the current workplace for a period of time. Since didn't have much hands on exposure with the tool I decided to go through the existing Project Test Suites and Test Cases to get a idea of how everything was structured and how the groovy scripting was involved while referencing many guidelines from the Internet.
Yes! Groovy Scripting.
Having a challenge to beat is the motivator right? I haven't ever deal with Groovy at all previously. So, I had to learn groovy while learning the SoapUI too.
While getting the dirt on hands by playing with the groovy and the Soap-UI got an unexpected thought murmuring in a corner of my mind to see how to call a groovy script from another groovy script in Soap-UI, as you cant call a class or a method like we usually call in the Dev-IDEs. Digging into the question made me realize that the Stack Overflow even had the same question written by someone, But without any proper answer from anyone.
Challenge ‘n’ Motivator
It became a challenge made to me by my self since the solution isn't there in Stack as well. Decided to take an existing test-case as the lab-rat and the current project for the test-bed for the R‘n’D approach.
What I noticed is, even though it is a code-script of us, for the SoapUI it is just another Test-step, which is dedicated to write Groovy. Therefore it is not feasible to call the groovy step just like calling a class from another class like we learn’t in OOP by creating an object of it or like a nested method in the developer IDE’s. Somehow, managed to solve the concern after messing with it for few hours with various approaches and efforts.
Yes, Ultimately a solution respond with a Green Tag (Pass)…!!
and the solution is pretty simple than I expected.
Solution / Code Snippet (For Your Reference):
Step 1 : Two Groovy Scripts
(can be inside the same test case or different test case. It doesn't matter as long as the two scripts are under one project umbrella)
As for my example;
Parent Script : Groovy Script
Child Script : Groovy Script 2
[Therefore, Parent Script will call the Child Script]
Step 2 : Code Snippet for the ‘Groovy Script’
Make sure to customize the following parameters based on your solution.
ProjectName, TestSuiteName, TestCaseName and Calling Groovy Script. According to my example here it is ‘Groovy Script 2’
Ps: runner.getStatus() is used just for verification purpose,
therefore that code-line can be ignored in your solution.
Step 3 : Code Snippet for the ‘Groovy Script 2'.
To verify the call is successfully capturing the response of the child (second script) from the parent (first script) here I have printed on log temporarily.
Step 4 : Response when you run the ‘Groovy Script’ (Parent Script)
Always remember there can be various routes and approaches for the same destination, and so goes for this solution.
If there is a better approach as for your knowledge, comment section is here to discuss the pros and cons of this and that.