QTP or UFT and VBScript: How to upload file to QC or ALM ?
August 13, 2018
QTP or UFT and VBScript: How to upload a file to QC \ How to upload file to ALM ?
Welcome to this post!-“QTP or UFT and VBScript: How to upload file to QC or ALM ?”
If you are looking for latest HP UFT \ QTP interview Questions, then you are at right place. This post “How to upload file to QC \ How to upload file to ALM” consists of technical interview question which had been part of many interviews whether external or internal. Usually these are assumed to be known in depth to check the logical solvency efficiency of the candidate and of course suitability for the project and company. Go ahead and enjoy reading…
This discussion will help you prepare well if you are going for the interview or you need the function to be used in your project.
In the interview usually, you might be questioned in different way like:
- How to upload file of any type to QC ?
- How to upload the excel to ALM ?
- How to upload the resource to QC ?
- How can you save resource onto QC folder ?
- How to upload the script dependent file to QC?
- How to link a file to QC?
- How QTP or UFT uploads a file to QC \ ALM?, and so on.
Well, most of the time testers find it difficult while automating the scripts if the scripts or its resources are stored in QC or in turn the changed test data file needs to be uploaded to ALM\QC so that another script might pick up the latest file and start working on it. The scenarios can be many but what exactly is the common solution possible to it, this is what we are going to discuss.
Firstly, you need to understand that QC in itself is a complete module and has an object model too just like UFT or QTP has automation object model or AOM.
People tend to confuse themselves as they are unaware of this fact that QC or ALM has its built-in model on which it works, so to understand it better we will take the example of uploading the file to ALM but before discussing the complete code, we will visit the code in parts to understand what exactly is meant there.
Sound Good! right?
So, to begin with, always create a heading comment section whenever you are writing code, it’s a good practice and really matters when you are liable for the module or application under test in which you were working. The readers or peers should be able to concentrate or focus on the usage of function just by going through your comments. Let me show you a good way with example.
Consider the heading comments below:
'Function Name: Fn_UploadToALM 'Documentation: uploads any file with any resource type to the QC\ALM module 'Created By: Prachur Saxena
It looks fair enough for anyone reading the heading comments of the function as there is good information given here. But we can save more time of code readers by providing the input and output expected or valid values for this function. Don’t you think so, it will be really a good for another person to go through it easily?
Let’s revisit the same again,
'Function Name : Fn_UploadToALM 'Documentation : uploads any file with any resource type to the QC\ALM module 'Created By : Prachur Saxena 'Date : 13/1/2018 'Modified By: NA 'Revision Comments: NA 'Return Values: True or False 'Example: Fn_UploadToALM "test_data","test_data.xls","C:\Users\Public\Documents\TEST_DATA_Files","Data table"
Wow! now that’s look fantastic and quite impressive to understand for anyone to use the function in his code.
With this information he skips many steps like code debugging, asking the developer to understand the code and actually waste both the persons time, etc. The more you put input parameters examples the better it becomes as it gives much clarity on the working logic.
There are 3 additional comments which are shown here i.e. Date, Modified By, Revision Comments – these are a MUST and should be mentioned in the comments. The impact initially won’t occur to you if you are the developer of the function but will give you broader view if someone else has done that code and you are being asked to work on it. If This Sounds Often to you, then make a HABIT of doing it.
Here I believe our very first part of writing a good function heading with comments ends. Now lets focus on the code that we are targeting on the blog that is how to upload any file to QC or ALM.
Part 1 : Variables:
Dim ResCount : ResCount = 0 Dim iterator Dim ObjQC,CurrentResObj,ObjResFactory,ResObjList,oNewResObj
In the beginning of the code it is really a good practice to write all your variables. Here we have declared 4 variables
ResCount = for storing the count of resources which we receive from the collection of resources available in QC or ALM.
iterator = is just a variable for FOR loop,
CurrentResObj = points to each new resource which we get inside the FOR loop, while iterating through the resource collection.
And declare all other variables required for making connection and storing the resource object list or object.
Part 2 : Assignments
Set oNewResObj = Nothing Set ObjQC = QCUtil.QCConnection Set ObjResFactory = ObjQC.QCResourceFactory Set ResObjList =ObjResFactory.NewList("") ResCount = ResObjList.Count
In this section we are assigning the variables with values or creating the instances of the classes.
Firstly,
Set ObjQC = QCUtil.QCConnection } = > we are creating the object of connection to QC,
Now with the help of your QC connection object, get the instance of QCResourceFactory and store the list available within ‘ObjResFactory’ object in the variable ‘ResObjList’.
And Set oNewResObj variable to Nothing as we will use this later to store our final resource object
Finally get the count of all the available resources and store it in the variable declared earlier:
ResCount = ResObjList.Count
Part 3 : Execution
Now in this part, we are iterating in FOR loop with iterator as our variable here. Now accessing each of the collection object available in the list ‘ResObjList’ with the hep of ‘Item’ method.
For iterator = 1 To ResCount CurrentResObj = ResObjList.Item(iterator).name if UCase(CurrentResObj) = UCase(QCResName) then Set oNewResObj = ResObjList.Item(iterator) Exit For end if Next oNewResObj.Filename = UploadFileName oNewResObj.ResourceType = ResType '"Test Resource","Data table" oNewResObj.Post oNewResObj.UploadResource LocalFilePath, True
Now match the two values first your currentResobj and second your expected QCResName. If the match is successful, then set the new variable for this match as
Set oNewResObj = ResObjList.Item(iterator) ,
because we want the exact location of the resource on which we want the upload should happen.
Once this is done exit the FOR loop.
Now try uploading the file onto the resource location you got in hand.
Use oNewResObj.Filename = To set the file name which you want to upload here, in this case it is input parameter ‘UploadFileName’.
use oNewResObj.ResourceType = To set the resource type of the file to be uploaded, in this case ‘ResType’ is parameter and you can have values like ‘”Test Resource”,”Data table” and so on.
use oNewResObj.Post = to initialize the which method you want to call over http connection and finally,
use oNewResObj.UploadResource method to specify the your ‘LocalFilePath’.
Part 4 : Result
In this part you have either error in hand or a successful upload of the file. In case some error has occurred then it is reported as FAIL along with the description. And here value for return is set to False.
If upload was successful then PASS event is called and value of return is set to True.
if Err.Number <> 0 then reporter.ReportEvent micFail,"File upload to ALM got FAILED. please check the error:=" & Err.Description Fn_UploadToALM = False else reporter.ReportEvent micPass,"File upload to ALM was successful." Fn_UploadToALM = True end if
Part 5: Releasing objects
A very crucial step is releasing the objects which you used. Most of the times we do not release them thinking this might happen itself, but in VbScript there is no guarantee to be honest. For some objects it happens correctly and for some it might have memory links open. So to avoid any future issues, its always good to write code for release the binding as shown below:
Set ResObjList = Nothing
Set CurrentResObj = Nothing
Set oNewResObj = Nothing
Set ResObjList = Nothing
Set ObjResFactory = Nothing
Set ObjQC = Nothing
Till now we have discussed the code but in parts, now let’s have a look on this code integrated into one single block:
A word of Caution!
Before running this code please check you have active connection available to QC or ALM.
'------------------------------------------------------------' 'Function Name: Fn_UploadToALM 'Documentation: uploads any file with any resource type to the QC\ALM module 'Created By: Prachur Saxena 'Return Values: True or False 'Example: Fn_UploadToALM "test_data","qcresourcetest.xls","C:\Users\Public\Documents\TEST_DATA_Files","Data table" '----------------------------------------------------------- Function Fn_UploadToALM(QCResName,UploadFileName,LocalFilePath,ResType) Dim ResCount : ResCount = 0 Dim iterator Dim ObjQC,CurrentResObj,ObjResFactory,ResObjList,oNewResObj Set oNewResObj = Nothing Set ObjQC = QCUtil.QCConnection Set ObjResFactory = ObjQC.QCResourceFactory Set ResObjList =ObjResFactory.NewList("") ResCount = ResObjList.Count For iterator = 1 To ResCount CurrentResObj = ResObjList.Item(iterator).name if UCase(CurrentResObj) = UCase(QCResName) then Set oNewResObj = ResObjList.Item(iterator) Exit For end if Next oNewResObj.Filename = UploadFileName oNewResObj.ResourceType = ResType '"Test Resource","Data table" oNewResObj.Post oNewResObj.UploadResource LocalFilePath, True if Err.Number <> 0 then reporter.ReportEvent micFail,"File upload to ALM got FAIL. please check the error:=" & Err.Description Fn_UploadToALM = False else reporter.ReportEvent micPass,"File upload to ALM was successful." Fn_UploadToALM = True end if Set ResObjList = Nothing Set CurrentResObj = Nothing Set oNewResObj = Nothing Set ResObjList = Nothing Set ObjResFactory = Nothing Set ObjQC = Nothing End Function
In different scenarios of course the parameters will change. But I really doubt that till now you have noticed the actual benefit out here? Have you?
In layman terms, the function is able to receive any resource type for upload which is amazing don’t you think. You don’t need to make code for each of the resource type’s. One function is enough to handle the complete scenarios.
Plus, this makes the function more adaptable to any framework in question like linear, hybrid, keyword etc. You can use this with any existing framework or if you are designing a framework from scratch.
The important points to note here are :
- Prior calling the function you must know which type of resource you are targeting for the upload.
- Often this kind of functions are used to upload the excel file containing the test data, etc. These test data files can be used later as reference in calling scripts.
- This function gives more flexibility in uploading the files to your project designated folders.
- This function also gives you flexibility in uploading Different Types of Files.
- This function can be part of any framework in hand, i.e. regardless of kind of framework it can be easily adapted.
- Last but not least, even you can upload your test result files to be archived at common folder in QC.
I really hope you enjoyed reading the post. If you have any doubt on this please feel free to add your comment below.
And if you like to read more on UFT or QTP Technical Interview Questions please follow below links:
UFT Technical Interview Questions – Set 1
UFT Technical Interview Questions – Set 2
How to Download Resource From QC\ALM?
Or if you prefer General Interview Questions please follow below links:
UFT General Interview Questions – Part 1
UFT General Interview Questions – Part 2
If you would like to keep track of further articles on UFT (QTP). I recommend you to SUBSCRIBE by Email and have new UFT articles sent directly to your inbox.