Utility Statements in QTP – Part 3

Hello Friends,
This is in continuation from Part-1 and Part-2 of posts. Here we will cover following utility statements-
GetLastError Statement
InvokeApplication Statement
Print Statement
RegisterUserFunc Statement
RunAction Statement
SetLastError Statement
UnregisterUserFunc Statement
Wait Statement

GetLastError Statement

GetLastError returns the error code of the last/most recent error. It is used with the DescribeResult Statement.

Browser(“mybrowser”).Page(“mypage”).Image(“myimage”).Click 23, 47
errX = GetLastError
print (DescribeResult(errX))

In the above example, the script fails because the Login image was not found on the page. The error number is retrieved Using the GetLastError function and description of error using DescribeResult Statement.

SetLastError Statement

Inserts a VBScript error into the test script.


InvokeApplication Statement

InvokeApplication is used to invoke an executable application i.e. ‘.exe’ files.

InvokeApplication “C:Program FilesInternet ExplorerIEXPLORE.EXE”

Print Statement

Print is very useful and simple to use statement, generally used in place of ‘msgbox’. It displays information in the QuickTest Print Log window during the

run session. The log window remains open until you close it manually.

Print “This is an example”
Print Browser(“MyBro”).GetROProperty(“title”)

RegisterUserFunc Statement

Using RegisterUserFunc, you can add a new method or can override the existing method of any test object class.

For example, ‘Set’ is a method of WebEdit class, which sets value in the edit box.

Using RegisterUserFunc, you can change the behaviour of this method or you can add new method to WebEdit class.


Function NewSet (obj, val)
 Dim MyWord
 MyWord = UCase(val)   ‘ Returns val in upper case
End Function

RegisterUserFunc “WebEdit”, “Set”, “NewSet”
Browser(“MyBro”).Page(“MyPage”).WebEdit(“name”).Set “abhikansh”
UnRegisterUserFunc “WebEdit”, “Set”

In above code, you have just overridden ‘Set’ method with your new function ‘NewSet’. Now default behavior of the Set is changed. Whatever you type now, it will be written in upper case in the edit box.

Enables you to add new methods to test objects or change the behavior of an existing test object method during a run session.

When you use this statement, QuickTest uses your user-defined function as a method of a specified test object class for the remainder of a run session, or until you unregister the method.

If the specified method name does not already exist for the test object, it becomes a new method for the object. If the method name is a defined QuickTest method for the object, your definition (temporarily) overrides the existing functionality of the specified method.

Please note that..

1. Registered method applies only to the test or library file in which you register it.
2. It is applicable to current run session only.
3. QuickTest clears all function registrations at the beginning of each run session.
4. UnRegisterUserFunc instructs QuickTests to stop using the current registration of the method.

RunAction statement

The RunAction statement runs the specified action (if associated with your test).
If you want to run external action, which is not associated with your test, then you have to use (Insert > Call to Action) or (Insert > Copy of Action) options from QTP menu.

RunAction “Action2”, oneIteration

Easiest and more accurate way to use RunAction is through Menu.

Wait Statement

Wait is one of the most commonly used utility statement. Used for a pause of specified seconds, during a run session.


Above statement will cause a pause of 10 seconds before moving to the next steps. Its a method of synchronization.

For other Synchronization methods, please refer >> http://www.qtpschool.com/2011/03/importance-of-qtp-synchronization.html

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top