Friday, 9 August 2013

How to Export HP QTP Result in to Excel Sheet Using Script

HP QTP provides functions to add the results to the Excel File instead in the QTP Results Window.
We will be using a separate Sheet by name “Results” in which the Results of the Test will be appended.
There are 2 functions
1) initOutExcel(sTablePath)
Description: This function is to initialize a particular Excel Where you want to update the Results of the QTP Tests.
2) WriteResults(sTablePath,sStatus,sFunctionality,sDescription)
Description: This function appends the Result of a Test to the end of the existing results.
Function Code is as follows :
//Function initOutExcel code
Function initOutExcel(sTablePath)
Datatable.addSheet “Results”
Datatable.importSheet sTablePath, “Results”,”Results”
iParamCount = Datatable.getSheet(”Results”).getParameterCount
if iParamCount = 0 Then
Datatable.getSheet(”Results”).addParameter “S.No”,”"
Datatable.getSheet(”Results”).addParameter “Status”,”"
Datatable.getSheet(”Results”).addParameter “Functionality”,”"
Datatable.getSheet(”Results”).addParameter “Description”,”"
End If
Datatable.ExportSheet sTablePath,”Results”
End Function
//Function WriteResults code
Function WriteResults(sTablePath,sStatus,sFunctionality,sDescrip)
Datatable.addSheet “Results”
Datatable.importSheet sTablePath, “Results”,”Results”
iRowCount = Datatable.getSheet(”Results”).getRowCount
Datatable.getSheet(”Results”).setCurrentRow(iRowCount+1)
Datatable(”S.No”,”Results”) = iRowCount+1
Datatable(”Status”,”Results”) = sStatus
Datatable(”Description”,”Results”) = sDescrip
Datatable(”Functionality”,”Results”) = sFunctionality
Datatable.ExportSheet sTablePath,”Results”
End Function

Count the Number of a Letter Until End of String

Dim iArrCounter, str, strLen, temp, flValueFound, sResult
Dim arr2D(20,1)
str = “india is my country”
‘Initialize the first element in the array
arr2D(0,0) = “i” : arr2D(0,1) = 0 : iArrCounter = 1
strLen = Len(str)
For i = 0 to strLen – 1
flValueFound = False
‘Get the first character
temp = Left(str, 1)
‘Check if this already exists in the array. If yes, incease its count by
1. Else add it in the array and set its count as 1
For j = 0 to iArrCounter – 1
If temp = arr2D(j, 0) Then
arr2D(j, 1) = arr2D(j, 1) + 1 ‘increment the value by 1
flValueFound = True
Exit For
End If
Next
‘If value not found in the array, add it
If flValueFound = False Then
arr2D(iArrCounter,0) = temp : arr2D(iArrCounter,1) = 1
iArrCounter = iArrCounter + 1
End If
‘Truncate the string
str = Right(str, Len(str) – 1)
Next
‘Display the result
For k = 0 to iArrCounter – 1
sResult = sResult & arr2D(k, 0) & ” occurs ” & arr2D(k, 1) & ” times” &
Vbcrlf
Next
msgbox sResult

Code to Upload All the Local Files(e.g. Library) to the QC Test Plan Using OTA

Code is as follows – 
strFilePathToSave = “C:\Library\function.vbs”
Set QCConnection = QCUtil.QCConnection
Set treeManager = QCConnection.TreeManager

‘ Specify the local path to the file.
LocalFilePath = strFilePathToSave
‘ Specify the Quality Center path to the desired folder
QC_Path =”Subject\BW Automation\Library”
Set node = treeManager.nodebypath(QC_Path)
set att = node.attachments
Set atta = att.AddItem(Null)
atta.FileName = LocalFilePath
atta.Type = 1
atta.Post()

Note – This code uploads only one file, run this code in loop & assign file names to variable strFilePathToSave, will upload all files from local folder.

Explain the Keyword CreateObject

CreateObject creates and returns a reference to an Automation object
Syntax: CreateObject(servername.typename [, location])
Arguments
Servername: Required. The name of the application providing the object. E.g. Internet Explorer, Excel
Typename : Required. The type or class of the object to create.
Location : Optional. The name of the network server where the object is to be created
Example : Set IE = CreateObject(”InternetExplorer.Application”)

What Are Pitfalls of Ordinal Identifiers

QTP Ordinal Identifiers pitfalls are as follows -
a) If two objects are overlapped on each other than location based object recognition will fail.
b) If only index based recognition is used your script will work but script execution time will increase.