Using Variables

This section discusses how to use variables.

Variables enable you to store a value in one step and access that data in a subsequent step. Variables are useful when your test requires a value that will not be known until the test is executed.

Variables are always prefixed by an ampersand (&) – when you set their value and when they represent a value.

Store a value for a variable either by placing the ret=&varname parameter in the Parameters field in a step that supports return values, or by using a Variable.Set_Value step.

You can refer to the values stored in a variable in two ways:

  • Use the variable in the Recognition field of a Conditional. If_Then step or in the Parameter field for any step that takes a parameter.

  • Use the variable in the Value field of any step that sets or verifies the value of an object on the page.

Important! Variables are not automatically initialized. If the variable is not assigned a value in either the recognition or value column, the value will be the variable name. For example, if the variable &var1 was never initialized, it will return &var1.

For example, suppose you have the following test instructions for a test of the Maintain Proposal component:

1. From the Maintain Proposal page, make a note of the new proposal ID.
2. Click on the version ID link and verify that the same proposal ID appears 
   on the Resource Estimate page.

The application generates a proposal ID when the test is executed, so it is not known ahead of time.

The following example shows one way to automate these steps using a variable. The first step gets the value of the Proposal ID field from the application and stores it to the &propID variable. The second step clicks the version ID link, which brings up the Resource Estimate page. The third step verifies the value of the Proposal ID field on the Resource Estimate page against the value saved in the &propID variable:

Image: Example of Using a Variable

This example illustrates using a variable to verify the value of a property on a page.

Example of using a variable

The length of PTF fields (Value, Recognition, Parameters) is limited to 254 characters. To construct a variable string that is longer than 254 characters, you will need to wrap the string into multiple variables and concatenate them for usage in verification or set value steps.

Image: Example Using Multiple Variables for a String Over 254 Characters

This example illustrates the steps to create smaller variables and then concatenate the string for use in a LongText.Verify step.

Example using multiple variables for string over 254 characters

For additional examples of variable usage, see the “Test Language Reference” chapter, especially the Conditional.If_Then and Variable.Set_Value steps.

Wrapping Variables in Quotes

Variable assignments, comparisons, and functions may use special characters (such as the equals sign, parentheses, and so forth) to manage text string operations. Quotes are necessary in these situations to help PTF distinguish strings that contain these characters from the actual operators themselves.

Quotes are only necessary in the following situations:

  • Around text being assigned to a variable in the Recognition field.

  • Around text being provided as a parameter of a function (in either the Parameters or Value field).

  • Around text being used in a Conditional step in the Recognition field.

Assigning Variables in the Recognition Field

Use the following guidelines when assigning variables in the Recognition field:

  • Use quotes around all text strings.

  • Use double quotes (““) to return one quotes character. When a final text string result is expected to include leading and trailing quotes characters, the parameters supplied should be lead and trailed by three quotes characters, for example: ”””Hello”””.

Note: PTF will return an error (unrecognized or illegal variable format) if the user fails to wrap a text string being assigned to a variable in quotes.

This table displays examples of setting the variable &MyVar in the Recognition field and the expected result:

Recognition

Expect Result

&MyVar=&PriorVar

Contents of the &PriorVar variable

&MyVar=”&PriorVar”

&PriorVar

&MyVar=”Hello”

Hello

&MyVar=”““Hello”““

“Hello”

&MyVar=Hello

error message

Assigning Variables in the Value field

It is a good practice to assign variable values in the Value field (rather than the Recognition field) for the following reasons:

  • It simplifies variable assignments.

  • It reinforces habits that allow the user to take fuller advantage of dynamic PTF functionality such as Test Case data and reserved words (which are only recognized in the Value field).

Use the following guidelines when assigning variables in the Value field:

  • Quotes are not necessary to identify a text string in the Value field for the purpose of a variable assignment step.

  • The CONCAT() function should be used to return a text string containing special characters such as ampersands or pound symbols that could be interpreted as a variable or reserved word.

  • Double quotes are generally not needed, because text entered into the Value field is interpreted literally.

This table displays examples of setting the variable &MyVar in the value field and the expected result:

Recognition

Value

Expected Result

&MyVar

&PriorVar

Contents of &PriorVar variable

&MyVar

CONCAT(“&PriorVar”)

&PriorVar

&MyVar

Hello

Hello

&MyVar

“Hello”

“Hello”

&MyVar

““Hello”“

““Hello”“

Displaying Variables in Message Logs

You can log variables instead of variable values as a message in a test execution log.

To log a variable, prefix an additional ampersand (&) to the variable in the Recognition field of the selected test step.

Similarly, you can prefix an ampersand to variable string in the Value field so that the variable is displayed in the details section of the message log.

For example, set “&&adsname” is my dataset in the Recognition and Value field. When the test execution log is generated, the variable string “&adsname” is my dataset is logged as a message.

Image: Prefixing Ampersand to Variables in Test Steps

This example illustrates how an additional ampersand (&) can be prefixed to variables in test steps to print the variable string in message logs.

Prefixing Ampersand to Variables in Test Steps

This table shows examples of setting a variable (&adsname) in the Recognition field and the expected result in the message log:

Recognition

Expected Result

&adsname is my dataset

PIVOTGRID_DEFINITION is my dataset

&&adsname is my dataset

&adsname is my dataset

“&&adsname” is my dataset

“&adsname” is my dataset

““&&adsname”” is my dataset

““&adsname”” is my dataset

“““&&adsname””” is my dataset

“““&adsname””” is my dataset