Blog to understand automation concepts in QTP, Selenium Webdriver and Manual Testing concepts

How to load and Associate Function libraries in QTP Test at runtime

Below are the various ways by which we can add/associate libraries with QTP tests so that functions in the libraries can be used within test.

ExecuteFile Statement 

      Executes the VBScript statements in the specified file. Functions, subroutines defined in the file are available from the global scope of the action's script.We cannot debug functions or statement contained in the file.

ExecuteFile "C:\BusinessLib.vbs”

Through QTP Interface 

      A library can be associated with a test so that the functions and subroutines, classes defined in the function library can be used .This can be achieved as follows:
      Navigate to File > Settings > Resources > Associate Function Library’ option in QTP. Add the required Libraries to the resources by clicking on + icon.

      Once function library is associated with the test.We can debug function in the function libraries and view function definition of the functions from the scripts.We can also define the priority of function library using up and down icon.

     
Associating Libraries with QTP tests

     

Using Load Function Library method

In  QTP 11, and onwards, We can dynamically load function library with the test using load function library. Libraries are released from test once completed. We can debug into function on calling library using LoadFunctionLibrary. The scope of the library is local to the action in which it is called.

Syntax: LoadFunctionLibrary “D:\TestLib.vbs”


We can add multiple files seperated by


Using Automation Object Model 

We can associate multiple libraries to QTP test using the below code. Also all the tests actions can use the functions from associated libraries.

strFilesNameCommaSeperated = "d:\test1.vbs,d:\test.vbs"
strTestName ="D:\Sample\Test1"
Call LoadLibrariesThroughAOM(strFilesNameCommaSeperated,strTestName)
Public Function LoadLibrariesThroughAOM(strFilesNameCommaSeperated,strTestName)
Set oqtapp = CreateObject("QuickTest.Application")
oqtapp.Launch
oqtapp.Visible = True
 strLibName = split(strFilesNameCommaSeperated,",")
'Open a test and associate a function library to the test
oqtapp.Open strTestName, False, False
Set oLib = oqtapp.Test.Settings.Resources.Libraries
 For i = 0 to ubound(strLibName)
                 LibName = strLibName(i)
                 ' If library is not already added, add the required library.
 
                If oLib.Find(LibName) = -1 Then
                         oLib.Add LibName, 1
                End If
 Next
oqtapp.Test.Save
                End Function


Understanding QTP Automation Object Model

QTP AOM (Automation Object Model) can be used to automate QTP Operations. Using the objects, methods, and properties exposed by the Quick Test automation object model,we can execute script, configure options settings,



Creating the QTP Object


Below lines of codes creates an instance of QTP,launches QTP and makes it visible.
Dim qtApp
Set qtApp = CreateObject("QuickTest.Application")
qtApp.Launch 'Start QTP
qtApp.Visible = True

If we want to execute on a remote computer, we use

Set qtApp = CreateObject("QuickTest.Application",”server”)

If we want to connect to QC from QTP AOM

 qtApp.TDConnection.connect “QC URL”,”Domain”,”Project”,”User”,”Pwd”


Working With QTP Tests using AOM


Open a new Test

Dim qtApp
Set qtApp = CreateObject("QuickTest.Application")
qtApp.Launch 'Start QTP
qtApp.Visible = True
qtApp.New

Opening an Existing Test

qtApp.Open “Test Path”,”True” ‘ Opens in read only mode.

Running a test through AOM

qtApp.Test.Run

Saving a test in QTP

If qtApp.Test.IsNew Then 
        qtApp.Test.SaveAs "C:\QTP\testqtp"
Else
        qtApp.Test.Save
End If

Generating script for QTP Settings and QTP Options for AOM


Set qtResultsOpt = CreateObject("QuickTest.RunResultsOptions") ' Create the Run Results Options object
qtResultsOpt.ResultsLocation =
"C:\Tests\Test1\Res1" ' Set the results location

qtTest.Run qtResultsOpt
Set qtResultsOpt = CreateObject("QuickTest.RunResultsOptions") ' Create the Run Results Options object
qtResultsOpt.ResultsLocation =
"C:\Tests\Test1\Res1" ' Set the results location

qtTest.Run qtResultsOpt

Defining Settings in QTP

To know, how settings are defined and QTP object is launched, and settings are defined using AOM, Go to File>Settings>Properties and click on Generate Script. This will generate a script with all the default setting parameters

Default Setting script


The Content of the script will be as follows:

Dim App 'As Application
Set App = CreateObject("QuickTest.Application")
App.Launch
App.Visible = True
App.Test.Settings.Launchers("Web").Active = False
App.Test.Settings.Launchers("Web").Browser = "IE"
App.Test.Settings.Launchers("Web").Address = "http://www.rediff.com"
App.Test.Settings.Launchers("Web").CloseOnExit = True
App.Test.Settings.Launchers("Windows Applications").Active = True
App.Test.Settings.Launchers("Windows Applications").Applications.RemoveAll
App.Test.Settings.Launchers("Windows Applications").RecordOnQTDescendants = True
App.Test.Settings.Launchers("Windows Applications").RecordOnExplorerDescendants = False
App.Test.Settings.Launchers("Windows Applications").RecordOnSpecifiedApplications = True
App.Test.Settings.Run.IterationMode = "rngAll"
App.Test.Settings.Run.StartIteration = 1
App.Test.Settings.Run.EndIteration = 1
App.Test.Settings.Run.ObjectSyncTimeOut = 20000
App.Test.Settings.Run.DisableSmartIdentification = False
App.Test.Settings.Run.OnError = "Dialog"
App.Test.Settings.Resources.DataTablePath = "<Default>"
App.Test.Settings.Resources.Libraries.RemoveAll
App.Test.Settings.Web.BrowserNavigationTimeout = 60000
App.Test.Settings.Web.ActiveScreenAccess.UserName = ""
App.Test.Settings.Web.ActiveScreenAccess.Password = ""


Defining Options in QTP


To know the current options settings in QTP, we can generate a script similar to Settings as defined above through Tools>Options>General and clicking on generate scripts.


















Dim App 'As Application
Set App = CreateObject("QuickTest.Application")
App.Launch
App.Visible = True
App.Options.DisableVORecognition = False
App.Options.AutoGenerateWith = False
App.Options.WithGenerationLevel = 2
App.Options.TimeToActivateWinAfterPoint = 500
App.Options.SaveLoadAndMonitorData = True
App.Options.Run.RunMode = "Fast"
App.Options.Run.ViewResults = True
App.Options.Run.StepExecutionDelay = 0
App.Options.Run.MovieCaptureForTestResults = "Never"
App.Options.Web.AddToPageLoadTime = 10
App.Options.Web.RecordCoordinates = False
App.Options.Web.RecordMouseDownAndUpAsClick = False
App.Options.Web.RecordAllNavigations = False
App.Options.Web.RecordByWinMouseEvents = ""
App.Options.Web.BrowserCleanup = False
App.Options.Web.RunOnlyClick = False
App.Options.Web.RunMouseByEvents = True
App.Options.Web.RunUsingSourceIndex = True
App.Options.Web.EnableBrowserResize = True
App.Options.Web.PageCreationMode = "URL"
App.Options.Web.CreatePageUsingUserData = "Get Post"
App.Options.Web.CreatePageUsingNonUserData = ""
App.Options.Web.CreatePageUsingAdditionalInfo = True
App.Options.Web.FrameCreationMode = "URL"
App.Options.Web.CreateFrameUsingUserData = "Get Post"
App.Options.Web.CreateFrameUsingNonUserData = ""
App.Options.Web.CreateFrameUsingAdditionalInfo = True
App.Options.WindowsApps.AttachedTextRadius = 35
App.Options.WindowsApps.AttachedTextArea = "TopLeft"
App.Options.WindowsApps.ExpandMenuToRetrieveProperties = True
App.Options.WindowsApps.NonUniqueListItemRecordMode = "ByName"
App.Options.WindowsApps.RecordOwnerDrawnButtonAs = "PushButtons"
App.Options.WindowsApps.ForceEnumChildWindows = 0
App.Options.WindowsApps.ClickEditBeforeSetText = 0
App.Options.WindowsApps.VerifyMenuInitEvent = 0
App.Options.TextRecognitionOrder = "APIThenOCR"
App.Folders.RemoveAll


How to work with Dictionaries using scripting.dictionary object in QTP

Dictionary Object stores data key, item pairs. Each item is associated with a unique key.The key is used to retrieve an individual item and is usually a integer or a string, but can be anything except an array.

Creating a dictionary object:

We can create an instance of dictionary object as shown below:
Set odict = CreateObject("Scripting.Dictionary")
odict.add "country", "India"
odict.add "Capital", "Delhi"  

 
We have created a dictionary object odict, and added 2 key item collection in the dictionary object.
 

Below are the methods of dictionary object:


1.  Add Method - Adds a key and item pair to a Dictionary object.

odict.add "Currency", "Rupee" 


2.  Exists Method - Returns true if a specified key exists in the Dictionary object, false if it does not.

odict.exists(“country”) will return true  for dictionary defined in above examples.



3.  Items Method - Returns an array containing all the items in a Dictionary object.aite = odict.items(“country”)



4.  Keys Method - Returns an array containing all existing keys in a Dictionary object.aite = odict.keys(1)



5.  Remove Method - Removes a key, item pair from a Dictionary object.odict.remove("Currency")



6. RemoveAll Method - The RemoveAll method removes all key, item pairs from a Dictionary object.odict.removeAll()


Properties of Dictionary object

1. Count Property - Returns the number of items in a collection or Dictionary object.Intcount = odict.count


2. Item Property - Sets or returns an item for a specified key in a Dictionary object. For collections, returns an item based on the specified key.ItemVal = odict.Item("Capital")   - gets value of the keyodict.Item("Capital") = “MUMBAI” – sets value of  the key.



3. Key Property - Sets a key in a Dictionary object.odict.Key("Capital") = "City"odict.item(“City”) will now give value of item stored in key Capital before. 


Comparing Dictionaries Vs Arrays


Below is the comparison between array and dictionary objects
  •  Dictionary  has methods to add new items and check for existing items.
  •  On Deleting a particular item from a dictionary, all the subsequent items automatically shift up.
  • You use keys to identify dictionary items. Keys can be any data type while arrays have numeric values as key
  •  A dictionary can't be multidimensional while array can be.
  •  It is much easier to find an item value based on keys as compared to arrays.
  •  Keys in Dictionary can have unique values only
  •  Dictionary can be used as Global variable declaration. so that any test can access the values from it in the run time. Will discuss this in detail in a separate post.