Download all attachments from QC test plan tab to local system

Source Location: Folder Structure of the Test Plan Tree , seprated by forward slash
Target Location: Local System Path , where files are to be downloaded '
'Download Sub: if Sub folder are to be downloaded then Set this argument as True , Other wise False


Public objChild
Public objNestedChild(100)
Public FolderLocation(100)


'#############################################################################################################
'@ Status: Packed
'@ Purpose : Download all attachments from the Test Plan Tab of QC
'@ Input : Source Location : Folder Structure of the Test Plan Tree , seprated by forward slash
' : Target Location : Local System Path , where files are to be downloaded '
' : Download Sub : if Sub folder are to be downloaded then Set this argument as True , Other wise False
'@Output : Folder Hierarchy created and atachments downloaded on the Local System
'@ Precondition : QTP and QC should be connected
'@ Limitations : It Cannot Start downloading from the Root Folder
' : Folder Hierarchy is limited to 100
' : It will not download the Script Files
''#############################################################################################################
'@ Description: This Function will download Attachments from QC Test Plan
Public Function gen_DownloadAttachmentsFrom_QCTestPlan(SourceLocation,TargetLocation,DownloadSub)

Dim objTD,objTM ,objRoot
Dim Root

'@ QC Connection object
Set objTD=TDUtil.QCConnection

'@ Check for the connection
If not QCUtil.isconnected Then
gen_DownloadAttachmentsFrom_QCTestPlan="QTP is not Connected with QC"
Exit Function
End If

'@ QC Tree Manager Object
Set objTM=objTD.TreeManager

'@ Create Target Location ' if not present
Chk_TargetLocation(TargetLocation)

'@ Download files based upon the location
If instr(1,SourceLocation,"\",1)<>0 Then

Dim FolderNo
Dim splSL ' split the source location
Dim ChildIndex:ChildIndex=0

splSL=split(SourceLocation,"\",-1,1)

For FolderNo=1 to ubound(splSL)+1

'@ Check for the root folder
If FolderNo=1 Then
'@ check if the root exists
If not IsRootFolderExists(objTM,splSL(FolderNo-1)) Then
gen_DownloadAttachmentsFrom_QCTestPlan="Root Folder Not found"
Exit Function
end If
else
'@ Call the First Child functions
If FolderNo=2 Then
On error Resume Next
Set objChild=GetRootChildObj(ObjTM,splSL(FolderNo-2),splSL(FolderNo-1),ChildIndex)
On Error GoTo 0
'@ Check for the error
If isobject(objChild)="False" Then
gen_DownloadAttachmentsFrom_QCTestPlan="First Child Not found"
Exit Function
end If
else
'@ Call the Nested Child functions
If FolderNo > 2 Then
On error Resume Next
Set objChild=GetChildObj(objChild,splSL(FolderNo-1),ChildIndex)
'@Check for the error
If err.number=-2147220481 Then
Exit Function 'There are no more childs
end If
On Error GoTo 0
end If
end If
end if
Next

else
'@ check if the root exists
If not IsRootFolderExists(objTM,SourceLocation) Then
gen_DownloadAttachmentsFrom_QCTestPlan="Root Folder Not found"
end if
end If

'@ Download files of the curent folder
DownloadFiles_QC2LocalSystem objChild.child(ChildIndex),TargetLocation

'@ Download All Nested folders and there files
If DownloadSub Then
DownloadAllChilds objChild,ChildIndex,TargetLocation,0
end If

End Function
''#############################################################################################################



''#############################################################################################################
'@ Status: Packed
'@ Purpose : 'Download all the child folders with there attachments
'@ Input : Parent Obj : Object s of the folder from where files are to be downloaded
' : ChildIndex : child index of the folder to be downloaded
' : TargetLocation : Location of the Target
' : Index : used for the internal loop
'@Output : Files downloaded on the local system
''#############################################################################################################

'@ Description: Download all Nested childs and there files
Public Function DownloadAllChilds (ParentObj,ChildIndex,TargetLocation,Index)

'@ Loop Counter
' DownloadAllChildObjCounter=DownloadAllChildObjCounter+1

On error resume next
Set objNestedChild(Index)=ParentObj.child(ChildIndex)
If err.number=-2147220481 Then
Exit Function 'There are no more childs
end If
On error goto 0

Dim ChildCounter

'@ Iterate for the first childs
For ChildCounter=1 to objNestedChild(Index).Count
If isobject(objNestedChild(Index)) Then
Chk_Create_Target objNestedChild(Index),ChildCounter,TargetLocation 'create the target location and download the files
FolderLocation(Index)=TargetLocation & "\" & objNestedChild(Index).child(ChildCounter).name ' change the folder location
Dim NewIndex
NewIndex=Index+1 ' reset the index for every first child loop
DownloadAllChilds objNestedChild(Index),ChildCounter,FolderLocation(Index),NewIndex ' Recursive call for the nested childs
end if
Next
End Function

''#############################################################################################################



''#############################################################################################################
'@ Status: Packed
'@ Purpose : This Function will check,create and Adjust the Target Folder Location
'@ Input : ObjChild : Child Object from where files are to be downloaded
' : ChildIndex : child index of the folder to be downloaded
' : TargetLocation : Location of the Target
'@Output : Folder created on the local system
''#############################################################################################################
Public Function Chk_Create_Target (objChild,ChildIndex,TargetLocation)

Dim objfolder,fso

'@ Create fso
Set fso=CreateObject("Scripting.FileSystemObject")

'@ set the next folder object
On error resume next
Set objfolder=objChild.child(ChildIndex)
If err.number=-2147220481 Then
Exit Function 'There are no more childs
End If
On error goto 0

'@ Change the target location and Create the folders
Dim var,strTargetLocation
strTargetLocation =TargetLocation & "\" & objChild.child(ChildIndex).name
CreateFoldr strTargetLocation

'@ Download the current folder files
DownloadFiles_QC2LocalSystem objfolder,strTargetLocation
End Function

''#############################################################################################################


''#############################################################################################################
'@ Status: Packed
'@ Purpose: Download the files from QC to Local System
'@ Input : ObjFolder : Object of the folder from where files are to be downloaded
' : DownloadLocation : Location on the Local System from where files are to be downloaded
'@Output : Downloaded files on the system , if exists
''#############################################################################################################
Public Function DownloadFiles_QC2LocalSystem(objfolder,DownloadLocation)

Dim fold,objAttach,objFileList,FileItem,ObjAttachStor,AttachName,fso

' Set fold=objfolder
' Set objAttach = fold.Attachments
' Set objFileList = objAttach.NewList("")
Set objFileList = objfolder.Attachments.NewList("")

'@ Create fso
Set fso =CreateObject("Scripting.FileSystemObject")

'@ Create folders if required and download files
If objFileList.count<>0 Then
For Each FileItem In objFileList
'Download file
Set ObjAttachStor = FileItem.AttachmentStorage
AttachName = FileItem.DirectLink
ObjAttachStor.Load AttachName, True
CreateFoldr DownloadLocation
fso.CopyFile ObjAttachStor.ClientPath& "\" & AttachName, DownloadLocation & "\",True

' Delete file if already existing
Dim Rename,FileLoc
Rename=Right(AttachName,(len(AttachName)-15))
FileLoc=DownloadLocation & "\" &Rename
If fso.FileExists(FileLoc)Then
fso.deletefile FileLoc,True
end If

'Rename the new file
Dim f
Set f= fso.Getfile(DownloadLocation & "\" & AttachName)
f.name=Rename
Set f=Nothing
Next
else
CreateFoldr DownloadLocation
end if

'@ Release object
Set fso=Nothing

End Function
''#############################################################################################################



''#############################################################################################################
'@ Status: Packed
'@ Purpose: Get Child Object of the parent folder
'@ Input : Parent obj: Parent Object whose child is to be find
' : ChildName: Name of the child whose object is required
' : Child Index : Index of the child
'Output : : Child object if Exists
' : False if Child folder doesn't Exists
' : Child Index of the child
''#############################################################################################################
Public Function GetChildObj(ParentObj,ChildName,byref ChildIndex)
Dim objCRoot,ChildCounter
Set objCRoot=parentobj.child(ChildIndex)
For ChildCounter=1 to objCRoot.Count
If lcase(objCRoot.child(ChildCounter).name)=lcase(ChildName) Then
ChildIndex=ChildCounter
Set GetChildObj=objCRoot
Exit Function
else
If ChildCounter = objCRoot.Count Then
GetChildObj=False
ChildIndex=0
Exit Function
end If
end If
Next
End Function
''#############################################################################################################


''#############################################################################################################
'@ Status: Packed
'@ Purpose: Get First Child Object of Root Folder
'@ Input : objTM : Tree Manager Object
' : RootName: Root Name
' : ChildName: Name of the child whose object is to be made
' : Child Index : Index of the child
'Output : : Child object if Root Folder Exists
' : False if Child folder doesn't Exists
' : Child Index of the child
''#############################################################################################################
'@ Packed
Public Function GetRootChildObj(objTM,RootName,ChildName,ByRef ChildIndex)

Dim objCRoot,ChildCounter

Set objCRoot=ObjTM.TreeRoot(RootName)

'@ Look for the child object
For ChildCounter=1 to objCRoot.Count
If lcase(objCRoot.child(ChildCounter).name)=lcase(ChildName) Then
ChildIndex=ChildCounter
Set GetRootChildObj=objCRoot
Exit Function
else
If ChildCounter = objCRoot.Count Then
GetRootChildObj=False
ChildIndex=0
Exit Function
end If
end If
Next

End Function
''#############################################################################################################




''#############################################################################################################
'@ Status: Packed
'@ Purpose: Check if the root folder exists
'@ Input : objTM : Tree Manager Object
' FolderName:Name of the Root Object
'Output : : True if Root Folder Exists
' : False if Root folder doesn't Exists
''#############################################################################################################

Public Function IsRootFolderExists(objTM,FolderName)

Dim RootCounter,objRoot, TDOLE_SUBJECT

'@ Root List Object
Set objRoot=objTM.RootList(TDOLE_SUBJECT)

'@ Look for the Root Folder
For RootCounter=1 to objRoot.Count
If objRoot.item(RootCounter)= trim(FolderName) Then
IsRootFolderExists=True
Exit Function
else
If RootCounter = objRoot.Count Then
IsRootFolderExists=False
Exit Function
end if
end If
Next

End Function
'#############################################################################################################


''#############################################################################################################
'@ Status: Packed
'@ Purpose : Create Folder if not Present
'@ Input : Download Location : Path of the folder to be downloaded
''#############################################################################################################
Public Function CreateFoldr(DownloadLocation)

Dim fsobj
'@ Create fso
Set fsobj=CreateObject("Scripting.FileSystemObject")
'@ check if the folder exists
If not fsobj.FolderExists(DownloadLocation) Then
fsobj.createfolder DownloadLocation ' if not then create
end If
'@ Releae objects
Set fsobj=Nothing
End Function



''#############################################################################################################
'@ Status: Packed
'@ Purpose : Check and create Initial Target Location
'@ Input : Target Location : Local System Path , where files are to be downloaded ''
'@Output : Folder Hierarchy created on the Local System
''#############################################################################################################
Public Function Chk_TargetLocation(TargetLocation)

Dim splTr,var,DownloadLocation

If instr(1,TargetLocation,":",1)<>0 Then

splTr=split(TargetLocation,"\",-1,1)

For var=0 to ubound(splTr)

DownloadLocation=DownloadLocation & splTr(var)

If var >0 then CreateFoldr DownloadLocation

If var<>ubound(splTr) Then
DownloadLocation=DownloadLocation &"\"
end If
Next
end If

End Function
'#############################################################################################################

Comments

Popular posts from this blog

Arrays

What is the difference between eval, execute and executeglobal.

Sample Test Strategy for a MicroService Project with APIs only