FAQ

How can you write more flexible, universal functions with SilkTest (the 4Test @ operator)?

Detail

The @ Operator is one of 4Test’s best kept secrets.  The @ operator, called the reference operator, is the closest thing in 4Test to a pointer. This little symbol makes it possible to pass a function name as a string argument and convert a string to a value of an enumerated type. Having written 4Test for nearly a decade, it’s still no surprise when a new usage for the reference operator is discovered.

According to the SilkTest On-line Help: “The reference operator (@expression) lets you refer to functions and variables indirectly, by name. It uses the value of its operand to refer to variable, the fields in a record, function, method, property or child window.” In other words, it allows 4Test programmers to write more flexible, universal functions in a style that might even be termed ‘elegant’.

The best way to understand how the employ the @ operator is to look at some examples.

@ to indirectly refer to a function or method name:

VOID MyFunction (STRING sFunctionNameToCall)
MyDialogBox.MyTextField.@( sFunctionNameToCall)( )

In this example, the @ operator converts the name of the function to be called into an actual function call. Used in this manner, it promotes the development of general purpose functions that can be reused across multiple projects.

@ to indirectly refer to the name of a control or child window:

VOID MyFunction (STRING sFieldToModify)
MyDialogBox.@(sFieldToModify).SetText(“hello world”)

Because the 4Test compiler won’t allow passing an argument of type WINDOW that has not been declared, the ability to refer to a child window as a string is a major convenience. In this example, the control name represented by the STRING variable ‘sFieldToModify’ is concatenated to its parent to form a legal window by using the @ operator to strip off the quotes.

@ to reference the members of a record type:

type EMPLOYEE_DATA is record

  • STRING sLastName
  • STRING sFirstName
  • INTEGER iAge
  • INTEGER iYearsEmployed

VOID MyFunction (EMPLOYEE_DATA edEmployee )

  • STRING sMemberName
  • for each sMemberName in FieldsOfRecord (EMPLOYEE_DATA)
  • print (edEmployee.@sMemberName)

@ to reference the values of an enumerated type:

type COLOR is enum

  • Red
  • Green
  • Blue

window DialogBox MyDialog

  • CheckBox Red
  • CheckBox Green
  • CheckBox Blue

VOID MyFunction ()

  • COLOR eColor
  • for each eColor in ValuesOfEnum (COLOR)

MyDialog.@([STRING] eColor).SetState (TRUE)

A bit unusual, but there are applications for the @ operator to refer to the values of an enumerated type. In this example the value of the enum is used to refer to a each member of a set of CheckBoxes that have been given identifiers that match the enum values. The @ operator allows the value of the enum (casted to a string) to be concatenated to the parent dialog to form a legal window name.

@ used in a test plan symbol:

$wToInvoke.@$sFieldName
$wToInvoke = Find
$sFieldName = “OK”

In this example, the @ operator is embedded in a testplan symbol to provide a way to construct legal window names by concatenating the parts of a window name hierarchy much in the same way as was shown in child window example above. Look for other ways to make test plan symbols work for you in an upcoming automatedtesting.com article.

The next time you find yourself writing a huge switch statement, think again and determine whether the @ operator can let you accomplish your objective in a single statement.