QC & QTP – How to Attach a File to a Test Sets Attachment Tab

by Joe Colantonio on August 19, 2011

Post image for 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.

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

  1. Open QTP
  2. 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")
  3. 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. 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.

{ 16 comments… read them below or add one }

Kumar S August 22, 2011 at 3:01 pm

Nice work Joe. Thank you again.

Reply

greg woffindin October 4, 2011 at 5:29 pm

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) .

Reply

Joe Colantonio October 20, 2011 at 11:39 am

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

Reply

Greg Woffindin January 15, 2012 at 3:48 pm

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.

Reply

Joe Colantonio January 15, 2012 at 4:03 pm

Greg Woffindin » Awesome – thanks for the update Greg!

Reply

Murali January 20, 2012 at 2:30 pm

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,

Reply

Joe Colantonio January 27, 2012 at 11:51 am

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

Reply

Venkat April 9, 2012 at 6:25 pm

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

Reply

Joe Colantonio April 20, 2012 at 6:59 pm

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?

Reply

Abhishek February 26, 2013 at 10:12 am

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

Reply

Poornima September 11, 2012 at 10:24 am

Very Helpful , Thank you :)

Reply

Poornima September 11, 2012 at 10:28 am

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

Reply

Abhijit Mahajan October 26, 2012 at 7:59 am

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 .

Reply

Joe Colantonio November 2, 2012 at 1:18 am

Abhijit Mahajan » You should be able to upload text file usingg this vbscript regardless of what test tool created it.

Reply

Abhishek February 27, 2013 at 9:33 am

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

Reply

Laura April 18, 2013 at 3:44 pm

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?

Reply

Leave a Comment

Previous post:

Next post: