Sunday, January 27, 2008

Function Library Example 3

QTP Function Library - Example 3

Registering a Function to a test object by creating a new operation

Open a new test.

Open a Function Library.

Make sure that we are in Function Library.

Go to Insert-> Function Definition Generator.

Type the name of the function in the 'Function definition' area (I entered function_1). Let Type and Scope be the default i.e. ‘Function’ and ‘Public’ respectively.

Click on '+' in the ‘Arguments’ area and type 'var1' under Name to create a new variable and let the Pass Mode be default i.e. 'By Value'.

Check the 'Register to a test object' checkbox.

From Test Object dropdown select 'WinEdit' and in the Operation dropdown instead of selecting one of the values it already shows there, just type a new value 'New_operation_1'

In the ‘Additional information’ area, type 'my first operation on WinEdit' in Description text box.

Click Ok.

It copies the whole code it generated in the Preview area to the open library.

After it is copied to the already open library just complete the function body. Finally it looks like this:

'@Description my first operation on WinEdit
Public Function function_1(test_object, var1)
' TODO: add function body here
msgbox var1
End Function
RegisterUserFunc "WinEdit", "New_operation_1","function_1"

Save the function library.

Once it is saved, go to File->'Associate Library with Test' to associate this function library with the already open test.

After it is associated go the ‘Expert View’ of the test already open and type:

Window("title:=Filght Reservation").WinEdit("Attached text:=Name:", "height:=20").

as soon as you press . (dot), the list of operations which WinEdit supports are displayed by default (also called IntelliSense), just select New_operation_1 from there.

After selecting New_operation_1 just supply one argument to it because while creating the function we provided one argument called var1. So the final line of code in the Expert View now looks like:

Window("title:=Filght Reservation").WinEdit("Attached text:=Name:", "height:=20").New_operation_1"merry"

[You can open Flight Reservation window for this test, but it is not necessary]

Just Run the test. It does nothing for the WinEdit object or anything special. It just displays a message box with the value which we have supplied to it at the time of writing the code. This is just a simple example to show how it works.

If in this same test you go to ‘Keyword View’, it will show you New_operation_1 under the Operation column. It is displayed as an operation in the Keyword View Operation list when that test object is selected from the Item list. But here we are not using any Object repository (we are using Descriptive Programming) so we cannot select any object from the Item list as the Object Repository is empty. But if we do the below steps it surely will show:

We will record a simple step in a new action.

Now insert another new action in this test. (Insert->Call to New Action)

Make sure ‘Flight Reservation’ window is open.

Click on Record.

In the ‘Flight Reservation’ window, in the Name text box enter any name.

Stop recording.

Now go to the ‘Keyword View’. In this ‘Keyword View’, go to the row which has 'Name' under item. Make a single click under Operation column in that row (click where it shows Set) it will show a dropdown and you will be able to see New_operation_1 in that dropdown. This New_operation_1 will be there for every object of class WinEdit.

Now if you open any new test and again type

Window("title:=Filght Reservation").WinEdit("Attached text:=Name:", "height:=20").

Now QTP will not display New_operation_1unless otherwise you associate the library we created earlier to this new test.


Also See:

QTP Function Library
QTP Function Library - Example 1
QTP Function Library - Example 2
QTP Function Library - Example 4
ExecuteFile