Groovy is an object-oriented programming language for the Java platform alternative to the Java language. Can be seen as the scripting language for the Java Platform, has characteristics similar to those of Python, Ruby, Perl, and Smalltalk.

Features:
Groovy uses a syntax similar to that of Java, based on braces, dynamically compiles bytecode for the Java Virtual Machine, and interacts seamlessly with other Java code and existing libraries. The Groovy compiler can be used to generate standard Java bytecode which can be used by any Java project. Groovy can also be used as a dynamic scripting language.

  • Dynamic Typing
  • Native syntax for lists, maps and regular expressions
  • Closures
  • Overloading operators

Another Groovy feature that is worth emphasizing is its native support for various markup languages like XML and HTML. This feature allows to define and manipulate many types of disparate data with a syntax and a methodology of uniform programming.

Standard Java (Java 5+):

for (String item : new String [] {"Pinscher", "Testing", "Blog"}) {
     if (item.length() <= 4) System.out.println(item);
}

Groovy:

["Pinscher", "Testing", "Blog"].findAll{it.size() <= 4}.each{println it}

 
Groovy Site
 

To test better services SoapUI we can use Groovy.
Between a request and the next we can insert a groovy script, renamed properly, it generates something between the two step or create an assert “more complex” than those provided by SoapUI.

Follow four rows generates a random string

import org.apache.commons.lang.RandomStringUtils
String charset = (('a'..'z')).join() //+ ('0'..'9')).join()
Integer length = 9
String randomStringName = RandomStringUtils.random(length, charset.toCharArray())

Print value of a variable on SoapUI console

log.info "randomStringName: ${randomStringName}"

Define of a json array

def body = '''[{
         "OrderId": "1",
         "buyerEmail": "pinschertesting@gmail.com",
         "buyerName": "'''+"${randomStringName}"+'''",
         "earliestDeliveryDate": null,
         "earliestShipDate": null,
         "isPremium": null,
         "isPrime": null,
         "lastUpdateDate": 1462113597000,
         "latestDeliveryDate": null,
         "latestShipDate": null,
         "numOfItemsShipped": null,
         "numberOfItemsUnshipped": null,
         "orderCurrency": null,
         "orderItems": [],
         "orderStatus": "A",
         "orderTotal": null,
         "purchaseDate": 1462113597000,
         "shipServiceLevel": null,
         "shipmentServiceLevelCategory": null,
         "shippingAddress": null
}]'''

Sends a json object to a request

def restRequest = testRunner.testCase.getTestStepByName("Request updates the list of orders")
restRequest.setPropertyValue('Request', body)

Defines a variable that contains a value takes from a specific xpath value of Response; after that, compares value stored in variables

def buyerName_beforePatch = context.expand( '${#[Order TestSuite#/orders TestCase#Request All Orders]#Response#$.orders[4].buyerName}' )
log.info "buyerName_beforePatch: ${buyerName_beforePatch}"

def buyerName_afterPatch = context.expand( '${#[Order TestSuite#/orders TestCase#Copy of Request All Orders]#Response#$.orders[4].buyerName}' )
log.info "buyerName_afterPatch: ${buyerName_afterPatch}"

if ( buyerName_beforePatch != buyerName_afterPatch ) {
	log.info "Different values -> OK."
} else {
	log.info "Same values -> KO."
	return false
}

Takes the value of a Response and saves in a variable; JSON slurper parses text or reader content into a data structure of lists and maps

import groovy.json.JsonSlurper
import org.apache.commons.lang.math.RandomUtils
import java.util.Random

def orders = context.expand( '${#[Order TestSuite#/orders TestCase#Request All Orders]#Response#$.orders}' )
def slurperOrders = new JsonSlurper().parseText(orders)
log.info "slurperOrders: ${slurperOrders}"

Generates a random number between a range values

Integer randomInteger = RandomUtils.nextInt(new Random(),slurperOrders.size())
log.info "randomInteger: ${randomInteger}"

def aOrderId = slurperOrders[randomInteger].aOrderId
log.info "aOrderId: ${aOrderId}"

Defines a json object

def context = '''{
   "context":    {
      "cartid": null,
      "flowid": null,
      "id": "defaultSessionContextId",
      "userid": "pinscherTesting"
   },
   "cartItems":[]
}'''

Settings of a global property value

testRunner.testCase.testSuite.setPropertyValue("cartId", cartID)

Defines and takes property value

def cartID = testStep.getPropertyValue("id")

Conversion text in Json

import groovy.json.JsonOutput
def conv = JsonOutput.toJson(jsonSlurperRequest)

 

share me!:))Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInEmail this to someone
Print this page