How to Use Methods in SoapUI for Bulk Test Execution – SoapUI Tutorial #10

By Vijay

By Vijay

I'm Vijay, and I've been working on this blog for the past 20+ years! I’ve been in the IT industry for more than 20 years now. I completed my graduation in B.E. Computer Science from a reputed Pune university and then started my career in…

Learn about our editorial policies.
Updated March 6, 2024

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.

How to Use Methods in SoapUI For Bulk Test Execution

Use Methods in SoapUI

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
Methods in SoapUI 1

Double click on the groovy script test step and Copy and paste the following script.

def countryName = &amp;quot;India&amp;quot;
def cityName = &amp;quot;Hyderabad&amp;quot;

void CreateAndPassProperties(String countryName, String cityName)
{
&nbsp;&nbsp; &nbsp;// Add Global Properties
&nbsp;&nbsp; &nbsp;com.eviware.soapui.SoapUI.globalProperties.addProperty( &amp;quot;CountryName&amp;quot;)
&nbsp;&nbsp; &nbsp;com.eviware.soapui.SoapUI.globalProperties.addProperty( &amp;quot;CityName&amp;quot;)

&nbsp;&nbsp; &nbsp;// Assign values to the global properties
&nbsp;&nbsp; &nbsp;com.eviware.soapui.SoapUI.globalProperties.
setPropertyValue( &amp;quot;CountryName&amp;quot;, countryName )
&nbsp;&nbsp; &nbsp;com.eviware.soapui.SoapUI.globalProperties.
setPropertyValue( &amp;quot;CityName&amp;quot;, cityName )
}

// Method Invoking
CreateAndPassProperties(countryName, cityName)
log.info(&amp;quot;Testcase execution is completed successfully.&amp;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.

Methods in SoapUI 2

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

Methods in SoapUI 3

GetWeatherReport Webservice

Methods in SoapUI 4

Now we can execute the test suite and verify the results with the following screenshot.

(Click on the image for an enlarged view)

Methods in SoapUI 5

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.

Methods in SoapUI 6
  • Double-click on the GetCitiesByCountry service and make the changes to the request as shown in the following screenshot.
Methods in SoapUI 7
  • 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] = &amp;quot;India&amp;quot;
countries[1] = &amp;quot;US&amp;quot;
countries[2] = &amp;quot;Mauritius&amp;quot;
countries[3] = &amp;quot;Cyprus&amp;quot;
countries[4] = &amp;quot;Austria&amp;quot;

// Invoke Method
GetCountries(countries);

// Method Definition
void GetCountries(Object[] countries)
{
&nbsp;&nbsp; &nbsp;for(int i=0; i&amp;lt;5; i++)
&nbsp;&nbsp; &nbsp;{
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;// Assign values to the global properties and call the servive
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;com.eviware.soapui.SoapUI.globalProperties.setPropertyValue
(&amp;quot;CountryName&amp;quot;, countries[i] )

&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;// Call GetCitiesByCountry service to run
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;def testStep = testRunner.testCase.testSteps['GetCitiesByCountry'];
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;testStep.run(testRunner,context);
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;log.info('Method is called ' + i + 'time(s)');
&nbsp;&nbsp; &nbsp;}
}

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.

Methods in SoapUI 8

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.

Methods in SoapUI 9

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)
{
&nbsp;&nbsp; &nbsp;CName = 'Mauritius';
&nbsp;&nbsp; &nbsp;return CName;
}

// Invoke method
String&nbsp; getMethodValue = ReturnCountryName('US');

// Assign value to the global property
com.eviware.soapui.SoapUI.globalProperties.setPropertyValue
( &amp;quot;CountryName&amp;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:

Methods in SoapUI 10

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. 

Was this helpful?

Thanks for your feedback!

Recommended Reading

4 thoughts on “How to Use Methods in SoapUI for Bulk Test Execution – SoapUI Tutorial #10”

  1. @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.

    Reply

Leave a Comment