Exam > Handlebar support >
On this page

Handlebar support

The Handlebars templates may be used for templating values in Exam commands.

Overview

Exam provides following built-in helpers groups:

Date helpers
{{now "yyyy-MM-dd'T'HH:mm Z" tz="GMT+3" minus="1 y, 2 months, d 3" plus="4 h, 5 min, 6 s"}} will produce: "2021-01-09T19:31 +0300" 
{{today "yyyy-MM-dd" minus="1 y, 2 months, d 3"}} will produce: "2021-01-09" 
{{date '01.02.2000 10:20' format="dd.MM.yyyy HH:mm" minus="1 h" plus="1 h"}} will produce: object of class java.util.Date = "Tue Feb 01 10:20:00 MSK 2000" 
{{weeksAgo 2}} will produce: object of class java.util.Date = "Sat Feb 26 00:00:00 MSK 2022" 
{{daysAgo 2}} will produce: object of class java.util.Date = "Thu Mar 10 00:00:00 MSK 2022" 
{{dateFormat date "yyyy-MM-dd'T'HH:mm O" tz="GMT+3" minus="1 y, 2 months, d 3" plus="4 h, 5 min, 6 s"}} will produce: "1998-10-30T14:25 GMT+3" (given context has variables: {date=Sun Jan 02 10:20:00 MSK 2000})
Data matcher helpers
{{string}} will produce: "${json-unit.any-string}" (given context has variables: {placeholder_type=json})
{{number}} will produce: "${json-unit.any-number}" (given context has variables: {placeholder_type=json})
{{bool}} will produce: "${json-unit.any-boolean}" (given context has variables: {placeholder_type=json})
{{ignore}} will produce: "${json-unit.ignore}" (given context has variables: {placeholder_type=json})
{{regex '\d+'}} will produce: "${json-unit.regex}\d+" (given context has variables: {placeholder_type=json})
{{matches 'name' 'params'}} will produce: "${json-unit.matches:name}params" (given context has variables: {placeholder_type=json})
{{formattedAs "yyyy-MM-dd'T'hh:mm:ss"}} will produce: "${json-unit.matches:formattedAs}yyyy-MM-dd'T'hh:mm:ss" (given context has variables: {placeholder_type=json})
{{isoLocalDate}} will produce: "${json-unit.matches:formattedAs}yyyy-MM-dd" (given context has variables: {placeholder_type=json})
{{isoLocalDateTime}} will produce: "${json-unit.matches:formattedAs}ISO_LOCAL" (given context has variables: {placeholder_type=json})
{{formattedAndWithinNow "yyyy-MM-dd'T'hh:mm:ss" "5s"}} will produce: "${json-unit.matches:formattedAndWithinNow}yyyy-MM-dd'T'hh:mm:ss|param|5s" (given context has variables: {placeholder_type=json})
{{isoLocalDateAndWithinNow "5s"}} will produce: "${json-unit.matches:formattedAndWithinNow}yyyy-MM-dd|param|5s" (given context has variables: {placeholder_type=json})
{{isoLocalDateTimeAndWithinNow "5s"}} will produce: "${json-unit.matches:formattedAndWithinNow}ISO_LOCAL|param|5s" (given context has variables: {placeholder_type=json})
{{formattedAndWithin 'yyyy-MM-dd' '5s' '1951-05-13'}} will produce: "${json-unit.matches:formattedAndWithin}yyyy-MM-dd|param|5s|param|1951-05-13" (given context has variables: {placeholder_type=json})
{{isoLocalDateAndWithin '5s' '1951-05-13'}} will produce: "${json-unit.matches:formattedAndWithin}yyyy-MM-dd|param|5s|param|1951-05-13" (given context has variables: {placeholder_type=json})
{{isoLocalDateTimeAndWithin '5s' '1951-05-13'}} will produce: "${json-unit.matches:formattedAndWithin}ISO_LOCAL|param|5s|param|1951-05-13" (given context has variables: {placeholder_type=json})
{{after '2000-01-31T23:59:59.000'}} will produce: "${json-unit.matches:after}2000-01-31T23:59:59.000" (given context has variables: {placeholder_type=json})
{{before '2000-01-31T23:59:59.000'}} will produce: "${json-unit.matches:before}2000-01-31T23:59:59.000" (given context has variables: {placeholder_type=json})
Misc helpers
{{set 1 "someVar"}} will produce: object of class java.lang.Integer = "1" 
{{getOr var "default value"}} will produce: "default value" 
{{map key='value'}} will produce: object of class java.util.Collections$SingletonMap = "{key=value}" 
{{NULL}} will produce: null 
{{eval '#var'}} will produce: object of class java.lang.Integer = "2" (given context has variables: {var=2})
{{resolve 'today is {{today}}'}} will produce: "today is Sat Mar 12 00:00:00 MSK 2022" 
{{resolveFile '/hb/some-file.txt'}} will produce: "today is Sat Mar 12 00:00:00 MSK 2022" 
{{prop 'system.property' 'optional default'}} will produce: "optional default" 
{{env 'env.property' 'optional default'}} will produce: "optional default" 

Examples

Data matchers

Data types matchers

Given


givenDataJson:
{
  "string": "some string",
  "number": 123,
  "bool": true,
  "ignore": "anything 123",
  "regex": "123"
}

Then


The following markup:
<e:json-check actual="givenDataJson"> { "string": "{{string}}", "number": "{{number}}", "bool": "{{bool}}", "ignore": "{{ignore}}", "regex": "{{regex '\\d+'}}" } </e:json-check>
will be rendered as:
{ "string": "${json-unit.any-string}", "number": "${json-unit.any-number}", "bool": "${json-unit.any-boolean}", "ignore": "${json-unit.ignore}", "regex": "${json-unit.regex}\\d+" }
Date matchers

Given


givenDateJson:
{ 
  "formattedAs": "2022/03/12T15:26.17",
  "isoLocalDate": "2022-03-12",
  "isoLocalDateTime": "2022-03-12T15:26:17",
  
  "formattedAndWithinNow": "2022/03/12T15:26.17",
  "isoLocalDateAndWithinNow": "2022-03-12",
  "isoLocalDateTimeAndWithinNow": "2022-03-12T15:26:17",
  
  "formattedAndWithin": "2022/03/12T15:26.17",
  "isoLocalDateAndWithin": "2022-03-12",
  "isoLocalDateTimeAndWithin": "2022-03-12T15:26:17",
  
  "after": "2022-03-12T15:26:17",
  "before": "2022-03-12T15:26:17"
}

Then


The following markup:
<e:json-check actual="givenDateJson"> { "formattedAs": "{{formattedAs 'yyyy/MM/dd\'T\'HH:mm.ss'}}", "isoLocalDate": "{{isoLocalDate}}", "isoLocalDateTime": "{{isoLocalDateTime}}", "formattedAndWithinNow": "{{formattedAndWithinNow 'yyyy/MM/dd\'T\'HH:mm.ss' '5s'}}", "isoLocalDateAndWithinNow": "{{isoLocalDateAndWithinNow '1d'}}", "isoLocalDateTimeAndWithinNow": "{{isoLocalDateTimeAndWithinNow '5s'}}", "formattedAndWithin": "{{formattedAndWithin 'yyyy/MM/dd\'T\'HH:mm.ss' '5s' (dateFormat (now))}}", "isoLocalDateAndWithin": "{{isoLocalDateAndWithin '1d' (dateFormat (now))}}", "isoLocalDateTimeAndWithin": "{{isoLocalDateTimeAndWithin '5s' (dateFormat (now))}}", "after": "{{after (dateFormat (now minus='1 hour'))}}", "before": "{{before (dateFormat (now plus='1 hour'))}}" } </e:json-check>
will be rendered as:
{ "formattedAs": "${json-unit.matches:formattedAs}yyyy/MM/dd'T'HH:mm.ss", "isoLocalDate": "${json-unit.matches:formattedAs}yyyy-MM-dd", "isoLocalDateTime": "${json-unit.matches:formattedAs}ISO_LOCAL", "formattedAndWithinNow": "${json-unit.matches:formattedAndWithinNow}yyyy/MM/dd'T'HH:mm.ss|param|5s", "isoLocalDateAndWithinNow": "${json-unit.matches:formattedAndWithinNow}yyyy-MM-dd|param|1d", "isoLocalDateTimeAndWithinNow": "${json-unit.matches:formattedAndWithinNow}ISO_LOCAL|param|5s", "formattedAndWithin": "${json-unit.matches:formattedAndWithin}yyyy/MM/dd'T'HH:mm.ss|param|5s|param|2022-03-12T15:26:17", "isoLocalDateAndWithin": "${json-unit.matches:formattedAndWithin}yyyy-MM-dd|param|1d|param|2022-03-12T15:26:17", "isoLocalDateTimeAndWithin": "${json-unit.matches:formattedAndWithin}ISO_LOCAL|param|5s|param|2022-03-12T15:26:17", "after": "${json-unit.matches:after}2022-03-12T14:26:17", "before": "${json-unit.matches:before}2022-03-12T16:26:17" }