QTP Reusable function Codes :
Automation testing using QTP has always been talked upon the most in the testing industry. There are numerous features available within this very toolset, but most of us might not be aware of this very feature which has been discussed in this very articles on automation testing using QTP which is functions created in Vbscript , and is very useful in code customization to meet our automation testing requirements , as it reduces the script maintenance effort.
1. Getting Number from String
Function extract_number(msg)
Dim re, matches, item, result
Set re = New RegExp
re.pattern = "[A-Za-z -.]*(\d+).*"
Set matches = re.Execute(msg)
If matches.Count > 0 Then
Set item = matches(0)
If item.SubMatches.Count > 0 Then
result = item.SubMatches(0)
Else
result = -1
End If
Else
result = -1
End If
extract_number = result
End Function
MsgBox extract_number("This user belongs to 10 groups")
MsgBox extract_number("206 features assigned to the user.")
2. Close All the opened Browsers Except QC opened browser
On Error Resume Next
Dim intWndCnt
Dim oDesc, oWnd
'Create Object description
Set oDesc = Description.Create '
Set oWnd = Desktop.ChildObjects(oDesc)
intWndCnt = oWnd.Count
For i = 0 to intWndCnt - 1
Set TmpObj = oWnd.item(i)
strTmpTitle = oWnd.item(i).GetROProperty("Text")
If instr(1,strTmpTitle, "Microsoft Internet Explorer",1) > 0 Then
If instr(1,strTmpTitle, "Mercury Quality Center",1) > 0 Then
'msgbox "Title :" & oWnd.item(i).GetROProperty("Text")
Else
'msgbox "Close :" & oWnd.item(i).GetROProperty("Text")
oWnd.item(i).close
End if
End If
Next
'Clean up
Set oDesc = nothing
Set oWnd = nothing
3. Close all opened Excel files
Public Function fn_close_all_excel_files()
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery ("Select * from Win32_Process Where Name = 'EXCEL.EXE'")
For Each objProcess in colProcessList
objProcess.Terminate()
Next
End Function
4. Excel sheet column count
Public Function dtGetColCount (sSheetName)
On Error Resume Next
Do
i = i + 1
sColName = DataTable.GetSheet(sSheetName).GetParameter(i).Name
Loop While 0 = Err.Number
'GetParameter throws an error when using an index that is out of bounds ...
'We can use this functionality to ASSUME that it's an unused column.
'We've come to the end of our USED columns
dtGetColCount = i - 1
On Error GoTo 0
End Function
5. Count Number of files in any Folder
iFileCount = 0
Set objFileSysOb = CreateObject("Scripting.FileSystemObject")
Set colFolderName = objFileSysOb.GetFolder("C:\Automation") ' Folder Path
Set vFiles =colFolderName.Files
For each vFileItem in vFiles
print vFileItem
iFileCount = iFileCount + 1
Next
print iFileCount
6. Create Dynamic Array
For i = 0 to 20
Redim Preserve arrFileLines(i)
arrFileLines(i) = "test" & i
Next
7. Close Dialog box
Function DialogClose (obj)
If obj.Dialog("nativeclass:=#32770").Exist(0) Then
obj.Dialog("nativeclass:=#32770").WinButton("nativeclass:=Button","index:=0").Click
End If
RegisterUserFunc "Browser", "DialogClose", "DialogClose"
End Function
8. Create Dynamic Value
iDynamicValue = now
iDynamicValue = replace(iDynamicValue,"/","")
iDynamicValue = replace(iDynamicValue,":","")
iDynamicValue = replace(iDynamicValue," ","")
iDynamicValue = replace(iDynamicValue,"PM","")
iDynamicValue = replace(iDynamicValue,"AM","")
msgbox iDynamicValue
9. Get QTP file path
Set qtApp = CreateObject("QuickTest.Application") ' Create the Application objectqtpApp.Test.Location
TestLoc = qtApp.Test.Location
MsgBox TestLoc
10. IS QC Connected?
Set qtApp = CreateObject("QuickTest.Application")
If qtApp.TDConnection.IsConnected Then
msgbox "QC"
Else
msgbox "Local"
End If
(or)
if QCUtil.IsConnected then
Reporter.ReportEvent 0, "Connected", "Connected to server: " + QCUtil.QCConnection.ServerName + chr (13) +"Project: " +
QCUtil.QCConnection.ProjectName + chr (13) + "Domain: " + QCUtil.QCConnection.DomainName
else
Reporter.ReportEvent 1, "Not connected", "Not connected to Quality Center"
end if
11. Keyboard Key Press through QTP Script
'Create Shell Object
Set WshShell = CreateObject("WScript.Shell")
'Send any Functinal Keys
' ALT = %
' F4 = {F4}
WshShell.SendKeys "%{F4}"
' Set shell object to Null
Set WshShell=nothing
12. QTP9.0 Futurs
- Open and edit multiple Object Repositories
- Easy conversion to/from XML
- Multiple object repositories per test asset
- Leveraging the automation assets across multiple groups
- Missing resources panel
- Pass parameters between Actions
Learn More On QTP Reusable Function creation codes :
QTP-reusable-functions-codes-5
QTP-reusable-functions-codes-4
QTP-reusable-functions-codes-3
QTP-reusable-functions-codes-2
QTP-reusable-functions-codes-1
For gaining more insights in the automation using QTP log on to below url :
Automation Testing Using QTP
Function extract_number(msg)
Dim re, matches, item, result
Set re = New RegExp
re.pattern = "[A-Za-z -.]*(\d+).*"
Set matches = re.Execute(msg)
If matches.Count > 0 Then
Set item = matches(0)
If item.SubMatches.Count > 0 Then
result = item.SubMatches(0)
Else
result = -1
End If
Else
result = -1
End If
extract_number = result
End Function
MsgBox extract_number("This user belongs to 10 groups")
MsgBox extract_number("206 features assigned to the user.")
2. Close All the opened Browsers Except QC opened browser
On Error Resume Next
Dim intWndCnt
Dim oDesc, oWnd
'Create Object description
Set oDesc = Description.Create '
Set oWnd = Desktop.ChildObjects(oDesc)
intWndCnt = oWnd.Count
For i = 0 to intWndCnt - 1
Set TmpObj = oWnd.item(i)
strTmpTitle = oWnd.item(i).GetROProperty("Text")
If instr(1,strTmpTitle, "Microsoft Internet Explorer",1) > 0 Then
If instr(1,strTmpTitle, "Mercury Quality Center",1) > 0 Then
'msgbox "Title :" & oWnd.item(i).GetROProperty("Text")
Else
'msgbox "Close :" & oWnd.item(i).GetROProperty("Text")
oWnd.item(i).close
End if
End If
Next
'Clean up
Set oDesc = nothing
Set oWnd = nothing
3. Close all opened Excel files
Public Function fn_close_all_excel_files()
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery ("Select * from Win32_Process Where Name = 'EXCEL.EXE'")
For Each objProcess in colProcessList
objProcess.Terminate()
Next
End Function
4. Excel sheet column count
Public Function dtGetColCount (sSheetName)
On Error Resume Next
Do
i = i + 1
sColName = DataTable.GetSheet(sSheetName).GetParameter(i).Name
Loop While 0 = Err.Number
'GetParameter throws an error when using an index that is out of bounds ...
'We can use this functionality to ASSUME that it's an unused column.
'We've come to the end of our USED columns
dtGetColCount = i - 1
On Error GoTo 0
End Function
5. Count Number of files in any Folder
iFileCount = 0
Set objFileSysOb = CreateObject("Scripting.FileSystemObject")
Set colFolderName = objFileSysOb.GetFolder("C:\Automation") ' Folder Path
Set vFiles =colFolderName.Files
For each vFileItem in vFiles
print vFileItem
iFileCount = iFileCount + 1
Next
print iFileCount
6. Create Dynamic Array
For i = 0 to 20
Redim Preserve arrFileLines(i)
arrFileLines(i) = "test" & i
Next
7. Close Dialog box
Function DialogClose (obj)
If obj.Dialog("nativeclass:=#32770").Exist(0) Then
obj.Dialog("nativeclass:=#32770").WinButton("nativeclass:=Button","index:=0").Click
End If
RegisterUserFunc "Browser", "DialogClose", "DialogClose"
End Function
8. Create Dynamic Value
iDynamicValue = now
iDynamicValue = replace(iDynamicValue,"/","")
iDynamicValue = replace(iDynamicValue,":","")
iDynamicValue = replace(iDynamicValue," ","")
iDynamicValue = replace(iDynamicValue,"PM","")
iDynamicValue = replace(iDynamicValue,"AM","")
msgbox iDynamicValue
9. Get QTP file path
Set qtApp = CreateObject("QuickTest.Application") ' Create the Application objectqtpApp.Test.Location
TestLoc = qtApp.Test.Location
MsgBox TestLoc
10. IS QC Connected?
Set qtApp = CreateObject("QuickTest.Application")
If qtApp.TDConnection.IsConnected Then
msgbox "QC"
Else
msgbox "Local"
End If
(or)
if QCUtil.IsConnected then
Reporter.ReportEvent 0, "Connected", "Connected to server: " + QCUtil.QCConnection.ServerName + chr (13) +"Project: " +
QCUtil.QCConnection.ProjectName + chr (13) + "Domain: " + QCUtil.QCConnection.DomainName
else
Reporter.ReportEvent 1, "Not connected", "Not connected to Quality Center"
end if
11. Keyboard Key Press through QTP Script
'Create Shell Object
Set WshShell = CreateObject("WScript.Shell")
'Send any Functinal Keys
' ALT = %
' F4 = {F4}
WshShell.SendKeys "%{F4}"
' Set shell object to Null
Set WshShell=nothing
12. QTP9.0 Futurs
- Open and edit multiple Object Repositories
- Easy conversion to/from XML
- Multiple object repositories per test asset
- Leveraging the automation assets across multiple groups
- Missing resources panel
- Pass parameters between Actions
Learn More On QTP Reusable Function creation codes :
QTP-reusable-functions-codes-5
QTP-reusable-functions-codes-4
QTP-reusable-functions-codes-3
QTP-reusable-functions-codes-2
QTP-reusable-functions-codes-1
For gaining more insights in the automation using QTP log on to below url :
Automation Testing Using QTP
إرسال تعليق