用QTP来操作DOS命令行
上一篇 / 下一篇 2009-03-18 14:18:23 / 个人分类:QTP
Objectives
The objectives of this article are :
- Learn how to
use DOS command lines in QTP.
- Alternative
complex tasks.
- Alternative
.NET methods.
The following content is viewable because you are
logged-in.
Thanks for registering!
cmd.exe allows access to the Microsoft Windows Command Prompt, also known
as Microsoft DOS.
To-date, cmd.exe is a 32-bit command prompt used in Windows NT, 2000, and XP
and offers disk and file maintenance functions to your computer as well as
network functions. cmd.exe can be found underSystem32folder.
SystemUtil.Run"cmd.exe","","C:\WINDOWS\system32″,"open"
Capture the window by QTP
Window("object class:=ConsoleWindowClass")
Print Window("object class:=ConsoleWindowClass").GetROProperty("title")
Typing to DOS window
TheTypemethod types the specified string in the window.
Syntax:object.Type KeyboardInput.
KeyboardInput: A String value.
The text string and/or constants representing non-alphanumeric keys.
Window("object class:=ConsoleWindowClass").Type"
Window("object class:=ConsoleWindowClass").Type micReturn
Catching the Response
TheGetVisibleTextmethod returns the text from the specified area.
The text to capture must be visible in the application window when the step
runs. The area is defined by pairs of coordinates that designate two diagonally
opposite corners of a rectangle.
Syntax :object.GetVisibleText ([Left], [Top], [Right],
[Bottom])
txt = Window("object class:=ConsoleWindowClass").GetVisibleText()
print txt
Problems…
SystemUtil.Run"cmd.exe","","C:\WINDOWS\system32″,"open"
Window("object class:=ConsoleWindowClass").Type"dir"
Window("object class:=ConsoleWindowClass").Type micReturn
txt = Window("object class:=ConsoleWindowClass").GetVisibleText()
print txt
The GetVisibleText method "catches" only the
text that is visible. large commands with large responses are useless…
Solution
Wscript.Shell Object
TheWSript.Shellobject provides functions to read system
information and environment variables, work with the registry and manage
shortcuts. You create aWSript.Shellobject whenever you want to run a
program locally, manipulate the contents of the registry, create a shortcut, or
access a system folder.
DimwShell
SetwShell =
CreateObject("WScript.Shell")
you will see this detailed description if you are using
PDM.DLL version 9
Exec Method
Runs an application in a child command-shell, providing access to the
StdIn/StdOut/StdErr streams.
Syntax :object.Exec(strCommand)
strCommand :String value
indicating the command line used to run the script. The command line should
appear exactly as it would if you typed it at the command prompt.
TheExecmethod returns aWshScriptExecobject, which
provides status and error information about a script. run withExecalong
with access to theStdIn,StdOut, andStdErrchannels.
%compsec%
What if your colleague installedMS-Windowson D:\ Drive?
–> The C:\Windows\System32\cmd.exe will not work.
Since every machine can have different installation directory, the
environment %compsec% returns the full path of the cmd.exe file.
Executing a command
- /C– Carries out the command specified by string
and then terminates.
- /K– Carries out the command specified by string
but remains.
Executing the command
DimwShell, exec
SetwShell =
CreateObject("WScript.Shell")
Setexec =
wShell.Exec("%comspec% /C ping
advancedqtp.com")
WshScriptExec Object
Provides status information about a script. run withExecalong with
access to theStdIn,StdOut, andStdErrstreams.
WshScriptExec.Status Property
Provides status information about a script. run with theExec()
method.
- WshRunning (
= 0 )- The job is still running.
- WshFinished (
= 1 )- The job has completed.
WshScriptExec.ExitCode Property
Returns the exit code set by a script. or program run using theExec()
method. Executables set an exit code when they finish running. This conveys the
status information when a process ends. Often, it is used to send an error code
(or some other piece of information) back to the caller. If the process has not
finished, theExitCodeproperty returns 0. The values returned fromExitCodedepend on the application that was called.
WshScriptExec.StdOut Property
TheStdOutproperty contains a read-only copy of any information
the script. may have sent to the standard output.
- AtEndOfStream- Returns a Boolean value indicating whether the
end of an input stream has been reached.
- ReadAll- Returns all characters from a stream.
- ReadLine- Reads an entire line from a stream.
Handling Errors
Run the following code. why is the result empty?
DimwShell, exec
SetwShell =
CreateObject("WScript.Shell")
Setexec =
wShell.Exec("%comspec% /C png
advancedqtp.com")
print exec.StdOut.ReadAll
WshScriptExec.StdErr Property
since the "png" command has a syntax error (ping), the
STDOUT remains empty.
Now, run the following code :
DimwShell, exec
SetwShell =
CreateObject("WScript.Shell")
Setexec =
wShell.Exec("%comspec% /C png
advancedqtp.com")
print exec.StdErr.ReadAll
Since we have a syntax error, the STDERR buffer is filled, instead of the
STDOUT. but, what about a "bad" command ( without syntax errors )?
DimwShell, exec
SetwShell =
CreateObject("WScript.Shell")
Setexec =
wShell.Exec("%comspec% /C ping no-exists")
print exec.StdOut.ReadAll
in this case STDOUT will be filled, but not STDERR.
How could we know when an error occurred?
DimwShell,
exec, outStr
SetwShell =
CreateObject("WScript.Shell")
Setexec =
wShell.Exec("%comspec% /C ping
arrdvancedqtp.com")
DoWhileexec.Status = 0
IfNotexec.StdOut.AtEndOfStreamThen
outStr = exec.StdOut.ReadAll
Ifexec.ExitCode = 1then
Reporter.ReportEvent micWarning,"Command failed", outStr
EndIf
ExitDo
EndIf
IfNotexec.StdErr.AtEndOfStreamThen
outStr ="STDERR: "& exec.StdErr.ReadAll
Reporter.ReportEvent micFail,"Command
failed", outStr
ExitDo
EndIf
Wait
1
Print outStr
Alternative .NET
Setnt =
DotNetFactory.CreateInstance("Microsoft.VisualBasic.Devices.Network")
IfCBool( nt.Ping("www.advancedqtp.com") )Then
MsgBox("Server pinged successfully.")
Else
MsgBox("Ping request timed out.")
TAG: