Relevant Codes

A Test Development Resource for HP QuickTest Professional.

QTP: Create Test Archives

by Anshoo Arora on August 27, 2009

Archives
Creative Commons License photo credit: Yme Bosma

This topic discusses archiving files created during a test session. These can be text files, XMLs, custom snapshots etc. The important bit is to keep them in one centralized test folder. At my current project, one of the core requirements of any automated test-suite is capturing snapshots wherever required by the user. Because each suite has several test scripts, we end up collecting hundreds of snapshots in each cycle. This is where archives come in – we create archives for each script, making it easier for the users to refer to the correct folder for the test and the time the test was run.

Assume you have a folder ‘Test XYZ’ is where you are storing all the runtime files and snapshots:

Z:\Automated Scripts\QuickTest Professional\Some Application\Image Captures\Test XYZ\

Using the technique below, you can create quick archives for each test session. I usually do this at the end of my tests, but this procedure can be called anywhere in the script.

Assume (again) you have the following file structure during a run-session:

Before Archiving

Before Archiving

After running the following line of code:

'C:\Demo is the parent test folder
Call CreateTestArchive("C:\Demo")

You will see a neat archive folder with the correct timestamp:

After Archiving

After Archiving

The contents of the newly created folder (005) will now carry all the files that were in the parent folder:

Archive Folder- Contents

Archive Folder- Contents

How it’s done:

'-------------------------------------------
' Name: function CreateTestArchive
' 
' Purpose: Creates an archive folder with a date-time stamp
'
' Input(s):
'      sPath - Path of the parent folder where all the files are located
'
' Output:
'      ""
'-------------------------------------------
function CreateTestArchive(sPath)
'-------------------------------------------
	Dim oFSO, oFolder, colSubFolder, sFolder
 
	'FileSystemObject
	Set oFSO = CreateObject("Scripting.FileSystemObject")
	'Bind to the supplied path
	Set oFolder = oFSO.GetFolder(sPath)
	'Retrieve collection of all subfolders
	Set colSubFolder = oFolder.SubFolders
 
	'Create numbering for the new folder
	Select Case Len(CStr(colSubFolder.Count))
		Case 1: sFolder = "00"
		Case 2: sFolder = "0"
		Case Else: sFolder = ""
	End Select
 
	'New folder name: this part can be modified so naming convention complies
	' 	to your guidelines.
	'Example: 009 - Archive Aug, 09 2009 3.05.02 PM
	sFolder = sFolder & colSubFolder.Count + 1 & " - Archive " & _
		  ParseDate(Date, "MMM, dd yyyy") & " " & Replace(Time, ":", ".")
 
      'Move all files to the new folder
	If oFolder.Files.Count > 0 Then
		If Not InStr(Len(sPath), sPath, "\") <> 0 Then sPath = sPath & "\"
		'Create the new folder
		oFSO.CreateFolder sPath & sFolder
		oFSO.MoveFile sPath & "*.*", sPath & sFolder
	End If
 
	Set oFSO = Nothing
	Set oFolder = Nothing
	Set colSubFolder = Nothing
end function

The snippet below parses the date to a desired format. I am using MM, dd yyyy, but you can modify this to support any format, or multiple formats:

'-------------------------------------------
' Name: function ParseDate
'
' Purpose: Uses DOTNetFactory to parse date in desired format
'
' Input(s):
'      dtDate: Date that is to be parsed
'      sFormat: Format in which the date is to be parsed
'
' Output:
'      Date
'-------------------------------------------
function ParseDate(dtDate, sFormat)
'-------------------------------------------
	Dim oInstance
 
	Set oInstance = DotNetFactory.CreateInstance("System.DateTime")
	ParseDate = oInstance.Parse(CStr(dtDate)).ToString(sFormat)
 
	Set oInstance = Nothing
end function

I hope you will like this simple approach.

Thanks for visiting Relevant Codes :)

If you have any questions, please ask them in the comments section. If your query is confidential, please use the Contact Form to send me an e-mail instead.

Leave a Comment

Previous post:

Next post: