Variable – Source Reference


The Variable <Variable> element is used for defining data in one place and later refer to the data by using a Data Reference element. Hence, test data can be defined in a Variable in the beginning of the test scenario, which makes it easier to understand and to change. The Variable element can contain a simple String value, a list of String values or a complex data structure. It can have the data defined as child elements, or it can refer to data in an XML file, a property file or a Microsoft Excel spreadsheet. A variable can easily be added by drag and drop of an xml or property file into the test scenario editor.

Syntax

The syntax of the Variable element is

<Variable id="<id>">
<!-- Data structure-->
</Variable>

The ID attribute is required and specifies the ID that can be used by a Data Reference. The content of the Variable element can be a simple string, a list of entries, or a complex data structure. The filePath attribute is used to reference a file containing data to load. The path is relative to the test scenario file directory.

Example

Variable with a simple string. The log statement will write 33 to the console.

<Variable id="aVar">
<data:String>33</data:String>
</Variable>
<Log>
<data:Replaceable>
<DataReference referencedId="aVar" path="" />
</data:Replaceable>
</Log>
<Log>${aVar}</Log>

Variable with an array of strings and a loop iterating over the data. The log statement will write A, B and C to the console during the iteration.

<Variable id="aVar">
<Entry>A</Entry>
<Entry>B</Entry>
<Entry>C</Entry>
</Variable>
<IterateData id="loop" referencedId="aVar" dataPath="">
<Log>
<data:Replaceable>
<DataReference referencedId="loop" path="/Entry/*" />
</data:Replaceable>
</Log>
<Log>${loop.Entry}</Log>
</IterateData>

Variable with a complex structure and a Data Reference pointing to one field. The Log statement will write bb to the console.

<Variable id="aVar">
<AA>
  <BB>bb</BB>
  <CC>cc</CC>
</AA>
</Variable>
<Log>
<data:Replaceable>
<DataReference referencedId="aVar" path="/AA/BB/*" />
</data:Replaceable>
</Log>
<Log>${aVar.AA.BB}</Log>

Variable pointing to a file. The file contains the same data structure in as in the previous example

<Variable id="aVar" filePath="data.xml" />
<Log>
<data:Replaceable>
<DataReference referencedId="aVar" path="/AA/BB/*" />
</data:Replaceable>
</Log>
<Log>${aVar.AA.BB}</Log>

File content of data.xml:

<Root>
<AA>
  <BB>bb</BB>
<CC>cc</CC>
</AA>
</Root>

It is also possible to use a flat property file as in the following example:

<Variable id="data" filePath="data.properties" />
<Log>
<data:Replaceable>
<DataReference referencedId="data" path="data.value1" />
</data:Replaceable>
</Log>
<Log>${data.value2}</Log>

File content of data.properties:

value1=hello
value2=world

Finally, it is possible to reference a Microsoft Excel spreadsheet as the example below illustrates. Data must be defined from row 2. First column contains a label, while the next columns contain the values for data items. It is supported to use formulas in cells. Non-virtualized Test will evaluate the formulas and calculate the resulting cell values.

<Variable id="data" filePath="TestScript4_data.xls" />
<Log>=data.testcase[1].age_input</Log>
<Log>${data.testcase[1].age_input}</Log>

Content of Testscript4_data.xls:

image2021-3-1_20-10-32.png

 

Tip: For faster searching, add an asterisk to the end of your partial query. Example: cert*

BMC AMI DevX Workbench for Eclipse 23.06