Header Ads

How to Add Multiple Libraries with UFT Dynamically

In my previous post here-http://www.mydigitalvoice.in/2017/05/how-to-work-with-automation-object.html , I have discussed, how to add library files dynamically. But that was valid for only one library file.
Refer the below code under Library Settings


dim qtlibs
qtlibs=qtApp.Test.Settings.Resolurces.libraries
'to remove all
qtLibs.removeAll
Add a library:
If qtLibs.find("path of the lib"\abc.vbs")=-1 then
qtlibs.Add "path of the lib"\abc.vbs",1
End If
qtApp.Test.Save ' in this case you have to open the test in edit mode,readonly false
But there two ways to do that:
1. Execute File <path of the vbs/qfl file>
You have to write these lines for all library files one by one.
2. LoadFunctionLibrary <path of qfl/vbs> We can give many library files path with names separated by comma(,). I could have opted one of these two methods, but one issue compelled me to think otherwise. The issue was, there could be multiple libraries as and when project grows up. So every time,if a person going to add a new function library, Somebody needs to update the above lines in code just to load the files.That triggered the cost of maintenance during function library addition. Below is a way I have coded to handle addition of all libraries with our script dynamically using AOM way : The precondition is to have a folder called Libraries where we need to place all libraries.
Dim qtApp

Set qtApp=CreateObject("QuickTest.Application")

Set objFso=CreateObject("Scripting.FileSystemObject")
objLibPath=path of our Library folder ' Like C:\Test\Library\
Set objFolder=objFso.GetFolder(objLibPath)
Set colFiles=objFolder.Files
qtApp.Launch  ' launch QTP/UFT 

Set objLib=qtApp.Test.Settings.Resources.Libraries
For Each objFile in colFiles
fileName=objFile.Name
If Not instr(fileName,".lck") Then ' sometimes I have seen .lck files are 
'created and not destroyed even if the QTP is closed
   if objLib.Find(objLibPath&fileName)=-1 Then
      objLib.Add objLibPath&fileName
   End If
Else
objFile.Delete 'Delete the .lck file
End If
Next
Powered by Blogger.