Object-Oriented Groovy Scripting in SoapUI:
In our last SoapUI tutorial, we focused on conditional statements in Groovy scripts. In this tutorial, we shall explore the object-oriented paradigm in SoapUI Groovy scripts, an exciting and crucial programming topic that includes classes and objects, inheritance, encapsulation, and polymorphism.
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 )
This is the tenth chapter in our comprehensive series of SoapUI tutorials.
Crucial programming languages such as C++, Java, C#, Visual Basic 6.0 and so on are based on object-oriented principles. These concepts are used internally in various computer device components.
Let’s begin.
What does object-oriented programming entail?
Object-oriented programming is a branch of coding that emphasizes “objects.” Using this approach, complicated desktop, web, mobile, and business programs are frequently developed. Here’s a quick overview of the components of object-oriented programming:
- Classes and Objects
- These comprise of methods and attributes. Methods are instruction sets that perform particular functions. They allow us to use previously defined code. Properties are variables used to store values temporarily.
- Inheritance
- In object-oriented programming, this is a valuable component. It lets us make a new class from an existing one, allowing us to reuse or directly access the properties and methods of the newly created class. Formally, the original class is known as the Base/super class, and the class that inherits from it is referred to as the derived class.
- Encapsulation
- With this concept, we can conceal class data or prevent external access to class properties.
- Polymorphism
- Poly translates to “many” while morphism denotes “forms,” meaning that an object can be represented in various forms.
What constitutes objects?
An object is a structure that contains properties and methods that execute particular operation(s).
Objects can be seen as tangible-world entities like smartphones, tablets, and screens. They have qualities and functions.
Consider a person as an example of an object. As people, we have multiple attributes and actions, including our faces, skin tone, and daily habits. We also perform routine tasks like breathing, talking, and walking. In this way, an object can innately include characteristics and activities.
In the framework of object-oriented programming, an object is considered an instance of a class comprising properties and methods. It can also be viewed as a blueprint of the class. Typically, a class is defined in the following style:
class <class name>
{
[access modifiers:]
<attributes declaration>
<methods>
}
As we explored in previous tutorials, Groovy scripts permit direct Java code manipulation without requiring specific library imports. Although Groovy script editor doesn’t allow direct definition of user-defined classes, it’s straightforward to define methods in Groovy. Defined methods can be summoned anywhere in the script. Methods can be specified in Groovy scripts as shown below:
<return type> <method name> [ parameters ]
{
<instructions>
[return]
}
Illustration:
In the upcoming example, we specify a method with a few input parameters. These parameter values will then be assigned to global properties. We shall subsequently pass the property values as an input to the service request.
The procedure involves the following steps:
- Set up a project with the URL http://www.webservicex.net/globalweather.asmx?WSDL
- Add test suite and test cases as shown in the screenshot below:
Double-click on the Groovy script test step and copy-paste the posted script.
def countryName = "India" def cityName = "Hyderabad" void CreateAndPassProperties(String countryName, String cityName) { // Implement Global Properties com.eviware.soapui.SoapUI.globalProperties.addProperty( "CountryName") com.eviware.soapui.SoapUI.globalProperties.addProperty( "CityName") // Designate values to the global properties com.eviware.soapui.SoapUI.globalProperties. setPropertyValue( "CountryName", countryName ) com.eviware.soapui.SoapUI.globalProperties. setPropertyValue( "CityName", cityName ) } // Invoke Method CreateAndPassProperties(countryName, cityName) log.info("Testcase executed successfully.")
In the script above, we assign string values to each local variable. Subsequently, we define a method named CreateAndPassProperties with two parameters, countryName and cityName.
Within the method, the initial two lines are employed to state the global properties. Before the test execution commences, these properties can be viewed via “File->Preferences->Global Properties” as shown below. At first, these properties will be vacant.
Within the service request, go to the request test procedures which are GetCitiesByCountry and GetWeatherReport test steps, and make the necessary alterations as shown in the screenshots.
GetCitiesByCountry Web Service:
GetWeatherReport Web Service:
Orchestrate the test suite, validate the results and refer to the screenshot included.
(For an enlarged view, click on the image)
In this way, you can define multiple methods in groovy script test steps for executing bulk test operations all at once. Additionally, methods permit passing arrays for handling large data sets.
Methods and Arrays:
Let’s now assign array values to a global property and move on to pass it to the service. During the test suite execution, the script will pass the input data to the service, which will subsequently process the data and send the relative response in SoapUI.
Form a test suite and test steps according to the following screenshot.
- Double-click on the GetCitiesByCountry service and incorporate the required modifications in the request, as portrayed in the following screenshot.
- Double-click on the Method_Return_Value test step and insert 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 service 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 mentioned script, we define an array called countries that stores names of 5 countries.
Subsequently, we call the GetCountries method by passing in the countries array. The execution flow then proceeds to the method definition where we loop through the method five times. Each pass assigns a country name, which is an array element, to the global property (i.e., CountryName), after which the GetCitiesByCountry test step is initiated.
As we have already indicated the global property name in the service request, the actual country name functions as the input and is processed as required.
After successfully executing this test suite, verify the response to check the service execution status. Please refer to the following screenshot:
Final Thoughts
Let's quickly run through what we've discussed:
- The concept of object-oriented programming is vital across all major programming languages.
- Objects are the primary components that consist of attributes and methods.
- Objects can be seen as class blueprints.
- Class definition is necessary to create an object.
- Methods comprise an essential part of classes and they are utilised to prevent code redundancy.
This article covered several possible use-case scenarios for executing test cases. Implement these examples in your real-time web services and test suites to advance your competence and proficiency in script writing.
Coming up next guide #11: In the next tutorial in the SoapUI series, we will be discussing "Handle exceptions in SoapUI Groovy scripts."
Continue reading. We'll see you in the next guide. Don't hesitate to comment, share your experiences, give suggestions, or ask queries below.