Вы находитесь на странице: 1из 6

Using Library files in QTP

Introduction:

This document demonstrates on who to use Library files in QTP

Library files:

Library file is nothing but a VB script file containing code in VB Script format. Library
files in QTP are used to declare functions that are needed to be used across
actions. There are two ways to load a Library file in to your test:

1. Specify the Library file in the settings of the script

Create the VB Script file that you need and save to your PC

In QTP go to Test->Settings… Menu as shown in the image below

Go to the Resources tab of the Test Settings window and Click on “+” button. A
new line in the list box will appear
Click on “…” button to choose the location of the file
If you specify 2 or more library having a function with same name then QTP will
execute the one that comes first i.e. by looking at the order you specify the files.
There is no need to specify an order for dependent library file Ex- if file2 uses
functions defined in file1 then it is not necessary to specify file1 above file2 any
order will work fine.

Click on the “Check Syntax” button to verify that the script is a valid one
The VB script file has been associated with your test and functions it can be used
in all the actions present in the script. If you click on the “Set as Default” button
then all the library files specified in your current test would be automatically
added to the new tests that you create from now on.

Variables, classes, functions etc… declared in the Library files are accessible to all
the Actions present with in the test. Specifying library files using this method
makes it impossible to restrict the use of the Library to specific actions.

2. Loading the library file at run-time

Instead of specifying the library file in test settings we can load it at run-time by
using the execute file function.

ExecuteFile “C:\Documents and


Settings\tarun_lalwani\Desktop\Demo.vbs”

This would load all the variables, classes, structures etc… declared in the Library
but they would be accessible on in the action that has called the ExecuteFile
function i.e. if Action1 loads a library file using ExecuteFile then everything
present in the library file would only be accessible to Action1 and not any other
action.

To Load a library file present in Test Director the path can be specified
as

“[Quality Center] Subject\PathInTD”

Once a Library file is loaded using ExecuteFile it can’t be unloaded. It is unloaded


automatically when the action ends.

The below table gives advantages of both the methods


Specifying Library File in Test Loading Library File at Run-Time
Setting
For declaring Global Variable, Functions, For restricting functions of the library to
classes etc… specific Actions
For loading libraries depending upon
Add all global variables to a VB Script file conditions at run-time. Ex-
and specify it in settings.
If Condition1 then

ExecuteFile “C:\File1.vbs”

Else

ExecuteFile “C:\File2.vbs”

End If

Reference:
1. “Mercury QuickTest Professional, User’s Guide, Version
8.0.1”

3.

Dim qtApp 'As QuickTest.Application ' Declare the Application object variable

Dim qtLibraries 'As QuickTest.TestLibraries ' Declare a test's libraries collection


variable

Dim lngPosition

' Open QuickTest

Set qtApp = CreateObject("QuickTest.Application") ' Create the Application object

qtApp.Launch ' Launch QuickTest

qtApp.Visible = True ' Set QuickTest to be visible

' Open a test and get its libraries collection

qtApp.Open "C:\Test1", False, False ' Open a test


Set qtLibraries = qtApp.Test.Settings.Resources.Libraries ' Get the libraries collection
object

' Add Utilities.vbs if it's not in the collection

If qtLibraries.Find("C:\sai1.vbs") = -1 Then ' If the library cannot be found in the


collection

qtLibraries.Add "C:\sai1.vbs", 1 ' Add the library to the collection

End If

4.

Dim qtApp
Dim qtLibraries

Set qtApp = CreateObject("QuickTest.Application")


Set qtLibraries = qtApp.Test.Settings.Resources.Libraries ' Get the libraries collection
object

If qtLibraries.Find("C:sai1.vbs") = -1 Then ' If the library cannot be found in the


collection
ExecuteFile "C:sai1.vbs"
End If

Call test1()

Function In File Sai1.vbs


Function test1()
MsgBox "Hi Manish"
End Function

5. The simplest way out is ......

Store the Library file along with the entire path in a variable.

EG: sPath = "D:/Files/Functions.vbs"


Then just write ......
Executfile (sPath)

OR
Just write ...........
Executfile ("D:/Files/Functions.vbs")
Below this call which ever function you want to call from the library file
Call FunctName(argument list)

Вам также может понравиться