Basic VBScript syntax

Relevant for: GUI actions, scripted GUI components, and function libraries

The Editor used to develop actions, scripted components, or function libraries is a VBScript Editor. Therefore, in addition to entering test object statements and other testing-oriented functions, you can manually enter any valid Microsoft VBScript statement.

Overview

VBScript is an easy-to-learn, yet powerful scripting language. You can use VBScript to develop scripts to perform both simple and complex object-based tasks, even if you have no previous programming experience.

You can insert VBScript statements manually in the Editor. You can also use the Step Generator from the Editor or from the Keyword View to insert many VBScript statements.

This topic provides some basic guidelines to help you use VBScript statements to enhance your action, scripted component, or function library.

Each VBScript statement has its own specific syntax rules. If you do not follow these rules, errors will be generated when you run the problematic step. For details, see General syntax rules and guidelines and Formatting text.

Additionally, if you try to move to the Keyword View from the Editor, OpenText Functional Testing lists any syntax errors found in the document in the Errors pane. You cannot switch to the Keyword View without fixing or eliminating the syntax errors.

Back to top

VBScript Reference

Learn more about VBScript statements in the Microsoft VBScript references:

  • VBScript User's Guide and Language Reference
  • Windows Scripting

Note: The language reference you consult may contain both VBScript and JScript information or examples, OpenText Functional Testing supports only VBScript. Do not attempt to use JScript examples from the VBScript reference in your tests.

Back to top

Comments

Relevant for: GUI actions, scripted GUI components, and function libraries

A comment is a line or part of a line in a script that is preceded by an apostrophe ('). During a run session, OpenText Functional Testing does not process the comments. You can use comments to explain sections of an action, a scripted component, or a function library, to improve readability, and to make your scripts easier to maintain and update.

The following example shows how a comment describes the purpose of the statement below it:

'Sets the word "myname" into the "username" edit box.
Browser("Advantage Shopping").Page("Advantage Shopping").WebEdit("username").Set "myname"

By default, comments are displayed in green inside your VBScript code. You can customize the appearance of comments in the Fonts and Colors pane of the Text Editor tab in the Options dialog box (Tools > Options > Text Editor tab > General node). For details, see Fonts and Colors Pane.

Tip:  

  • You can comment or uncomment a block of text by selecting Edit > Format > Comment/Uncomment.

  • You can also add a comment line using the VBScript Rem statement. For more details, see the Microsoft VBScript Reference.

Back to top

Parameter indications

Relevant for: GUI actions and scripted GUI components

You can use OpenText Functional Testing to enhance your tests by parameterizing values. A parameter is a variable that is assigned a value from an external data source or generator.

When you create a parameter in the Keyword View, OpenText Functional Testing creates a corresponding line in VBScript in the Editor.

For example, if you define the value of a method argument as a Data pane parameter, OpenText Functional Testing retrieves the value from the Data pane using the following syntax:

Object_Hierarchy.Method DataTable (parameterID, sheetID)

Item

Description

Object_Hierarchy

The hierarchical definition of the test object, consisting of one or more objects separated by a dot.

Method

The name of the method that OpenText Functional Testing executes on the parameterized object.

DataTable

The reserved object representing the data table.

parameterID

The name of the column in the data table from which to take the value.

sheetID

The name of the sheet in which the value is stored. If the parameter is a global parameter, dtGlobalSheet is the sheet ID.

Example: Suppose you are creating a test for the Advantage Online Shopping site, and you select Italy as your shipping address. The following statement would be inserted into an action in your test in the Editor:

Browser("Advantage Shopping").Page("Advantage Shopping").WebList("countryListbox").Select "Italy"

Now suppose you parameterize the address value, and you create an Address column in the Data pane. The previous statement would be modified to the following:

Browser("Advantage Shopping").Page("Advantage Shopping").WebList("countryListbox").Select DataTable("Address", dtGlobalSheet) 

In this example, Select is the method name, DataTable is the object that represents the data table, Address is the Data pane parameter (column name), and dtGlobalSheet indicates the Global sheet in the Data pane.

For more details on using and defining parameter values, see Parameterize object values.

Back to top

Parentheses

Relevant for: GUI actions, scripted GUI components, and function libraries

When programming in VBScript, it is important that you follow the rules for using or not using parentheses () in your statements. You must use parentheses around method arguments if you are calling a method that returns a value and you are using the return value.

For example, use parentheses around method arguments if you are returning a value to a variable, if you are using the method in an If statement, or if you are using the Call keyword to call an action or function. When working with actions, you also need to add parentheses around the name of a checkpoint if you want to retrieve its return value.

Tip: If you receive an Expected end of statement error message when running a step, it may indicate that you need to add parentheses around the arguments of the step's method.

Example: Following are several examples showing when to use or not use parentheses around the method arguments.

Parentheses required when:

  • The method returns a value to a variable:

    Set webelementobj = Browser("Advantage Shopping").Page("Advantage Shopping").WebTable("PRODUCT NAME").ChildItem(2, 2, "WebElement", 0)
    webelementobj.Click
  • Call is being used:

    Call RunAction("BookFlight", oneIteration)

    or

    Call MyFunction("Hello World")
    ...
    ...
  • The method is used in an If statement:

    If Browser("index").Page("index").Link("All kinds of").WaitProperty("attribute/readyState", "complete", 4)
    Then 
        Browser("index").Page("index").Link("All kinds of").Click
    End If 
  • The method returns the value of the checkpoint:

    a = Browser("MyBrowser").Page("MyPage").Check(CheckPoint("MyProperty"))

Parentheses not required when:

The method does not return a value:

Browser("Advantage Shopping").Page("Advantage Shopping").WebTable("PRODUCT NAME").Click 2,2

Back to top

Calculations

Relevant for: GUI actions, scripted GUI components, and function libraries

You can write statements that perform simple calculations using mathematical operators. For example, you can use a multiplication operator to multiply the values displayed in two text boxes in your Web site. VBScript supports the following mathematical operators:

Operator

Description

+

addition

subtraction

negation (a negative number)

*

multiplication

/

division

^

exponent

In the following example, the multiplication operator is used to calculate the total price of three speakers:

'Retrieves the price of a speaker using the GetROProperty method
Price = Browser("Advantage Shopping").Page("Advantage Shopping").WebElement("$44.99").GetROProperty("innertext")
'Multiplies the price by 3
Total price = Price * 3
'Inserts the total price into a message box
msgbox("The total price is "& total price.")

Back to top

Variables

Relevant for: GUI actions, scripted GUI components, and function libraries

You can specify variables to store test objects or simple values in your action, scripted component, or function library. When using a variable for a test object, you can use the variable instead of the entire object hierarchy in other statements. Using variables in this way makes your statements easier to read and to maintain.

To specify a variable to store an object, use the Set statement, with the following syntax:

Set ObjectVar = ObjectHierarchy

In the example below, the Set statement specifies the variable UserEditBox to store the full Browser.Page.WebEdit object hierarchy for the username edit box. The Set method then enters the value tutorial into the username edit box, using the UserEditBox variable:

Set UserEditBox = Browser("Advantage Shopping").Page("Advantage Shopping").WebEdit("username")
UserEditBox.Set "tutorial"

Note: Do not use the Set statement to specify a variable containing a simple value (such as a string or a number). The example below shows how to define a variable for a simple value:

MyVar = Browser("Advantage Shopping").Page("Advantage Shopping").WebEdit("username").GetTOProperty("type")

You can also use the Dim statement to declare variables of other types, including strings, integers, and arrays. This statement is not mandatory, but you can use it to improve the structure of your action, scripted component, or function library.

Example: The examples below demonstrate using the Dim statement to declare a variable:

In an action or scripted component (using the TrackingNumber variable):

Dim TrackingNumber
TrackingNumber = Browser("Advantage Shopping").Page("Advantage Shopping").WebElement("trackingNumberLabel").GetTOProperty("innertext")

In a function libraries (using the actual_value variable):

Dim actual_value
	' Get the actual property value
	actual_value = obj.GetROProperty(PropertyName)

Back to top

See also: