Resons for using Groovy script in SOAPUI tool:
The
For example:
we have a property, Endpoint, defined at getRoomDetails SOAP Request TestStep. We can simply read the value of this property using a context object, as shown in the following script:
Note that you cannot read the property value from a TestStep of a different TestCase using this method.
The following will return null as deleteRoom TestStep is in a different TestCase:
EndPointProp2 = context.getProperty("deleteRoom","Endpoint")
log.info(EndPointProp2)
OUTPUT: Wed Feb 13 11:24:35 IST 2019:INFO:null
Now, let's do another test to find out what properties are available during a single run of a TestStep. Add the following script in the editor of Groovy Script TestStep and run the test step:
String[] props= context.getPropertyNames()
for (prop in props) {
log.info(prop)
}
Here, we read all property names of the current context into a string array and iterate over the values.
There are three built-in properties associated with the context of request submission—ThreadIndex, log, and RunCount:
OUTPUT: Wed Feb 13 11:30:13 IST 2019:INFO:ThreadIndex
Wed Feb 13 11:30:13 IST 2019:INFO:log
Wed Feb 13 11:30:13 IST 2019:INFO:RunCount
Double-click on the getRoomDetails TestCase to open the getRoomDetails TestCase editor. Now, you will have the getRoomDetails SOAP Test request and GroovyTestScript1 TestSteps under the TestCase. Run the getRoomDetails TestCase by selecting the green arrow icon at the top of the getRoomDetails TestCase and look at the output. This time you will see 19 properties such as httpMethod, requestUri, and postMethod, which are available at the TestCase context of the run session.
In addition to the built-in properties, we can set the properties and retrieve them later during a particular test run.
PropertyVal=new String("This is a property value")
//Setting a value to property1
context.setProperty("property1", PropertyVal)
//Reading property1's value
readPropval = context.getProperty("property1")
log.info readPropval
OUTPUT: Wed Feb 13 13:17:52 IST 2019:INFO:This is a property value
The context.expand (<String>) method is a useful method, which is inherited from the com.eviware.soapui.model.support.AbstractAdminContext base class.
This can be used in multiple situations and the simplest usage is for accessing a custom property at a different level of a test. If we have a custom property at project level (for example, Test), then we can use the expand method to read the property value from a script which runs from the TestStep level:
- To dynamically generate Mock Responses when simulating web services
- To add arbitrary functionality to TestCases using Groovy Script TestStep
- To use as Setup/TearDown scripts to initialize, and cleanup TestSuites and TestCases
- To use as Start/Stop scripts in initializing/cleaning up mock services
- To dynamically generate TestRequests and assertions based on database contents
- The OnRequest and AfterRequest scripts in Mock Services
- To perform arbitrary functionality during property expansion
The Groovy scripts inside soapUI have access to the following context-related variables:
1. context
2. testRunner
soapUI also provides us with a standard log4j Logger object—log—that can be used in scripts at any level in a soapUI project.
1. The Context Object
The context object holds information about a particular test run session. It can be used to read and write/update context-specific variables. There are different contexts available in a soapUI project.
For Example:
1. LoadTestRunContext: This holds context information about the load test run session.
2. MockRunContext: This context is available for the duration of a Mock Services' execution.
3. SubmitContext: This is available during one submit of a request.
4. TestRunContext: This is available during a TestCase execution and all scripts in a TestCase run have access to the TestRunContext.
The usage of the context object using a simple example:
1. Open the HotelReservation project in soapUI and add a Groovy Script TestStep into getRoomDetails TestCase. Name the test step GroovyTestScript1.
2. Add the following script in script editor and run the test step by clicking on the green arrow icon which is in the upper-left corner of the Groovy Script editor:
// Get the name of current TestStep
log.info(context.getCurrentStep().getLabel())
// Get the name of current parent TestCase
log.info(context.getTestCase().getLabel())
// Get the name of TestSuite
log.info(context.getTestCase().getTestSuite().getLabel())
// Get the name of SOAPUI project
log.info(context.getTestCase().getTestSuite().getProject().getName())
You will find the output of test run at the Log Output window which appears right below the script editor. It will look similar to the following.
Tue Feb 12 19:03:15 IST 2019:INFO:GroovytestScript1
Tue Feb 12 19:03:15 IST 2019:INFO:getRoomDetails
Tue Feb 12 19:03:15 IST 2019:INFO:RoomManagementServiceTestSuite
Tue Feb 12 19:03:15 IST 2019:INFO:HotelReservationproject

context object is useful in situations where you want to read the property values of TestStep. For example:
we have a property, Endpoint, defined at getRoomDetails SOAP Request TestStep. We can simply read the value of this property using a context object, as shown in the following script:
EndPointProp =context.getProperty("getRoomDetails","Endpoint")
The following will return null as deleteRoom TestStep is in a different TestCase:
EndPointProp2 = context.getProperty("deleteRoom","Endpoint")
log.info(EndPointProp2)
OUTPUT: Wed Feb 13 11:24:35 IST 2019:INFO:null
Now, let's do another test to find out what properties are available during a single run of a TestStep. Add the following script in the editor of Groovy Script TestStep and run the test step:
String[] props= context.getPropertyNames()
for (prop in props) {
log.info(prop)
}
Here, we read all property names of the current context into a string array and iterate over the values.
There are three built-in properties associated with the context of request submission—ThreadIndex, log, and RunCount:
OUTPUT: Wed Feb 13 11:30:13 IST 2019:INFO:ThreadIndex
Wed Feb 13 11:30:13 IST 2019:INFO:log
Wed Feb 13 11:30:13 IST 2019:INFO:RunCount
Double-click on the getRoomDetails TestCase to open the getRoomDetails TestCase editor. Now, you will have the getRoomDetails SOAP Test request and GroovyTestScript1 TestSteps under the TestCase. Run the getRoomDetails TestCase by selecting the green arrow icon at the top of the getRoomDetails TestCase and look at the output. This time you will see 19 properties such as httpMethod, requestUri, and postMethod, which are available at the TestCase context of the run session.
In addition to the built-in properties, we can set the properties and retrieve them later during a particular test run.
PropertyVal=new String("This is a property value")
//Setting a value to property1
context.setProperty("property1", PropertyVal)
//Reading property1's value
readPropval = context.getProperty("property1")
log.info readPropval
OUTPUT: Wed Feb 13 13:17:52 IST 2019:INFO:This is a property value
The context.expand (<String>) method is a useful method, which is inherited from the com.eviware.soapui.model.support.AbstractAdminContext base class.
This can be used in multiple situations and the simplest usage is for accessing a custom property at a different level of a test. If we have a custom property at project level (for example, Test), then we can use the expand method to read the property value from a script which runs from the TestStep level:
log.info(context.expand( '${#Project#Test}'))
The
1. Add another GrovyScript TestStep in the getRoomDetails TestCase. Let's name it GroovyTestScript2:
2. Add the following into GroovyTestScript1 to define a new property,
context.holder="testing"
3. Now, add the following in GroovyTestScript2 to read the property value:
holderValue = context.getProperty("holder")
log.info(holderValue)
OUTPUT: Wed Feb 13 19:02:15 IST 2019:INFO:testing
Once you run the getRoomDetails TestCase, you will see the log output of GroovyTestScript2 run which prints testing as the result.
In this example, context represented an instance of TestCaseRunContext where the context is visible inside TestCase.
The
context object is very useful if we want to store some value in one TestStep and use it in subsequent script test steps:1. Add another GrovyScript TestStep in the getRoomDetails TestCase. Let's name it GroovyTestScript2:
2. Add the following into GroovyTestScript1 to define a new property,
holder in context:context.holder="testing"
3. Now, add the following in GroovyTestScript2 to read the property value:
holderValue = context.getProperty("holder")
log.info(holderValue)
OUTPUT: Wed Feb 13 19:02:15 IST 2019:INFO:testing
Once you run the getRoomDetails TestCase, you will see the log output of GroovyTestScript2 run which prints testing as the result.
In this example, context represented an instance of TestCaseRunContext where the context is visible inside TestCase.
No comments:
Post a Comment