QTP or UFT and VBScript: How to Create Folder in QC or ALM using VBScript?
August 13, 2018
QTP or UFT and VBScript : How to Create Folder in QC or ALM using VBScript?
Welcome to this post!-“QTP or UFT and VBScript: How to Create Folder in QC or ALM using VBScript”.
If you are looking for latest HP UFT \ QTP interview Questions, then you are at right place. This post ” QTP or UFT and VBScript: How to Create Folder in QC or ALM using VBScript?” consists of technical interview question which have been part of many interviews either external or internal. Usually these are assumed to be known in deep 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 create folder in QC?
- How to add (scripted) folder under QC Test Resources?
- How to create folder using QTP or UFT in QC?
- How to create folder in QC using OTA? OR How to create folder in HP ALM using OTA?
- How to add new child folder in QC using VBS? and so on.
Well, while working with automation frameworks to find a solution on getting resources from qc folders directly to local working folder or other test suite automated scripts require previous resources which are stored in some QC Folder, to fetch from those folders and work on it is sometimes a challenge. Plus, if your QC folder needs to be different for each sprint then it’s evident to create folder in QC and then post your results and related data 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 creating a folder in QC or ALM using OTA and VBScript, but before discussing the complete code, we will visit the code in parts to understand what exactly is meant there.
Sounds Good! right?
Focusing on Function Head Comments
So, to begin with, let us focus on discussing the most important part of writing code which is always create a head 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 are 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_CreateFolderInALM 'Documentation: Create any folder at desired location in QC \ ALM. 'Created By : Prachur Saxena 'Date : 13/1/2018 'Modified By: NA 'Revision Comments: NA 'Return Values: True or False 'Example: Fn_CreateFolderInALM "ParentFolder","ChildFolder”
Wow! now that’s look fantastic and quite impressive to understand for anyone to use the function in his code. And of course, it looks so complete.
With this information he skips many steps like code debugging, asking another tester 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 some 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 someone else broader view if is 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 let’s focus on the code that we are targeting on the post that is How to Create Folder in QC or ALM using VBScript.
Part 1: Variables:
Dim ResCount : ResCount = 0 Dim iterator Dim ObjQC,CurrentResObj,ObjResFactory,ResObjList,resRoot,parentFolder,newChildFolder
In the beginning of the code it is really a good practice to write all your variables. Here we have declared all variables like
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.
parentFolder, newChildFolder = for referencing the objects of QC,
And declare all variables related to storing the connection and resource object list or object.
Part 2: Assignments
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’.
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 help of ‘Item’ method.
For iterator = 1 To ResCount Set CurrentResObj = ResObjList.Item(iterator) If InStr(1,ucase(CurrentResObj.Name),ucase(ParentFolderName),1) > 0 then on error resume next Set parentFolder = CurrentResObj.Item(iterator) Exit For End If Next On Error Resume Next Set resRoot = objQC.QCResourceFolderFactory.Root Set newChildFolder = resRoot.QCResourceFolderFactory.AddItem(null) newChildFolder.ParentId = parentFolder.item(iterator).id newChildFolder.Name = ChildFolderName newChildFolder.Post()
Now match the two values first your currentResobj name (get the name as by using code currentResobj.Name ) and second your expected ParentFolderName with the help in built function of VBScript – InStr.
If the match is successful, then set the reference of that node to parentFolder and exit for loop.
Specify the local path as we want the exact location on which we want to download the should happen.
Once this is done exit the FOR loop.
Now set the root instance of QCResourceFolderFactory and create a folder on it with the name you want, in this case it is ChildFolderName.
Part 4: Result
In this part you have either error in hand or a successful download 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 download was successful then PASS event is called and value of return is set to True.
'check for any error below if Err.Number <> 0 then reporter.ReportEvent micFail,"Folder didn't got created at desired location. Please check the error:=" & Err.Description Fn_CreateFolderInALM = False else reporter.ReportEvent micPass,"Folder got created at desired location and process was successful." Fn_CreateFolderInALM = True end if
Part 5: Releasing objects
A very crucial step is releasing the objects which you have 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, it’s always good to write code for release the binding as shown below:
Set ResObjList = Nothing
Set CurrentResObj = Nothing
Set ObjResFactory = Nothing
Set ObjQC = Nothing
Set resRoot = Nothing
Set parentFolder = Nothing
Set newChildFolder = Nothing
Till now we have discussed the complete code but in parts, now let’s have a look on the complete code:
A word of Caution!
Before running this code please check you have active connection available to QC or ALM.
'------------------------------------------------------------' 'Function Name : Fn_CreateFolderInALM 'Documentation: Create any folder at desired location in QC \ ALM. 'Created By : Prachur Saxena 'Date : 13/1/2018 'Modified By: NA 'Revision Comments: NA 'Return Values: True or False 'Example: Fn_CreateFolderInALM "ParentFolder","ChildFolder” '----------------------------------------------------------- Function Fn_CreateFolderInALM(ParentFolderName,ChildFolderName) Dim ResCount : ResCount = 0 Dim iterator Dim ObjQC,CurrentResObj,ObjResFactory,ResObjList,resRoot,parentFolder,newChildFolder Set ObjQC = QCUtil.QCConnection Set ObjResFactory = ObjQC.QCResourceFactory Set ResObjList = ObjResFactory.NewList("") ResCount = ResObjList.Count For iterator = 1 To ResCount Set CurrentResObj = ResObjList.Item(iterator) If InStr(1,ucase(CurrentResObj.Name),ucase(ParentFolderName),1) > 0 then on error resume next Set parentFolder = CurrentResObj.Item(iterator) Exit For End If Next On Error Resume Next Set resRoot = objQC.QCResourceFolderFactory.Root Set newChildFolder = resRoot.QCResourceFolderFactory.AddItem(null) newChildFolder.ParentId = parentFolder.item(iterator).id newChildFolder.Name = ChildFolderName newChildFolder.Post() 'check for any error below if Err.Number <> 0 then reporter.ReportEvent micFail,"Folder didn't got created at desired location. Please check the error:=" & Err.Description Fn_CreateFolderInALM = False else reporter.ReportEvent micPass,"Folder got created at desired location and process was successful." Fn_CreateFolderInALM = True end if Set ResObjList = Nothing Set CurrentResObj = Nothing Set ObjResFactory = Nothing Set ObjQC = Nothing Set resRoot = Nothing Set parentFolder = Nothing Set newChildFolder = Nothing End Function
In different scenarios of course, the parameters will change.
What else could have been done here? You could have also used ‘id’ as your input parameter to exactly locate the folder on which you want to create your child folder. But it has some drawback which is you need to know the ‘id’ of the parent folder in hand before calling function, which is not recommended.
This brings us to the end of the discussion on “How to create folder in QC or ALM using VBScript”and I think we should look at the points to take from this discussion.
Conclusion
The important points to note here are:
- Prior calling the function you must know parent folder name so that you can easily focus on it to create folder in QC.
- Often this kind of functions are used to upload or download the test related resources and hence making the child folder name unique to the sprint is important.
- This function gives more flexibility in uploading or downloading the files to your project designated folders.
- This function also gives you flexibility to implement sub or child folder inside child folder while creating the folder in QC without calling the same function again.
- This function can be part of any framework in hand, i.e. regardless of kind of framework it can be easily adapted.
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?
How to Upload Resource To 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.