Wednesday, 2 February 2011

How to Launch or Close the Process by using Systemutil.Run




SystemUtill.Run
How to Launch or Close the Process by using QTP
There are many ways to close the process as of now we will deal with SystemUtil
SystemUtil Object
QTP Provided many utility object one of them is SystemUtil.It can be used to for launching or closes a process. We will look in  various operations supported by this object:
SystemUtil.Run ( Filename, Parameters, Path, Operation )

Ø  Filename: The name of the file which we want to run.
Ø  Parameters: If the specified Filename is an executable file, use the Parameters argument to specify any parameters which need to be passed into the application.
Ø  Path: The default directory of the application or file.
Ø  Operation: The action to be performed. If this argument is blank (""), the open operation is performed.
The following operations can be specified for the operation argument of the SystemUtil.Run method:
The Run method allows running a new process. The code below illustrates few example on how to launching a process using SystemUtil object
'@@@@@@For run chrome explorer
SystemUtil.Run "chrome.exe"
'For run internet explorer
SystemUtil.Run "firefox.exe"
'@@@@@@For run internet explorer
SystemUtil.Run "iexplore.exe"

'@@@@@@For run internet explorer and open google.com
SystemUtil.Run " chrome.exe ", "http://www.google.com"

'@@@@@@Run a IE Process in Maximized window
const show_maximized = 5 'I will activates the window and displays it as a maximized window.
SystemUtil.Run "iexplore.exe", "http://www.google.com", , , SHOW_MAXIMIZED

SystemUtil.CloseProcessByName (bsProcessName)
“CloseProcessByName” is a method will allows closing a process by its name.
Example
'@@@@@@Close all internet explorer windows
closedCount = SystemUtil.CloseProcessByName("iexplore.exe")

'@@@@@@Display # of closed windows
MsgBox closedCount
The problem with using the above method is that it will also close Quality Center (QC) window even if script was launched from QC. This method should be used in case you are not concerned about running scripts from QC.
SystemUtil.CloseProcessByWndTitle (bsTitle, [bRegExp])
CloseProcessByWndTitle method allows closing a process by its title. The title can also be supplied as a regular expression by setting the bRegExp to True
'@@@@@@ For launching a notepad window
SystemUtil.Run"notepad.exe"

'@@@@@@Close the window just For launced using the exact title
SystemUtil.CloseProcessByWndTitle "Untitled - Notepad"

'@@@@@@For launching a  notepad window
SystemUtil.Run "notepad.exe"

@@@@@@For launching a Notepad, creating, saving, closing.(We can’t save a new notepad while other is open make sure-)
SystemUtil.Run "notepad.exe"
Window("Notepad").WinMenu("Menu").Select "File;Save Ctrl+S"
wait 2
Window("Notepad").Dialog("Save As").WinEdit("File name:").Set "TraningHub"
wait 3
Window("Notepad").Dialog("Save As").WinButton("Save").Click
wait 5
Window("Notepad").Close
'@@@@@@Close the window just For launching aed using a pattern string
SystemUtil.CloseProcessByWndTitle ".*Notepad", True
SystemUtil.CloseDescendentProcesses

CloseDescendentProcesses can be used to close any process For launching aed by QTP. The code below illustrates the usage

'@@@@@@For launching a explorer
SystemUtil.Run "iexplore.exe"

'@@@@@@For launching a excel using COM
Set oXL = CreateObject("Excel.Application")
oXL.Visible = True

'@@@@@@Close processes For launching aed by QTP. This will close
'the internet explorer and Excel as well and for any other.
SystemUtil.CloseDescendentProcesses

This method is best suited to be used during the end of a script to cleanup any process left open.

To Close all open Browsers


For Closing all open Browsers
'@@@@@@Check the existence of a browser and close it
'this code will ensure that  no more browsers exist
While Browser("creationtime:=0").Exist(0)

'@@@@@@Close the browser
Browser("creationtime:=0").Close

Wend



No comments:

Post a Comment