In this guide, we will delve into the essentials of constructing Groovy scripts in SoapUI, with a particular emphasis on elementary scripting actions and the array of operators that can be employed in Groovy scripts in SoapUI.
As part of our comprehensive exploration of Groovy scripting in SoapUI Pro, let’s dive into the sixth guide in our in-depth SoapUI e-learning series.
Recommended IPTV Service Providers
- IPTVGREAT – Rating 4.8/5 ( 600+ Reviews )
- IPTVRESALE – Rating 5/5 ( 200+ Reviews )
- IPTVGANG – Rating 4.7/5 ( 1200+ Reviews )
- IPTVUNLOCK – Rating 5/5 ( 65 Reviews )
- IPTVFOLLOW -Rating 5/5 ( 48 Reviews )
- IPTVTOPS – Rating 5/5 ( 43 Reviews )
Test Step for SoapUI Groovy Script:
The Groovy Script test step is a critical function offered in SoapUI / Pro for creating personalized automation test scripts. This capability is beneficial for executing functional, load, and regression assessments.
Given that Groovy is a scripting language, it incorporates all Java libraries internally. Consequently, all Java-linked phrases and functions can be employed directly into a Groovy script. The Java libraries are pre-loaded with SoapUI and are smoothly integrated during the SoapUI Pro setup process.
Here’s the process to include a Groovy script in a SoapUI test:
Step #1. Initiate by establishing a SOAP project in SoapUI Pro employing a legitimate WSDL document. Within the project, construct a test suite with the chosen name. Inside the test suite, incorporate a Groovy script test step as shown below:
Step #2. Give the step a name in the dialog box that emerges, and click OK to advance.
Step #3. An editor will now appear, enabling you to compose your script. Alternatively, you can double-click on the Groovy step name within your test case (denoted by the star prefix).
(Click on the image for an expanded view)
For example, let’s construct a straightforward script that exhibits a log message. Here’s the single-line script:
log.info ”soapUI script”
Step #4. To accomplish the previously referred script in SoapUI Pro, hit the Run button and watch the outcomes in the Log Output section.
Supplementary Points:
- Test Script Realization: Upon clicking the “Run” button within the Groovy editor, solely the code within the Groovy step is accomplished. On the contrary, when clicking the “Run” button for the entire test case, all the steps are realized in a consecutive order.
- With the aid of Groovy scripting, you can effortlessly add necessary validations to the test scripts.
- An unrestricted number of Groovy test steps can be included in a test case.
- Groovy scripts don’t necessitate separate compilation and interpretation to implement code akin to other programming languages like C, C++, Java, etc.
- Steps can be enabled or disabled within a test suite using the comment option. Here’s how:
// – Represents a single-line comment.
/* <some script> */ – Signifies a multi-line comment.
Arithmetical Operations:
You can achieve the following within the Groovy step editor:
/* Summing Two Numbers */
int a;
int b;
int c;
// Allocating Integer Values to Variables A and B
a = 100;
b = 200;
// Adding Values of A and B, and Assigning the Outcome to Variable C
c = a + b
// Exhibiting the Resultant Value in the Log
log.info(“Result: ” + c);
In the prior script, variables A, B, and C are utilized for storing or transferring values.
(Click on the image to enlarge)
Note: Take precaution when utilizing Groovy scripts as these are case-sensitive.
The operators listed below are compatible with Groovy:
Mathematical Operators:
+ Sum/Narrative concatenation
– Subtraction operator
* Multiplication operator
/ Division of numbers
% Numerical remainder operator
// Accessible Arithmetic Operators Examples:
// Addition Operator:
int x1 = 100 + 200
log.info(“Sum Result: ” + x1);
// The Concatenation of Two Strings Using the PLUS ( + ) operator
String city =”Timothy E.” + ” Shepherd”;
log.info(“Narrative Concatenation: ” + city);
// Subtraction Operator:
int x2 = 200 – 100
log.info(“Subtraction: ” + x2);
// Multiplication Operator:
int x3 = 10 * 200
log.info(“Multiplication: ” + x3);
// Division Operator:
int x4 = 200 / 10
log.info(“Division: ” + x4);
// Remainder Operator:
int x5 = 10 % 3
log.info(“Reminder or Modulus: ” + x5);
The following image provides an account of the previous scripts and their corresponding results:
Unary Operators:
Unary operators exclusively operate with a singular operand. For instance, ++ is recognized as the Increment operator, which escalates the present value by 1.
A case in point:
int A = 100;
A++; // Equals A = A + 1
log.info(A);
The preceding script will display 101. This increment action is called post-increment. Likewise, using this operator as a pre-increment operation can be seen below:
int A = 100;
log.info(++A);
The decrement operator (–) is also present, which depresses the present value by 1. It can be integrated into the cases we talked about previously.
int A = 100;
A–; // Analogous to A = A – 1
log.info(A);
The output of the aforementioned script will be:
Mon Jul 21 18:02:16 IST 2014:INFO:99
The decrement operator may also be used with pre and post operations.
Substitution Operators:
The fundamental substitution operator is the equals sign (=). Nevertheless, there are other valuable replacement operators accessible, such as +=, -=, *=, /=, and %=.
Let’s explore some instances.
int A = 100;
A += 10; // Equivalent to A = A + 10
log.info(A);
The above script will yield 110. If we use the minus-equal-to operator in the following script, the value would be 40.
int B = 50;
B -= 10;
log.info(B);
The remaining operators can be used in a similar pattern.
int C = 10;
C *= 10;
log.info(C);
And,
int D = 50;
D /= 10;
log.info(D);
Additionally, the remainder operator can be utilized as follows:
int E = 10;
E %= 3;
log.info(E);
This action will divide the value 10 by 3 and allocate the remainder to the variable “E”.
Wrap-Up:
We’re just getting started, as Groovy supports a multitude of other operators, like logical, comparison, and conditional operators, amongst others. We will delve into these operators in the future guides. In our next SoapUI guide, we will also delve deeper into working with properties in SoapUI Groovy scripts.
We welcome you on this exhilarating journey and encourage you to share your queries and remarks below.