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

Object-Oriented Groovy Scripting in SoapUI:

In last SoapUI tutorial we learned conditional statements in Groovy scrips. Now 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 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.

So let’s get started.

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 concept is below:

  • Classes and Objects
    • Consists of methods and properties. Methods are set of instructions that perform specific action. It helps us to reuse the code that is already defined. Properties are the variables to store the values temporarily.
  • Inheritance
    • This is very useful component in 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 as Base / super class and the inherited class is known as derived class.
  • Encapsulation
    • Using this concept, we can hide the class information or protect properties of the class by accessing 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 smart phone, tablet, monitor etc. it has characteristics and activities.

Let’s take human being as an example for object. As a person, we have different characters and behavior among us like face, skin color, 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 blue print of the class. A class is generally defined as following format.

class <class name>
{
                [access modifiers:]
                <properties declaration>

                <methods>
}

As we discussed in our previous tutorials, groovy script supports 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. Then, we will pass the property values to the service request as an input.

Here are steps:

  • Create a project with http://www.webservicex.net/globalweather.asmx?WSDL URL
  • And then add 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 = "India"
def cityName = "Hyderabad"

void CreateAndPassProperties(String countryName, String cityName)
{
    // Add Global Properties
    com.eviware.soapui.SoapUI.globalProperties.addProperty( "CountryName")
    com.eviware.soapui.SoapUI.globalProperties.addProperty( "CityName")

    // Assign values to the global properties
    com.eviware.soapui.SoapUI.globalProperties.
setPropertyValue( "CountryName", countryName )
    com.eviware.soapui.SoapUI.globalProperties.
setPropertyValue( "CityName", cityName )
}

// Method Invoking
CreateAndPassProperties(countryName, cityName)
log.info("Testcase execution is completed successfully.")

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 the method, the first two lines are used to define the 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 necessary changes in the input request as shown in the below screen shots.

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 image for enlarged view)

Methods in SoapUI 5

This way we can define multiple methods for various purposes in 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 to assign array values to a global property and then pass it 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 this screenshot.

Methods in SoapUI 6

  • Double click on the GetCitiesByCountry service and make the change in 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] = "India"
countries[1] = "US"
countries[2] = "Mauritius"
countries[3] = "Cyprus"
countries[4] = "Austria"

// Invoke Method
GetCountries(countries);

// Method Definition
void GetCountries(Object[] countries)
{
    for(int i=0; i<5; i++)
    {
        // Assign values to the global properties and call the servive
        com.eviware.soapui.SoapUI.globalProperties.setPropertyValue
("CountryName", 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 array element that is country name will be assigned to the global property (i.e. CountryName). Then, GetCitiesByCountry test step gets called.

Since we already mentioned global property name in the service request, 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 you know the purpose of the methods there is another feature that we need to discuss in 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)
{    
    CName = 'Mauritius';
    return CName;
}

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

// Assign value to the global property
com.eviware.soapui.SoapUI.globalProperties.setPropertyValue
( "CountryName", 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 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. Remaining code is 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 Run icon. The result would be as below:

Methods in SoapUI 10

Conclusion

Here is a quick recap of all the concepts we discussed so far:

  • Object oriented programming concept plays a 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. These are 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 you more comfortable and to become an expert in the script writing.

Next tutorial #11: In next SoapUI tutorial we will learn “Exception handling in SoapUI Groovy scripts”.

Keep reading. See you in the next tutorial. Please post comments, experiences, suggestions and questions below.

Recommended reading

2 comments ↓

#1 Sumitra

do you have any example on how to group tests?

#2 Swati Seela

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

Leave a Comment