In this tutorial, we will learn how to use methods in SoapUI for bulk test execution. Let’s get started.
In the last SoapUI tutorial, we learned about conditional statements in Groovy scripts. This tutorial is all about object-oriented programming in SoapUI Groovy scripts, a very interesting and important programming concept. It includes classes and objects, inheritance, encapsulation and polymorphism.
This is tutorial #10 in our comprehensive SoapUI tutorial series.
All the major programming languages such as C++, Java, C#, Visual Basic 6.0 etc., support object-oriented concepts. All the parts of computer devices have object-oriented concepts implemented internally.
Table of Contents:
How to Use Methods in SoapUI For Bulk Test Execution
What is object-oriented programming
Object-oriented programming is a programming language specification that deals with “objects”. Using this concept, complex desktop/web/mobile and business applications are often developed. A brief introduction to the components of object-oriented programming concepts is given below:
- Classes and Objects
- Consists of methods and properties. Methods are a set of instructions that perform a specific action. This will help us to reuse the code that is already defined. Properties are the variables to store the values temporarily.
- Inheritance
- This is a very useful component of OOP. It helps us create a new class from an existing class so the properties and methods can be reused or accessed in the derived class directly. Technically, the main class is called Base/super class and the inherited class is known as derived class.
- Encapsulation
- Using this concept, we can hide the class information or protect the properties of the class by accessing it from outside.
- Polymorphism
- Poly refers “many” and morphism means “forms” that means an object can be defined in many forms.
What are objects?
An object is a container that consists of properties and methods that perform certain action(s).
Objects are considered as real-world materials like a smart phone, tablet, monitor etc. it has characteristics and activities.
Let’s take human beings as an example. As people, we have different characters and behavior among us like face, skin colour, living style etc. we are doing regular activities such breathing, speaking, walking and so forth. So an object can include characteristics and actions implicitly.
In the object-oriented programming paradigm, an object is an instance of a class which consists of properties and methods also it is represented as the blue print of the class. A class is generally defined as the following format.
class <class name>
{
[access modifiers:]
<properties declaration>
<methods>
}
As discussed in our previous tutorials, groovy scripts support java code directly without having to import built-in libraries explicitly. Groovy script editor does not allow defining user-defined classes directly but defining methods in groovy is straightforward. Once done, these methods can be invoked anywhere in the script. The methods in Groovy scripts can be defined as below:
<return type> <method name> [ parameters ]
{
<statements>
[return]
}
Example
In the following example, we are going to define a method with some input parameters. Later the parameter values will be assigned to the global properties. We will then pass on the property value to the service request as an input.
Here are the steps:
- Create a project with http://www.webservicex.net/globalweather.asmx?WSDL URL
- Then add the test suite and test cases as shown in the below screenshot
Double click on the groovy script test step and Copy and paste the following script.
def countryName = &quot;India&quot; def cityName = &quot;Hyderabad&quot; void CreateAndPassProperties(String countryName, String cityName) { // Add Global Properties com.eviware.soapui.SoapUI.globalProperties.addProperty( &quot;CountryName&quot;) com.eviware.soapui.SoapUI.globalProperties.addProperty( &quot;CityName&quot;) // Assign values to the global properties com.eviware.soapui.SoapUI.globalProperties. setPropertyValue( &quot;CountryName&quot;, countryName ) com.eviware.soapui.SoapUI.globalProperties. setPropertyValue( &quot;CityName&quot;, cityName ) } // Method Invoking CreateAndPassProperties(countryName, cityName) log.info(&quot;Testcase execution is completed successfully.&quot;)
In the above script, we are defining local variables and assigning string data to each variable. Following that, we define a method called CreateAndPassProperties with two parameters – countryName and cityName.
Within this method, the first two lines are used to define global properties. These can be seen by using the “File->Preferences->Global Properties” as below. Initially, before the test gets executed, this will be empty.
Once done, go to the request test steps like GetCitiesByCountry and GetWeatherReport and make the necessary changes to the input request as shown in the below screenshots.
GetCitiesByCountry Web Service
GetWeatherReport Webservice
Now we can execute the test suite and verify the results with the following screenshot.
(Click on the image for an enlarged view)
This way we can define a number of methods for various purposes in a groovy script test step which can help us execute bulk test steps at once. Methods also allow us to pass arrays to handle bulk data.
Methods and Arrays
We will now assign array values to a global property and then pass it on to the service. During test suite execution, the script will pass input data to the service and the service will then process the data. Finally, it will send the respective response in SoapUI.
Let us create a test suite and test steps as shown in the screenshot below.
- Double-click on the GetCitiesByCountry service and make the changes to the request as shown in the following screenshot.
- Double-click on the Method_Return_Value test step and write the following script:
def MAX_LIMIT = 5 def countries = new Object[MAX_LIMIT] countries[0] = &quot;India&quot; countries[1] = &quot;US&quot; countries[2] = &quot;Mauritius&quot; countries[3] = &quot;Cyprus&quot; countries[4] = &quot;Austria&quot; // Invoke Method GetCountries(countries); // Method Definition void GetCountries(Object[] countries) { for(int i=0; i&lt;5; i++) { // Assign values to the global properties and call the servive com.eviware.soapui.SoapUI.globalProperties.setPropertyValue (&quot;CountryName&quot;, countries[i] ) // Call GetCitiesByCountry service to run def testStep = testRunner.testCase.testSteps['GetCitiesByCountry']; testStep.run(testRunner,context); log.info('Method is called ' + i + 'time(s)'); } }
In the above script, we have defined an array called countries which holds 5 country names.
Following that, we are invoking GetCountries method by passing countries array. Now the execution flow moves to the method definition where we iterate the loop five times. Each time, the array element that is the country name will be assigned to the global property (i.e. CountryName). Then, GetCitiesByCountry test step gets called.
Since we already mentioned the global property name in the service request, the actual country name will be taken as input and processed accordingly.
Once this test suite is executed successfully, verify the response to determine the service execution status. Let us have a look at the following screenshot.
Now that you know the purpose of the methods there is another feature that we need to discuss this topic and that is Methods can be return values to the caller through output values.
To do so, we employ the “return” keyword. Let us take GetCitiesByCountry service for this example. Add one more groovy script test step under WeatherTestSuite as shown in the screenshot.
Double click on the service name and make the changes if needed. Then write the following script in the groovy script editor.
// Method definition String ReturnCountryName(String CName) { CName = 'Mauritius'; return CName; } // Invoke method String getMethodValue = ReturnCountryName('US'); // Assign value to the global property com.eviware.soapui.SoapUI.globalProperties.setPropertyValue ( &quot;CountryName&quot;, getMethodValue ) // Call GetCitiesByCountry service to run def testStep = testRunner.testCase.testSteps['GetCitiesByCountry']; testStep.run(testRunner,context); // Message log.info('Testsuite executed successfully');
The above is a slight modification of the previous script. In the method definition, we are passing a country name as string type and then we assign the new country name to the method variable. So the “US” string will be changed to “Mauritius” after the execution.
This will be sent to the getMethodValue variable. The remaining code is the same as we discussed earlier. It will assign property value to the global property and then service test step will be called by testStep.run(…) method.
To execute the test suite, double-click on the test suite name and then click the Run icon. The result would be as below:
Conclusion
Here is a quick recap of all the concepts we have discussed so far.
- Object-oriented programming concept plays the main role in all the major programming languages.
- Objects are the main components that contain properties and methods.
- Objects are the blueprint of a class.
- Without defining class, it is not possible to instantiate an object.
- Methods are one of the parts in the class. This is used to avoid repeated code.
A few samples have been provided in this article to execute test cases. Implement these samples to your real-time web services and test suites to make yourself more comfortable and to become an expert in script writing.
Next tutorial #11: In the next SoapUI tutorial, we will learn about “Exception handling in SoapUI Groovy scripts”.
Keep reading. Please don’t forget to post your experiences, suggestions and questions in the comments section below. We would love to hear your thoughts.
do you have any example on how to group tests?
@Sumitra: Test suite itself is a way for us to group tests in SoapUI. For example, if you need a different set of tests, you just have to create a new test suite and create tests as required under it as test cases.
Can I get the variable value defined in the groovie script in my Auth dialog box.
how can we convert raw code into apis