VAPI Script to kill QTP process
There are instances when you want to kill qtp after "x" number of runs. This VAPI script does exactly that:
' ----------------------------------------------------
' Main Test Function
' Debug - Boolean. Equals to false if running in [Test Mode] : reporting to Quality Center
' CurrentTestSet - [OTA COM Library].TestSet.
' CurrentTSTest - [OTA COM Library].TSTest.
' CurrentRun - [OTA COM Library].Run.
' ----------------------------------------------------
Sub Test_Main(Debug, CurrentTestSet, CurrentTSTest, CurrentRun)
' *** VBScript Limitation ! ***
' "On Error Resume Next" statement suppresses run-time script errors.
' To handle run-time error in a right way, you need to put "If Err.Number <> 0 Then"
' after each line of code that can cause such a run-time error.
On Error Resume Next
' clear output window
TDOutput.Clear
Dim rc
Dim strProcessName:strProcessName = "QTPro.exe"
rc = ProcessExists (strProcessName)
If rc = TRUE Then
rc = ProcessKill (strProcessName)
If rc = -1 Then
TDOutput.Print "Failed to kill the " & strProcessName & " process...."
CurrentRun.Status = "Failed"
CurrentTSTest.Status = "Failed"
Else
TDOutput.Print "Successfully killed the " & strProcessName & " process...."
CurrentRun.Status = "Passed"
CurrentTSTest.Status = "Passed"
End If
Else
TDOutput.Print "There is no " & strProcessName & " process running to kill it...."
CurrentRun.Status = "Passed"
CurrentTSTest.Status = "Passed"
End If
End Sub
'##----------------------------------------------------------------------------------------------------
'## FUNCTION NAME: ProcessExists
'##----------------------------------------------------------------------------------------------------
'##
'## DESCRIPTION:
'##
'## Checks for the existance of the specified process.
'##
'## PARAMETERS:
'##
'## 1. ProcessName:
'##
'## PASSING MECHANISM: ByVal
'##
'## DATA TYPE: String
'##
'## REQUIRED: Yes
'##
'## DESCRIPTION: Name of the process to be checked. If NULL, then returns error code -1.
'##
'## RETURNS: True if the process exists and FALSE if the specified process does not exists.
'##
'## EXAMPLES:
'##
'## 1: This example decrypts the string which is encrypted using Encrypt Function.
'##
'## rc = ProcessExists ("notepad.exe")
'##
'## USAGE NOTES: None.
'##
'## REQUIRED FUNCTIONS: None.
'##
'## RELATED FUNCTIONS: ProcessKill, KillProcessFunc.
'##
'## SCREENSHOTS: None.
'##
Public Function ProcessExists (ByVal strProcessName)
Dim strFuncName:strFuncName = "ProcessExists: "
Dim strComputer, strProcess
Dim objService
If Trim (strProcessName) = "" Then
ProcessExists = FALSE
Exit Function
End If
' This statement tells to use the current machine
strComputer = "."
' "winmgmts:" is used to access the root of the CIM library
Set objService = GetObject ("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
For Each strProcess in objService.InstancesOf ("Win32_process")
If LCase (strProcess.Name) = LCase (strProcessName) Then
ProcessExists = TRUE
Exit Function
End If
Next
' Return
ProcessExists = FALSE
End Function
' End of ProcessExists common function
'##----------------------------------------------------------------------------------------------------
'## FUNCTION NAME: ProcessKill
'##----------------------------------------------------------------------------------------------------
'##
'## DESCRIPTION:
'##
'## Kills all occurences of the specified process.
'##
'## PARAMETERS:
'##
'## 1. ProcessName:
'##
'## PASSING MECHANISM: ByVal
'##
'## DATA TYPE: String
'##
'## REQUIRED: Yes
'##
'## DESCRIPTION: Name of the process to be killed. If NULL, then returns error code -1.
'##
'## RETURNS: 0 for successfully killing the process, 1 if the specified process is not found and -1 if failed to kill the specified process
'##
'## EXAMPLES:
'##
'## 1: This example decrypts the string which is encrypted using Encrypt Function.
'##
'## rc = ProcessKill ("notepad.exe")
'##
'## USAGE NOTES: None.
'##
'## REQUIRED FUNCTIONS: None.
'##
'## RELATED FUNCTIONS: None.
'##
'## SCREENSHOTS: None.
'##
Public Function ProcessKill (ByVal strProcessName)
Dim strFuncName:strFuncName = "ProcessKill: "
Dim rc:rc = 1
Dim strComputer, strProcess
Dim objService
' This statement tells to use the current machine
strComputer = "."
' "winmgmts:" is used to access the root of the CIM library
Set objService = GetObject ("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
For Each strProcess in objService.InstancesOf ("Win32_process")
If LCase (strProcess.Name) = LCase (strProcessName) Then
rc = strProcess.Terminate ()
If rc <> 0 Then
' Returns -1 as it has failed to kill the specified process
ProcessKill = -1
Exit Function
End If
End If
Next
ProcessKill = rc
End Function
' End of ProcessKill common function
--------------------------------------------------------------------------------
Happy testing!
Comments
Post a Comment