QC & QTP – How to Attach a File to a Test Sets Attachment Tab
Do you need your test scripts to save test files in QC? Or do you may have a file that contains test data that you want your scripts to read from? Yesterday I received an email form my blog friend Kumar that asked how this can be done using Quality Center and QTP. Hopefully this post will explain how this functionality can be achieved. You also might want to check out How to use QTP to save and retrieve a Quality Center Test Resource
Steps to attach a file in QC
1. In QTP Create a new test named TestLabAttachment and save in a folder in Quality Center:
2. In QC add the QTP script ‘TestLabAttachment’ into a test set in the QC ‘Test Lab” section
3. Create a local text file on your QTP machine that you want to add to the QC ‘Attachments’ tab. For example:
3. In QTP add the following lines of code:
Set nowTest = QCUtil.CurrentTestSet
Set attachmentPath = nowTest.Attachments
Set nowAttachment = attachmentPath.AddItem(Null)
‘Replace with the path to your file:
nowAttachment.FileName = “C:\QTPAUTOMATION\DATA\testenv.txt”
nowAttachment.Type = 1
4. Run QTP script. In the Run dialog select ‘Results Location’ tab and click on ‘New run Results in QC project”. Select the Test Set that the current script is saved in.
5. After the QTP script runs go back to QC and look under the test set’s ‘Attachments’ tab. The file should appear:
Steps to retrieve file from the QC Test Set ‘Attachments’ tab
- Open QTP
- In the same script that you created above TestLabAttachment enter the following code (“I admit this code is ugly – but it works. If you know a better way please let me know”)
qcAttachmentName = "yourfilename" 'For example testenv.txt localPathToCopyToOrFrom = "the path to save the QC file to" ' For example C:\OTA Set nowTest = QCUtil.CurrentTestSet Set otaAttachmentFactory = nowTest.Attachments Set attachList = otaAttachmentFactory.NewList ("") IF attachList.Count > 0 Then For i = 1 to attachList.Count Set attachObj = attachList.Item(i) tempPath = attachObj.FileName 'Get temp file path and create array to hold elements arrayPath = split(tempPath,"\") eCount = Ubound(arraypath) 'We use the last element to grab the file name we will use in target folder tempName = arrayPath(eCount) 'Check to see if file name exist in QC If instr(1,tempPath, qcAttachmentName) Then attachObj.Load True,"" Set objFSO = CreateObject("Scripting.FileSystemObject") localPath = localPathToCopyToOrFrom &"\" & tempName 'Check to see if the file exist -- if it does delete it before copying new file if objFSO.FileExists(localPath) then objFSO.DeleteFile(localPath) end if objFSO.MoveFile tempPath,localPath 'Check to see if a file with the same name exist in the target folder. 'If it does delete it before renaming localPathToCopyToOrFrom = localPathToCopyToOrFrom &"\" & qcAttachmentName if objFSO.FileExists(localPathToCopyToOrFrom)then objFSO.DeleteFile(localPathToCopyToOrFrom end if objFSO.MoveFile localPath,localPathToCopyToOrFrom exit for end If Next ELSE msgbox "File not found" End IF '######################### ' CLEAN UP '######################### Set nowTestSet = Nothing Set otaAttachmentFactory = Nothing
- Run QTP script. In the Run dialog select ‘Results Location’ tab and click on ‘New run Results in QC project”. Select the Test Set that the current script is saved in.
4. After the QTP script runs go the folder where you saved your file. The QC file should now be in it.
Also be sure to check out Application Lifecycle Management for the Whole Team