SystemUtil Object
Description
An object used to control applications and processes during a run session.
在脚本运行过程中,可使用该对象控制应用程序或进程。
Operations
The tables below list the built-in methods and properties that you can use as operations for the SystemUtil object.
下表列出了SystemUtil 对象的内建方法与属性。
Methods
BlockInput Prevents keyboard and mouse input events from reaching applications.
禁止键盘或鼠标操作。
CloseDescendentProcesses Closes all processes opened by QuickTest.
关闭由QuickTest 打开的所有进程。
CloseProcessByHwnd Closes a process that is the owner of a window with the specified handle.
根据窗口的句柄找到相应的进程,并关闭该进程。
CloseProcessById Closes a process according to its Process ID (PID).
关闭指定进程PID 的进程。
CloseProcessByName Closes a process according to its name.
关闭指定进程名称的进程。
CloseProcessByWndTitle Closes all processes that are owners of windows with the specified title.
关闭指定窗口标题的进程。
Run Runs a file or application.
运行文件或应用程序。
UnblockInput Re-enables keyboard and mouse input events after a BlockInput statement
was used to block them.
在BlockInput 语句禁止键盘或鼠标之后,使用UnblockInput 语句可解除对键盘或
鼠标的禁止。
BlockInput Method
Description
Prevents keyboard and mouse input events from reaching applications.
禁止键盘或鼠标操作。
Syntax
object.BlockInput
Argument Description
Object A test object of type SystemUtil.
SystemUtil 类型的测试对象。
Return Value
None
Remarks
You can use this method to prevent a run session being accidentally interrupted by someone using the keyboard
or mouse on a QuickTest Professional computer.
当电脑正在运行QTP 脚本时,使用BlockInput 方法可以防止因键盘或鼠标的操作扰乱QTP 的正常运行。
After using this method, keyboard and mouse input is blocked until one of the following occurs:
使用BlockInput 方法后,使用以下方法可以解除对键盘或鼠标的禁止:
· An UnblockInput statement is used
当使用了UnblockInput 语句后。
· A run session ends or is paused for any reason (end of test run, run error, breakpoint, and so forth)
当脚本运行结束或中断(如整个test 结束、运行错误、断点等等)。
· The Ctrl+Alt+Delete key combination is pressed on the keyboard
在键盘上按下Ctrl+Alt+Delete 结合键后。
· A critical system error occurs
当出现了严重的系统错误时。
Example
Block Keyboard and Mouse Input During a Run Session
下面的例子使用BlockInput 方法,在QTP 脚本运行过程中禁用了键盘及鼠标。
Sub BlockInput_Example()
SystemUtil.BlockInput
Browser("Welcome: Mercury Tours").Page("Welcome: Mercury Tours").WebEdit("userName").Set
"mercury"
Browser("Welcome: Mercury Tours").Page("Welcome: Mercury
Tours").WebEdit("password").SetSecure "4082986e39ea469e70dbf8c5a29429fe138c6efc"
Browser("Welcome: Mercury Tours").Page("Welcome: Mercury Tours").Image("Sign-In").Click 2,
2
SystemUtil.UnblockInput
End Sub
UnblockInput Method
Description
Re-enables keyboard and mouse input events after a BlockInput statement was used to block them.
在BlockInput 语句禁止键盘或鼠标之后,使用UnblockInput 语句可解除对键盘或鼠标的禁止。
Syntax
object.UnblockInput
Argument Description
object A test object of type SystemUtil.
SystemUtil 类型的测试对象。
Return Value
None
Remarks
You can use this method to unblock keyboard and mouse input that was earlier blocked using a BlockInput
statement.
如果先前使用了BlockInput 方法禁止了键盘及鼠标,使用UnblockInput 方法可解除禁止。
Example
Unblock Keyboard and Mouse Input That Was Previously Blocked
本例在开始使用BlockInput 方法禁止了键盘及鼠标,后来使用UnblockInput 方法解除了禁止。
Sub UnblockInput_Example()
SystemUtil.BlockInput
Browser("Welcome: Mercury Tours").Page("Welcome: Mercury Tours").WebEdit("userName").Set
"mercury"
Browser("Welcome: Mercury Tours").Page("Welcome: Mercury
Tours").WebEdit("password").SetSecure "4082986e39ea469e70dbf8c5a29429fe138c6efc"
Browser("Welcome: Mercury Tours").Page("Welcome: Mercury Tours").Image("Sign-In").Click 2,
2
SystemUtil.UnblockInput
End Sub
CloseDescendentProcesses Method
Description
Closes all processes opened by QuickTest.
关闭由QuickTest 打开的所有进程。
Syntax
object.CloseDescendentProcesses
Argument Description
object A test object of type SystemUtil.
SystemUtil 类型的测试对象。
Return Value
A Long value. The number of instances of the application that are closed when the statement runs.
一个长整型值。被CloseDescendentProcesses 方法语句关闭的程序实例数。
Remarks
Note: QuickTest initially tries to close the process by sending a WM_CLOSE message to the process window. If
the process is still open after 5 seconds, QuickTest terminates the process.
注意:执行该语句时,QuickTest 首先向进程窗口发送一个WM_CLOSE 信息,偿试关闭该进程;如果该进程在5 秒钟后存未
关闭,则QuickTest 强制结束进程。
Example
Close All Applications Opened By QuickTest
在本例中:假定在运行程序之初,Record and Run 对话框自动打开了某个窗口,然后通过Run 语句打开了一个NotePad 窗口,
那么该CloseDscendentProcessed 方法的返回值就就该是2,因此Message 对话框显示的信息应该是”2“。
Sub CloseDescendentProcesses_Example()
SystemUtil.Run "Notepad.exe"
MsgBox SystemUtil.CloseDescendentProcesses
End Sub
CloseProcessByHwnd Method
Description
Closes a process that is the owner of a window with the specified handle.
根据窗口的句柄找到相应的进程,并关闭该进程。
Syntax
object.CloseProcessByHwnd (hWnd)
Argument Description
object A test object of type SystemUtil.
SystemUtil 类型的测试对象。
hWnd Required. An ULong object.
The handle of the window owned by the process you want to close.
Tip: You can retrieve the window handle using the hwnd property. For
example:
Window("MyAppName").GetROProperty("hwnd")
必须。一个ULong 对象。
你想要关闭的进程的窗口的句柄。
Tip:你可以通过获取Runtime 窗口对象的hwnd 属性值来获得句柄信息。例如:
Window(“MyAppName”).GetRoProperty(“hwnd”)
Return Value
A Boolean value.
一个Boolean 值。
· True--The specified process was successfully closed.
True――指定的进程已被成功关闭。
· False--The specified process was not closed.
False――指定的进程未被关闭。
Remarks
Note: QuickTest initially tries to close the process by sending a WM_CLOSE message to the process window. If
the process is still open after 5 seconds, QuickTest terminates the process.
注意:执行该语句时,QuickTest 首先向进程窗口发送一个WM_CLOSE 信息,偿试关闭该进程;如果该进程在5 秒钟后存未
关闭,则QuickTest 强制结束进程。
Example
Close the Process that is an Owner of a Window with the Specified Handle
在本例中,首先通过GetroProperty 方法获取NotePad 窗口的句柄,然后使用CloseProcessByHwnd 方法关闭Notepad 程
序。
Sub CloseProcessByHwnd_Example()
hWnd = Window("Notepad").GetROProperty("hwnd")
SystemUtil.CloseProcessByHwnd (hWnd)
End Sub
CloseProcessById Method
Description
Closes a process according to its Process ID (PID).
根据进程的Process ID(PID)关闭进程。
Syntax
object.CloseProcessById (wdProcessId)
Argument Description
object A test object of type SystemUtil.
SystemUtil 类型的测试对象。
wdProcessId Required. An ULong object.
The Process ID (PID) of the process you want to close.
Tip: You can find the PID of an application by viewing the value in the
Processes tab of the Windows Task Manager, or you can retrieve the value
using the process id property. For example:
Window("MyAppName").GetROProperty("process id")
必须。一个ULong 对象。
你想关闭的进程的PID。
Tip:在Windows 的任务窗口的进程标签页可以查看到程序的PID 值,你也可以通
过获取Runtime 窗口对象的Process id 属性值来获得PID 信息。例如:
Window(“MyAppName”).GetRoProperty(“process id”)
Return Value
A Boolean value.
一个Boolean 值。
· True--The specified process was successfully closed.
True――指定的进程已被正常关闭。
· False--The specified process was not closed.
False――指定的进程未被关闭。
Remarks
Note: QuickTest initially tries to close the process by sending a WM_CLOSE message to the process window. If
the process is still open after 5 seconds, QuickTest terminates the process.
注意:执行该语句时,QuickTest 首先向进程窗口发送一个WM_CLOSE 信息,偿试关闭该进程;如果该进程在5 秒钟后存未
关闭,则QuickTest 强制结束进程。
Example
Close an Application According to its Process ID
下面的例子通过GetRoProperty 方法获取Notepad 窗口的PID 值,然后使用CloseProcessById 方法关闭Notepad 程序。
Sub CloseProcessById_Example()
PID = Window("Notepad").GetROProperty("process id")
SystemUtil.CloseProcessById (PID)
End Sub
CloseProcessByName Method
Description
Closes a process according to its name.
Syntax
object.CloseProcessByName (bsProcessName)
Argument Description
object A test object of type SystemUtil.
SystemUtil 类型的测试对象。
bsProcessName Required. A String value.
The name of the process you want to close.
必须。一个字符串值。
你想关闭的进程的名称。
Return Value
A Long value. The number of instances of the application that are closed when the statement runs.
一个Long 值。所关闭的程序的实例数。
Remarks
Note: QuickTest initially tries to close the process by sending a WM_CLOSE message to the process window. If
the process is still open after 5 seconds, QuickTest terminates the process.
注意:执行该语句时,QuickTest 首先向进程窗口发送一个WM_CLOSE 信息,偿试关闭该进程;如果该进程在5 秒钟后存未
关闭,则QuickTest 强制结束进程。
Example
Close All Instances of a Specified Application
本例通过CloseProcessByName 方法,关闭所有已打开的Notepad 实例。如果当前所有的Notepad 实例都是由下面的Run
语句打开的,则CloseProcessByName 方法语句会关闭这些实例,并且MsgBox 窗口显示的数值应为“3”。
Sub CloseProcessByName_Example()
SystemUtil.Run "Notepad.exe"
SystemUtil.Run "Notepad.exe"
SystemUtil.Run "Notepad.exe"
MsgBox SystemUtil.CloseProcessByName("Notepad.exe")
End Sub
CloseProcessByWndTitle Method
Description
Closes all processes that are owners of windows with the specified title.
关闭指定窗口标题的所有进程。
Syntax
object.CloseProcessByWndTitle (bsTitle, [bRegExp])
Argument Description
object A test object of type SystemUtil.
SystemUtil 类型的测试对象。
bsTitle Required. A String value.
The title of the window owned by the process you want to close.
必须。一个字符串值。
你想关闭的进程的窗口的标题。
bRegExp Optional. A Boolean value.
Indicates whether the bsTitle argument is treated as a regular
expression. Default = False.
可选。一个Boolean 值。
检查BsTitle 参数是否是一个正则表达式。默认值为False。
Return Value
A Long value. The number of instances of the application that are closed when the statement runs.
一个Long 值。被语句所关闭的程序的实例数。
Remarks
Note: QuickTest initially tries to close the process by sending a WM_CLOSE message to the process window. If
the process is still open after 5 seconds, QuickTest terminates the process.
注意:执行该语句时,QuickTest 首先向进程窗口发送一个WM_CLOSE 信息,偿试关闭该进程;如果该进程在5 秒钟后存未
关闭,则QuickTest 强制结束进程。
Example
Close All Windows with a Title Containing a Specified String
本例关闭了所有的标题包含有“Notepad”字样的顶层窗口。因为bRegExp 参数值为“ture”,表示bsTitle 值是一个正则表
达式。
Sub CloseProcessByWndTitle_Example1()
SystemUtil.CloseProcessByWndTitle "Notepad", True
End Sub
Close All Windows in which the Exact Title Matches a Specified String
本例关闭所有窗口标题为“Untitled-Notepad”的顶层窗口。因为bRegExp 参数值为“false”,表示bsTitle 参数值是一个文
字串,因此窗口标题必须与参数值精确匹配。
Sub CloseProcessByWndTitle_Example2()
SystemUtil.CloseProcessByWndTitle "Untitled - Notepad"
End Sub
Run Method
Description
Runs a file or application.
运行一个文件或应用程序。
Syntax
object.Run file, [params], [dir], [op], [mode]
Argument Description
object A test object of type SystemUtil.
SystemUtil 类型的测试对象。
file Required. A String value.
The name of the file you want to run.
必须。一个字符串值。
你想要运行的文件的名称。
params Optional. A String value.
If the specified file argument is an executable file, use the params argument to specify
any parameters to be passed to the application.
可选。一个字符串值。
如果file 参数中指定的是一个可执行文件,则可以使用params 参数来指定该可执行文件的运行参数。
dir Optional. A String value.
The default directory of the application or file.
可选。一个字符串值。
应用程序或文件的默认目录。
op Optional. A String value. The action to be performed. If this argument is blank ( ""), the
open operation is performed.
The following operations can be specified for the op argument:
可选。一个字符串值。将要被执行的动作。如果该参数值为空(“”),则默认操作为open 操作。
下面是op 参数中可以指定的操作:
Operation Description
open
Opens the file specified by the FileName parameter. The file can be an
executable file, a document file, or a folder. Non-executable files are
open in the associated application.
打开file 参数中所指定的文件。文件有可能是一个可执行文件、一个文档文件、或
一个文件夹。非可执行文件会被相应的应用程序打开。
edit
Launches an editor and opens the document for editing. If the FileName
argument does not specify an editable document file, the statement
fails.
将文件在文件编辑器中打开以进行编辑。如果file 参数中指定的文件不可编辑,则
本语句失败。
explore
Explores the folder specified by the FileName argument.
打开由file 参数中所指定的文件夹。
find
Initiates a search starting from the specified folder path.
在file 参数所指定的文件夹中进行搜索。
print
Prints the document file specified by the FileName argument. If the
specified file is not a printable document file, the statement fails.
打印在file 参数中所指定的文档。如果参数中所指定的文档不可打印,则语句失败。
mode Optional. An Integer value.
Specifies how the application is displayed when it opens. You can specify one of the modes
in the table below.
Default = 1
可选。一个Integer 值。
指定应用程序打开时的显示模式。可以指定以下几种打开模式。默认模式是1。
Mode Description
0
Hides the window and activates another window.
隐藏程序窗口,激活另外一下窗口。
1
Activates and displays the window. If the window is minimized or maximized,
the system restores it to its original size and position. Specify this flag when
displaying the window for the first time.
激活并显示程序窗口。如果窗口是最大化或最小化状态,则将它恢复为默认窗口大小并放
置于默认位置。
2
Activates the window and displays it as a minimized window.
激活窗口并最小化。
3
Activates the window and displays it as a maximized window.
激活窗口并最大化。
4
Displays the window in its most recent size and position. The active window
remains active.
按照窗口最近一次的显示大小及位置显示该窗口,但不激活该窗口。原来活动的窗口仍保
持活动状态。
5
Activates the window and displays it in its current size and position.
激活窗口,保持当前的大小及位置不变。
6
Minimizes the specified window and activates the next top-level window in the
Z order.
最小化指定的窗口,并激活它的下一个top-level 窗口(按Z 顺序找到它的下一下top-level
窗口)。
7
Displays the window as a minimized window. The active window remains active.
显示窗口并最小化,但不激活该窗口。原来活动的窗口仍保持活动状态。
8
Displays the window in its current state. The active window remains active.
显示窗口,大小及位置保存不变。原来活动的窗口仍保持活动状态。
9
Activates and displays the window. If the window is minimized or maximized,
the system restores it to its original size and position. Specify this flag when
restoring a minimized window.
激活并显示窗口。如果窗口处于最大化或最小化状态,则将它恢复为原始大小与位置。
10
Sets the show-state based on the state of the program that started the
application.
Return Value
None
Remarks
When specifying a non-executable file, the file opens in the associated application.
当指定的是一个非可执行文件时,文件被相应的应用程序打开。
Note: 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.
注意:在录制脚本的过程中,如果你从系统的“开始”菜单或“运行”对话框中运行应用程序时,SystemUtil.Run 语句会自动
添加到脚本中。
Tip: You can also use this method to perform operations on the specified file, similar to the usage of the Windows
ShellExecute command.
Tip:你也可以使用本方法对指定的文件进行操作,该功能类似于使用Windows ShellExecute 命令。
Example
Open a Text File in the Default Text Application (Notepad)
本例使用Run 方法打开了一个名为type.txt 的文件(txt 类型文件的默认打开程序是Notpad),然后向文件中输入“happy days”
文字,然后使用快捷键保存该文件,然后关闭NotePad 应用程序。
Sub CloseDescendentProcesses_Example()
SystemUtil.Run "C:/type.txt", "", "", ""
Window("Text:=type.txt - Notepad").Type "happy days"
Window("Text:=type.txt - Notepad").Type micAltDwn & "F" & micAltUp
Window("Text:=type.txt - Notepad").Type micLShiftDwn & "S" & micLShiftUp
Window("Text:=type.txt - Notepad").Close
End Sub