Saturday, 22 January 2011


3 different ways to close an application through Task Manager using QTP


n this article, we’ll see how to use QTP to close an application through Windows Task Manager. We all know that Windows Task Manager is an application available in Windows Operating Systems which provides detailed information about all the processes/tasks currently running in your system. To manually close any process/task from the task manager, all you have to do is select the task/process in the task manager and use the End Task/End Process button to close the task. Lets see how the same can be achieved using QTP.
QTP provides 3 built-in methods using which you can close a process from Task Manager. All these 3 methods are part of SystemUtil object. These 3 methods use the Process ID, Process Name & Application Window Title to access and then close the process in Task Manager. QTP initially tries to close the process by sending WM_CLOSE message to the process window. If the process is still open after 5 seconds, QTP then terminates the process.
Method NameDescription
CloseProcessByNameCloses a process based on its name.
CloseProcessByIdCloses a process according to its Process ID (PID)
CloseProcessByWndTitleCloses a task based on the title of the window
Task Manager Process


Lets see some examples on how to use these SystemUtil methods.

1. CloseProcessByName – This method closes the process based on its image name in the task manager. It returns a long value which represents the number of instances of the application that are closed using this method.
1
2
3
4
5
6
7
8
'Open 3 notepad instances
SystemUtil.Run "notepad.exe"
SystemUtil.Run "notepad.exe"
SystemUtil.Run "notepad.exe"
 
'Close the notepads using the image name
iReturn = SystemUtil.CloseProcessByName("notepad.exe")
msgbox iReturn 'Returns 3 as total 3 notepads are closed

2. CloseProcessById – This method closes a process based on its Process ID and returns a) True if the process was closed successfully b) False if the process was not closed.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Dim pID_Notepad, pID_Excel
 
'Open notepad and excel applications
SystemUtil.Run "notepad.exe"
SystemUtil.Run "excel.exe"
 
'Find Process IDs of both the applications
pID_Notepad = Window("regexpwndtitle:=Notepad").GetROProperty("process id")
pID_Excel = Window("regexpwndtitle:=XLMAIN").GetROProperty("process id")
 
'Close both the applications
iReturn_N = SystemUtil.CloseProcessById(pID_Notepad)
iReturn_E = SystemUtil.CloseProcessById(pID_Excel)
 
msgbox iReturn_N 'Returns True
msgbox iReturn_E 'Returns True

3. CloseProcessByWndTitle – This method closes a process based on its window title and returns a long value that represents the number of instances of the applications that are closed.
1
2
3
4
5
6
7
8
'Open notepad and excel applications
SystemUtil.Run "notepad.exe"
SystemUtil.Run "excel.exe"
 
'Close all the tasks where the window title exactly matches "Untitled - Notepad"
SystemUtil.CloseProcessByWndTitle "Untitled - Notepad", False
'Close all the tasks where the window title contains the string "Excel"
SystemUtil.CloseProcessByWndTitle "Excel", True


Other than these 3 methods, SystemUtil object provides 2 more in-built methods that can be used to close an application. These methods are – 

a. CloseDescendentProcesses – This method closes all the processes that are opened by QTP. It returns a long value which indicates the number of instances of the application that are closed using this method.
1
2
3
4
5
6
7
'Open notepad and excel applications using QTP
SystemUtil.Run "notepad.exe"
SystemUtil.Run "excel.exe"
 
'Close all the processes opened using QTP
iReturn = SystemUtil.CloseDescendentProcesses
msgbox iReturn

b. CloseProcessByHwnd – This method closes a process based on its window handle (The window handle can be found out using ‘hwnd’ property). This method returns a) True – if the process is closed successfully, and b) False if the process is not closed.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Dim hwnd_Notepad, hwnd_Excel
 
'Open notepad and excel applications
SystemUtil.Run "notepad.exe"
SystemUtil.Run "excel.exe"
 
'Find Handles of both the application windows
hwnd_Notepad = Window("regexpwndtitle:=Notepad").GetROProperty("hwnd")
hwnd_Excel = Window("regexpwndtitle:=XLMAIN").GetROProperty("hwnd")
 
'Close both the applications
iReturn_N = SystemUtil.CloseProcessByHwnd(hwnd_Notepad)
iReturn_E = SystemUtil.CloseProcessByHwnd(hwnd_Excel)
 
msgbox iReturn_N 'Returns True
msgbox iReturn_E 'Returns True
This was all about the SystemUtil built-in methods that can be used to close an application process. 

0 comments:

Post a Comment

TrainingHUB. Powered by Blogger.

Total Pageviews

THE BEST QTP TRAINING INSTITUTE IN HYDERABAD

QTP Training in hyderabad

Sql Tutorial

Popular Posts

Our Facebook Page

TrainingHUB

Followers