In this tutorial, we will explore the fundamentals of writing Groovy scripts in SoapUI, specifically focusing on basic scripting operations and the various operators that can be utilized in Groovy scripts in SoapUI.
To commence our exploration of groovy scripting in SoapUI Pro, let us dive into the sixth tutorial in our comprehensive SoapUI online training 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 )
SoapUI Groovy Script Test Step:
The Groovy Script test step is an essential feature available in SoapUI / Pro for custom automation test script creation. This feature can be utilized for conducting functional, load, and regression tests.
Groovy, being a scripting language, integrates all Java libraries internally. As a result, all Java-related keywords and functions can be directly utilized in a Groovy script. The Java libraries come pre-installed with SoapUI and are seamlessly incorporated during the SoapUI Pro installation process.
Here’s how you can add a Groovy script to a test in SoapUI:
Step #1. Start by creating a SOAP project in SoapUI Pro using a valid WSDL document. Inside the project, create a test suite with the desired name. Within the test suite, add a Groovy script test step as demonstrated below:
Step #2. Provide a name for the step in the dialogue box that appears, and click OK to proceed.
Step #3. An editor will be displayed, allowing you to write your script. Alternatively, you can double-click on the Groovy step name within your test case (identified by the star prefix).
(Click on the image to enlarge)
For instance, let’s write a simple script that displays a message in the log. Here’s the one-line script:
log.info ”soapUI script”
Step #4. To execute the aforementioned script in SoapUI Pro, click the Run icon and observe the results in the Log Output section.
Additional Points:
- Test Script Execution: When the “Run” button within the Groovy editor is clicked, only the code within the Groovy step will be executed. Conversely, when the “Run” button for the entire test case is clicked, all the steps are executed in a sequential manner.
- With Groovy scripting, you can easily add necessary validations to the test scripts.
- Any number of Groovy test steps can be added to a test case.
- Groovy scripts do not require separate compilation and interpretation to execute code similar to other programming languages like C, C++, Java, etc.
- Within a test suite, steps can be enabled or disabled using the comment feature. To do so, utilize the following syntax:
// – Indicates a single-line comment.
/* <some script> */ – Denotes a multi-line comment.
Arithmetic Operations:
Within the Groovy step editor, you can perform the following:
/* Adding Two Numbers */
int a;
int b;
int c;
// Assigning Integer Values to Variables A and B
a = 100;
b = 200;
// Adding Values of A and B, and Assigning the Result to Variable C
c = a + b
// Displaying the Resultant Value in the Log
log.info(“Result: ” + c);
In the above script, A, B, and C are variables used for storing or transferring values.
(Click on the image to enlarge)
Note: Variables in Groovy scripts are case-sensitive. Exercise caution when using them.
The following are the operators supported in Groovy:
Arithmetic Operators:
+ Addition operator / String concatenation
– Subtraction operator
* Multiplication operator
/ Division operator
% Remainder operator
// Examples of Arithmetic Operators:
// Addition Operator:
int x1 = 100 + 200
log.info(“Addition Result: ” + x1);
// Concatenation of Two Strings using PLUS ( + ) operator
String city =”Timothy E.” + ” Shepherd”;
log.info(“String 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);
// Modulus Operator:
int x5 = 10 % 3
log.info(“Reminder or Modulus: ” + x5);
The following screenshot depicts the above scripts and their respective results:
Unary Operators:
Unary operators solely work with a single operand. For instance, ++ is known as the Increment operator, which increments the current value by 1.
Here’s an example:
int A = 100;
A++; // Equivalent to A = A + 1
log.info(A);
The above script will output 101. This increment operation is referred to as post-increment. Similarly, this operator can be utilized as a pre-increment operation as shown below:
int A = 100;
log.info(++A);
There is also the decrement operator (–), which decreases the current value by 1. We can incorporate this operator into the examples discussed above.
int A = 100;
A–; // Equivalent to A = A – 1
log.info(A);
The above script will output:
Mon Jul 21 18:02:16 IST 2014:INFO:99
The pre and post operations can also be used with the decrement operator.
Assignment Operators:
The basic assignment operator is the equal sign (=). However, there are other useful assignment operators available, such as +=, -=, *=, /=, and %=.
Let’s take a look at some samples.
int A = 100;
A += 10; // Equivalent to A = A + 10
log.info(A);
The above script will output 110. If we use the minus-equal-to operator in the script below, the value will be 40.
int B = 50;
B -= 10;
log.info(B);
We can utilize the remaining operators in a similar manner.
int C = 10;
C *= 10;
log.info(C);
And,
int D = 50;
D /= 10;
log.info(D);
Furthermore, the remainder operator can be used as follows:
int E = 10;
E %= 3;
log.info(E);
This will divide the value 10 by 3 and assign the remainder to the variable “E”.
Conclusion:
This is just the beginning, as Groovy supports many other operators, such as logical, comparison, and conditional operators, among others. We will explore these operators in the upcoming tutorials. In our next SoapUI tutorial, we will also delve into working with properties in SoapUI Groovy scripts.
Join us in this exciting journey and don’t hesitate to leave your comments and questions below.