Skip to main content

Using Expressions in Test Components

Expressions are fields that reference an item in the test scope. The item can be a variable or an inner value in a data structure, such as a JSON.

What You'll Need

Using Expressions

When working with structured data, expression is the path for reaching out a specific element. Most of the time, it's just object dot notation. Most expressions will start with the name of the variable the data is stored in. In this example, we will assume the data has been assigned to a variable named payload:

"data": {
"created_time": "1453198835",
"images": {
"thumbnail": {
"width": 150,
"url": "",
"height": 150
"Total-items": 1

If you want to reach the value of the created_time attribute:

If you want to check the width of the images:

Special Characters

The Total-items element is a bit tricky, because the minus sign ( - ) would be misunderstood and treated as a subtraction operation. For this reason, the dot notation would require square brackets:['Total-Items']


The above statements are valid for both JSON and XML. When you have to reference XML attributes, the dot notation is valid, but the attribute has to be written between the square brackets and preceded by the @.


<HotelList size="2">
<HotelSummary order="0">
<name>Hotel One</name>

If you want to check the size attribute, you have to write:


Simple Object Access Protocol (SOAP)

When you are working with SOAP API, the response might look something like this:

<?xml version="1.0"?>
<soap:Envelope xmlns:soap="" soap:encodingStyle="">
<m:Username>Tony Stark</m:Username>

You'll also need to include the namespace in the expression. In the above scenario, if you want to check the Username item, you have to write:


In XML, the root element is the variable itself, so you won't need to reference it explicitly.


Expressions can also contain directives to transform the data you are willing to evaluate. For instance:

<HotelList size="2">
<HotelSummary order="0">
<name>Hotel One</name>

Will count the number of instances of HotelSummary.

Expressions "Everywhere"

Expressions are automatically evaluated in the Expression field, but can also be introduced in other fields, such as value, with a specific notation.

In this example, we compare the actual size of the collection with the "size" attribute, by enclosing the expression within ${ .. }. The "type" attribute ensures the comparison will happen with a numeric comparator, rather than string.
assert Equals

Expression Language Extensions

Our API Testing expression language is mostly used to identify a path in a payload or reference a variable. But there's more to it. A number of extensions are available to generate calculated data, determine the quality of a value and so on.

These extensions can be used in any field that can be evaluated, which means in all expression fields, and all the fields where the value is wrapped in the ${...} brackets.


This is the main extension. It supports many useful functions.

  • exists(object : Object) : Boolean : an XML and JSON existence state is different by definition. Use this in an "if statement" if a test should work both with JSON and XML

  • contains(object : Object, substring : String) : Boolean : returns true whether the string version of "object" contains the "substring" sub-string.

    WSUtil.contains(, 'banana')
  • isInteger(string: String) , isFloat(string: String), isUrl(string: String), isEmail(string: String), isPhoneNumber(string: String), isBoolean(string: String), isArray(object: Object), isMap(object: Object), isCreditCard(string: String) : Boolean : evaluate the nature of a data item



Given any array, you can ask the system to create a random subset of it. One typical usage is when an iterator would turn out to be huge, and you prefer to cherry-pick a few items. The code below will return an array of five random elements off the artists array.


A hands on example:

- id: each
- id: assert-exists
expression: _1.href
- id: assert-exists
expression: payload.artists.pick(5)


Similar to the pick(n), this method will pick one random item off an array, and return it.


If you are testing XML, the pick() function must be WSUtil.pick(array,n). Considering the previous example, payload.artists.pick(5) becomes WSUtil.pick(payload.artists,5).


Utility functions for numbers.

  • random(min: Int, max: Int) : Int : generates a random integer number between min and max.

  • random(min: Int, max: Int, quantity: Int) : List : generates a list of random numbers



Plays with dates.

  • nowMillis() : Int : returns the current Unix epoch in milliseconds.
  • plusDays(millis: Int, days: Int): Int : returns the provided milliseconds, plus the provided number of days
  • plusHours(millis: Int, hours: Int): Int : returns the provided milliseconds, plus the provided number of hours
  • minusDays(millis: Int, days: Int) : Int : returns the provided milliseconds, minus the provided number of days
  • minusHours(millis: Int, hours: Int): Int : returns the provided milliseconds, minus the provided number of hours
  • format(millis: Int, format: String) : String : creates a timestamp with the given format, using the current timezone
  • format(millis: Int, format: String, timezone: String) : String : creates a timestamp with the given format, based on the provided timezone id
  • format(millis: Int, format: String, offset: Int) : String : creates a timestamp with the given format, based on the provided timezone offset
  • parse(timestamp: String) : Int : tries to parse the provided timestamp and convert it in milliseconds. It will use current timezone if not provided

Here's the conversion map for formats:

Ccentury of era (>=0)number20
Yyear of era (>=0)year1996
wweek of weekyearnumber27
eday of weeknumber2
Eday of weektextTuesday; Tue
Mmonth of yearmonthJuly; Jul; 07
dday of weektextTue
Dday of monthnumber2
ahalfday of daytextPM
Khour of halfday (0~11)number0
hclockhour of halfday (1~12)number12
Hhour of day (0~23)number0
kclockhour of day (1~24)number24
mminute of hournumber30
ssecond of minutenumber55
Sfraction of secondmillis978
Ztime zone offset/idzone-0800; -08:00; America/Los_Angeles


Generates fake data.

Addresses and Countries

  • F.streetName() - Generates a street name
  • F.streetAddressNumber() - Generates an address number
  • F.streetAddress() - Generates a street and address number. If secondary is specified, this method provides an apartment number.
  • F.secondaryAddress() - Generates an apartment number
  • F.zipCode() - Generates a ZIP code. Valid only for US states.
  • F.streetSuffix() - Generates a street suffix
  • F.citySuffix() - Generates a city suffix
  • F.cityPrefix() - Generates a city prefix
  • - Generates a city name
  • F.state() - Generates a state/province
  • F.buildingNumber() - Generates a build number
  • - Generates a country
  • F.countryCode() - Generates a country code
  • F.countryCodeSL() - Generates a country code in small letters

People and Identity

  • F.fullName() - Generates a full name
  • F.firstName() - Generates a first name
  • F.lastName() - Generates a last name
  • F.profession() - Generates a profession
  • F.timeZone() - Generates a time zone
  • - Generates a phone number
  • - Generates a mobile number


  • F.emailAddress() - Generates an email address. Note: These email addresses are randomly generated with real domains. Please be careful if you are using this in a test as there is a chance that some of them could be real email addresses.
  • F.domainName() - Generates a domain name
  • F.domainWord() - Generates a word
  • F.domainSuffix() - Generates a suffix
  • F.url() - Generates a URL
  • F.password(<minimumLength,maximumLength,includeUppercase,includeSpecial,includeDigit>) - Generates a password. For example, F.password(5,10,true,false, true).

Credit Card

  • F.creditCardNumber() - Generates a credit card number
  • F.creditCardExpiry() - Generates a credit card expiration date
  • F.creditCardType() - Generates a credit card type


  • F.productName() - Generates a product name
  • F.price() - Generates a price
  • F.promotionCode() - Generates random promotion code


  • F.companyName() - Generates a company name
  • F.suffix() - Generates a company suffix

Random Numbers

  • F.integer(<min,max>) - Generates an integer. For example, F.integer(2,20)
  • F.decimal(<min,max,maxdecimals>) - Generates a decimal number. For example, F.decimal(0,2,2)
  • F.uuid() - Generates a unique identifier


  • F.bool() - Generates a boolean value


Encryption utilities:

  • md5(input : String) : String : returns the md5 hash of the input string.
  • hash(input : String) : String : returns the SHA-1 hash of the input string, hex encoded.
  • base64(action: String, input: String) : decodes from or encodes into a base64 string. Action can either be 'encode' or 'decode'.
    WSCrypto.base64('encode', 'whatever')
  • sha256(input : String) : String : creates an hash of input using the SHA-256 algorithm.
  • sha256(input : String, secret : String) : String : encrypts input with secret using the HMAC-SHA256 algorithm.
  • hmacSha1(input : String, secret : String) : String : encrypts input with secret using the HMAC-SHA1 algorithm.