UFT Functions and Function Libraries – UFT/QTP Training Tutorial 9

This is the 9th UFT Tutorial in our UFT training series. Please Click here to check out list of all tutorials we covered till now. Today learn about UFT functions and function libraries.

Overview of UFT Functions and Function Libraries:

A function is a set code that is useful to perform an important task during testing and users always want to work on that task and repeat those task several times, and also want the function to be easily available, but there is no appropriate method available for it, by default.

To fulfill tester needs, UFT is holding some built-in functions and methods that might help testers during test.There is some situation where user performs some important task that is not available by default for a particular test object class. In this case, user creates a user-defined function and save it in a function library file to perform a task.

In the given below example, you will get to know that how to create a function and function library that checks the date format on a page generated by the Mercury Tours Web site, and how to add  the function library to entire test. Adding a function library with a test permits user to call any of the functions in the function library from that test.

Create a Function

In this exercise, we will create a function to check whether a date displays in MM/DD/YYYY format or not and also check the month should not exceed 12 and the date should not exceed 31. This function will be later called by UFT test.

First, start UFT and open the Mercury Tours test, created earlier. Ensure that only the Web Add-in is loaded.

After opening the Mercury Tours test in the UFT window, open the Solution dialog box. To the Solution dialog box, go to File à Open à Solution. The Open Solution dialog box opens.

UFT Open Solution

In Solution dialog box, browseMercury Tours1.ftsln file and click Open. Shown in the figure below.

UFT Open Solution

Second, after opening Mercury Tours1.ftsln file in UFT window, create a new function library by clicking on“New” down arrow button to select New Function Library from the list. The New Function Library dialog box opens.

UFT New Function Library

In the New Function Library dialog box, first create a new folder by giving name Mercury Tours_Function Libraries to hold and locate Function Library files. Inside a new folder, create a new Function Library by giving a new name or by leaving the default name mentioned in the Name text box then click on “Create”.

Here, Folder Name for Function Library: Mercury Tours_Function Libraries and Function Library file name: Mercury Tours_CheckDateFunction.qfl.

To open function library in the document pane of the UFT window, go to File > Open > Function Library. A blank function library opens in the document pane.

Third, paste the given below function into the editor.

Use given below function in the editor to check that date displays in MM/DD/YYYY format and month should be less than 12, date should be less than 31 and year should be less than 2000.

'The following function checks whether a date string (dateStr) 
'has 10 characters representing MM/DD/YYYY 
Function check_data_validity(dateStr)
Dim firstSlashPos, secondSlashPos
Dim mmPart, ddPart, yyyyPart
firstSlashPos = inStr(dateStr,"/")
secondSlashPos = inStrRev(dateStr,"/")
If (firstSlashPos <> 3 or secondSlashPos <> 6) Then
reporter.ReportEventmicFail,"Format check","Date string is missing at least one slash ( / )."
check_data_validity = False
Exit function
End If
mmPart = mid(dateStr, 1,2)
ddPart  = mid (dateStr, firstSlashPos+1, 2)
yyyyPart = mid(dateStr, secondSlashPos +1 , 4)
If mmPart > 12 Then
reporter.ReportEventmicFail, "Format Check", "The month value is invalid. It exceeds 12." 
check_data_validity = False
Exit function
End If
If ddPart > 31 Then
reporter.ReportEventmicFail, "Format Check", "The date value is invalid. It exceeds 31." 
check_data_validity = False
Exit function
End If
If yyyyPart < 2000 Then
reporter.ReportEventmicFail, "Format Check" , "The year value is invalid. (Prior to 2000)" 
check_data_validity = False
Exit function
End If
check_data_validity = True
End Function

In UFT editor pane, function will look like the given below figure. Save the function by clicking on save button. After saving, just check, if any error comes, if “0 Error” then proceed further.

UFT editor pane

After saving the function library, close it by clicking on File > Close.

Include the Function Library with the UFT Test Application

Above we have created a function library for date MM/DD/YYYY. Here, we will include that function library with the test application.

First, in UFT window, open the Mercury Tours test and making sure that only Web Add-in is loaded.

Next, click down arrow of “Open” button to select “Open Solution. The Open Solution dialog box opens.

From Open Solution dialog box, open the Mercury Tours1.ftsln file, created earlier in the ”Example of Action”.

Second, add the “Mercury Tours_CheckDateFunction.qfl”function library with the Mercury Tours test.

To do so, open the Solution Explorer window, if it is not opened by going to View > Solution Explorer or Solution Explorer button. In the Solution Explorer window, right-click the Mercury Tours test and select Add > Associate Function Library.

Add Associate Function Library In UFT

The “Open Function Library” dialog box opens. Open the “Mercury Tours_CheckDateFunction.qfl” function library. After selecting the function library when we click on “Open” the “Automatic Relative Path Conversion”message box opens, click “Yes”. This converts the path to a relative path.

Now, function library added with the Mercury Tours test. We can see the added function library below in the Solution Explorer window, as a child of the Mercury Tours test node.

Added Associate Function Library In UFT

Third, save the test by clicking on “Save” button.


⇓ Subscribe Us ⇓


If you are not regular reader of this website then highly recommends you to Sign up for our free email newsletter!! Sign up just providing your email address below:


 

Check email in your inbox for confirmation to get latest updates Software Testing for free.


  Happy Testing!!!
 

2 thoughts on “UFT Functions and Function Libraries – UFT/QTP Training Tutorial 9”

  1. I know this is old, but I was wondering if anyone knows how to associate more than one function library at a time?
    Moved functions, and have 8 for every one of 177 tests…a lot of boring work ahead:)

    Reply

Leave a Comment

Share This Post