Using Variables in Scripts

A value contained in double quotes (a string) in any Silk Test Workbench command can be replaced by a value in a variable. Typically this is a string variable. However, you can use variables for other data types such as Integer or Double when inputting values into a field.

The most common use of variables is as a substitute for a literal value used as input to a control, such as an edit box. Any method which enters data into a control, such as SetText() or TypeKeys() can substitute a variable for a literal value. To do this, declare the variable, assign its value, then replace the literal value with the variable. For example:

browser.DomTextField("@id='name-search:lastName'").SetText("Smith")

'becomes:

Dim lastName = "Smith"

.DomTextField("@id='name-search:lastName'").SetText(sLastName)

When selecting an item from a list or menu you can also use a variable for the entry selection. For example:

browser.DomListBox("@id='quick-link:jump-menu'").Select("Agent Lookup")

'becomes:

Dim selection = "Auto Quote"

browser.DomListBox("@id='quick-link:jump-menu'").Select(sSelection)

Values returned by methods or properties can be placed into a variable for evaluation. A common use of this is to return a value of the Boolean data type for evaluation in conditional logic. The following code shows how a Boolean value returned by the AllowsMultiSelect property is used to evaluate whether a list box control allows selection of multiple items.

Dim canMultiSelect As Boolean

' Returns true if the second list box in the window allows multiple selections

canMultiSelect = mainWindow.ListBox("[2]").AllowsMultiSelect

If canMultiSelect Then

    MsgBox ("Employee selection list box allows to select multiple employees.")

Else

    MsgBox ("Employee selection list box allows to only select one employees.")

EndIf

A variable can replace the raw attach name of a control. Use the string concatenate character &. For example:

browser.DomLink("@caption='Eye'").Select()

'could also read:

Dim sLink As String

sLink = "Eye"

browser.DomLink("@caption=" & sLink).Select()
Tip: Silk Test Workbench also supports wildcard characters to account for variability in a GUI map or a control or window.

Parameters in scripts can be shared with visual tests, so the same data can be used across a testing solution. For details, see Passing Data Between Scripts and Visual Tests.