## Templates

Templates group action sequences to a logical unit. You can think of templates as reusable components that are used in several tests. The maintenance is much more effective because the templates are referenced several times.

The template always has a unique name. Inside a test case we call the template by this unique name. Have a look at a first example:

    <template name="doCreateVariables">
        <create-variables>
            <variable name="var" value="123456789"/>
        </create-variables>

        <call-template name="doTraceVariables"/>
    </template>

    <template name="doTraceVariables">
        <echo>
            <message>Current time is: ${time}</message>
        </echo>

        <trace-variables/>
    </template>

The code example above describes two template definitions. Templates hold a sequence of test actions or call other templates themselves as seen in the example above.

Note The action calls other templates by their name. The called template not necessarily has to be located in the same test case XML file. The template might be defined in a separate XML file other than the test case itself:

XML DSL

    <testcase name="templateTest">
        <variables>
            <variable name="myTime" value="citrus:currentDate()"/>
        </variables>
        <actions>
            <call-template name="doCreateVariables"/>

            <call-template name="doTraceVariables">
                <parameter name="time" value="${myTime}">
            </call-template>
        </actions>
    </testcase>

Java DSL designer

    @CitrusTest
    public void templateTest() {
        variable("myTime", "citrus:currentDate()");

        applyTemplate("doCreateVariables");

        applyTemplate("doTraceVariables")
            .parameter("time", "${myTime}");
    }

Java DSL runner

    @CitrusTest
    public void templateTest() {
        variable("myTime", "citrus:currentDate()");

        applyTemplate(template -> template.name("doCreateVariables"));

        applyTemplate(template -> template.name("doTraceVariables")
                        .parameter("time", "${myTime}"));
    }

There is an open question when dealing with templates that are defined somewhere else outside the test case. How to handle variables? A templates may use different variable names then the test and vice versa. No doubt the template will fail as soon as special variables with respective values are not present. Unknown variables cause the template and the whole test to fail with errors.

So a first approach would be to harmonize variable usage across templates and test cases, so that templates and test cases do use the same variable naming. But this approach might lead to high calibration effort. Therefore templates support parameters to solve this problem. When a template is called the calling actor is able to set some parameters. Let us discuss an example for this issue.

The template "doDateCoversion" in the next sample uses the variable ${date}. The calling test case can set this variable as a parameter without actually declaring the variable in the test itself:

    <call-template name="doDateCoversion">
        <parameter name="date" value="${sampleDate}">
    </call-template>

The variable sampleDate is already present in the test case and gets translated into the date parameter. Following from that the template works fine although test and template do work on different variable namings.

With template parameters you are able to solve the calibration effort when working with templates and variables. It is always a good idea to check the used variables/parameters inside a template when calling it. There might be a variable that is not declared yet inside your test. So you need to define this value as a parameter.

Template parameters may contain more complex values like XML fragments. The call-template action offers following CDATA variation for defining complex parameter values:

    <call-template name="printXMLPayload">
        <parameter name="payload">
          <value>
            <![CDATA[
              <HelloRequest xmlns="http://www.consol.de/schemas/samples/sayHello.xsd">
                <Text>Hello South ${var}</Text>
              </HelloRequest>
            ]]>
          </value>
        </parameter>
    </call-template>

Important When a template works on variable values and parameters changes to these variables will automatically affect the variables in the whole test. So if you change a variable's value inside a template and the variable is defined inside the test case the changes will affect the variable in a global context. We have to be careful with this when executing a template several times in a test, especially in combination with parallel containers (seecontainers-parallel).

    <parallel>
        <call-template name="print">
            <parameter name="param1" value="1"/>
            <parameter name="param2" value="Hello Europe"/>
        </call-template>
        <call-template name="print">
            <parameter name="param1" value="2"/>
            <parameter name="param2" value="Hello Asia"/>
        </call-template>
        <call-template name="print">
            <parameter name="param1" value="3"/>
            <parameter name="param2" value="Hello Africa"/>
        </call-template>
    </parallel>

In the listing above a template print is called several times in a parallel container. The parameter values will be handled in a global context, so it is quite likely to happen that the template instances influence each other during execution. We might get such print messages:

    2. Hello Europe
    2. Hello Africa
    3. Hello Africa

Index parameters do not fit and the message 'Hello Asia' is completely gone. This is because templates overwrite parameters to each other as they are executed in parallel at the same time. To avoid this behavior we need to tell the template that it should handle parameters as well as variables in a local context. This will enforce that each template instance is working on a dedicated local context. See the global-context attribute that is set to false in this example:

    <template name="print" global-context="false">
        <echo>
            <message>${param1}.${param2}</message>
        </echo>
    </template>

After that template instances won't influence each other anymore. But notice that variable changes inside the template then do not affect the test case neither.