Friday, August 1, 2008

QTP SystemUtil Vs InvokeApplication

SystemUtil is an object which is used to control applications and processes during a run session.Run is “one of the methods” of SystemUtil object. Although there are other methods like CloseProcessByName etc.The InvokeApplication method can open only executable files and is used primarily for backward compatibility
object.Run file, [params], [dir], [op], [mode]

file: The name of the file you want to run.
params: If the specified file argument is an executable file, use the params argument to specify any parameters to be passed to the application.
dir The default directory of the application or file.
op:: The action to be performed. If this argument is blank ( ""), the open operation is performed. Other actions can be Edit, Print etc.
mode: Specifies how the application is displayed when it opens. You can specify one of the modes in the table below. Default = 1 - Activates and displays the window.

There are 10 modes, for complete list of modes, please see QTP User Guide.

Example using all arguments except params:
SystemUtil.Run "foo.txt"," " , "C:\", "Open", "1"
wait(3)
window("text:=foo - Notepad").Close
Example using params:
SystemUtil.Run "iexplore.exe",
"http://www.google.com"
InvokeApplication(Command ,[StartIn])
command: The path and command line options of the application to invoke.
startIn:The working folder to which the Command path refers.
Return Value
Boolean. If the function fails to open the application, False is returned.
Example using Command argument
The following example uses the InvokeApplication function to open Internet Explorer on my machine. You can type the below line in a new test in QTP and Run it.

InvokeApplication "C:\Program Files\Internet Explorer\IEXPLORE.EXE"

Example using both the arguments.
InvokeApplication "\Program Files\Internet Explorer\IEXPLORE.EXE", "c:"
This below example opens a text file foo which is saved in C:\ drive and waits for sometime and then closes it. You can write the below code in a new test in QTP and run it. It should work fine. (Make sure you have a file name foo.txt in C:\ drive)

SystemUtil.Run "C:\foo.txt", "", "", ""
wait(3)
window("text:=foo - Notepad").Close

You can run any application from a specified location using a SystemUtil.Run statement. This is especially useful if your test includes more than one application, and you selected the Record and run test on any application check box in the Record and Run Settings dialog box.
A SystemUtil.Run statement is automatically added to your test when you run an application from the Start menu or the Run dialog box while recording a test.


Also See:

QTP Optional Step
QTP Relative Path
QTP Random Variables
QTP Crypt Object