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.
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
nowAttachment.Post()
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")
- 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.
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

4. After the QTP script runs go the folder where you saved your file. The QC file should now be in it.

{ 16 comments… read them below or add one }
Nice work Joe. Thank you again.
Thanks, this worked fine. The problem i am having is how do i skip this function when i am running a test locally vs through quality center. I need to confirm a QC connection and skip this when it’s not connected (running locally) .
greg woffindin » Hi Greg – sorry for my late response. If you have not figured this out already you could use something like this:
‘This is if the test is truly local meaning the QTP script is not stored in QC
qcEnabled = QCUtil.IsConnected
if qcEnabled = “True” then
yourcode QC attachement code
end if
If the script is stored in QC and you want to run the script from QTP not QC you need to store the script in a test lab. When you click run in the ‘Results Location” tab point to the scripts test lab in the ‘new run results in QC’ ‘Test Set’ option.
Hope this helps you.
Cheers~Joe
Thanks, I figured out what i needed. Actually what i have is first a check to see if an attachment is stored in QC in the test lab section, if not it then checks for a copy of the attachment on the pc running the script and then if it can’t find it uses a default value. I was loading some environment variables and wanted to allow someone running the script via QC to easily change them and have a way to run the scripts on a local pc for creating/troubleshooting and be able to change them.
Greg Woffindin » Awesome – thanks for the update Greg!
Hello Joe,
You have an excellent site with tons of resources. I am glad to say I have been a beneficiary of this site since the time I found. Many Thanks for maintaing such a wonderful site.
Have a question about creating shortcut keys for menu items of an AUT. We have a custom written .Net application which doesnt lend itself easily to the recording feature of QTP(11.0), as it appears all the objects are private. Someone suggested the only way of accessing the Menu Items would be to custom map shortcut keys and then navigate to the objects on the pages opened.
Wondering if you ran into a similar situation and considered taking this approach. would appreciate your thoughts and guidance on how this may be achieved.
Regards,
Murali » Hi Murali – thanks! I’m glad that you find the site helpful! I have seen instances when I do need to use either SendKeys or a Windows type function to access a AUT functionality. I would give either method a try and see how reliable it works for your app. Also I would ask a developer if there was a way to make the menu more automation friendly. Either by using standard controls or exposing a method the help QTP recognize it. Hope this helps. Cheers~Joe
Joe,
Can you help me in explaining how we can able to attach a file to the current step at runtime using QTP? We have a requirement to upload a snapshot for each of the step that has been executed using automated script. We were able to upload a file or snapshot to current test run, but not to step. Appreciate your help.
thanks
Venkat
Venkat » Hi Venkat I haven’t had time to try to write to a specific test step in a test. BUt does the QTP>Options>Run>Screen capture set to “always” options capture enough info to meet your requirements?
Hey, it is set as always but still the screenshot is not attached fr each step.
Actually, i have used a report fail fn and within that upload function exist. I’ve used the below fn
Public Function UploadScreenshotQC(ScrnshotFileName)
Dim Test_CurrentRun,Test_Attachment,Test_AttachItem’,Test_AttachStore
Dim Local_Path,Local_FileName
Set Test_CurrentRun=QCUtil.CurrentRun
‘Check that the test is running from QC, otherwise it will exit
If (Test_CurrentRun Is Nothing) Then
Exit Function
End If
‘Attach the File to the current test
Set Test_Attachment=Test_CurrentRun.Attachments
Set Test_AttachItem=Test_Attachment.AddItem(Null)
Test_AttachItem.FileName=ScrnshotFileName
Test_AttachItem.Type=1
Test_AttachItem.Post
Test_AttachItem.Refresh
End Function
Very Helpful , Thank you
Joe, Is there any way to provide a link to the attachments in that particular test set inside the QC Report? This would help the person looking into the QC report as they can directly click on the link to go to the uploaded attachments
Can I use above code (attachment to QC) with others tools as well like Testing anywhere as for those tools we are not storing scripts in QC only uploading results .
Abhijit Mahajan » You should be able to upload text file usingg this vbscript regardless of what test tool created it.
Using QCUtil it is possible to add screenshot to current test, current run, test set level. But is it posible to add the screensot in test step level….Pls advice
I have results from a SOAPUI Pro script that I would like to attach to the Test Lab Test Suite, but I can only see how to do it one at a time. Is there a way to attach multiple files at once?