Objectives
The objectives of this article are :
- Learn how to use DOS command lines in QTP.
- Alternative complex tasks.
- Alternative .NET methods.
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 under System32 folder.
SystemUtil.Run <span style="color: #006080;">"cmd.exe"</span>,<span style="color: #006080;">""</span>,<span style="color: #006080;">"C:\WINDOWS\system32"</span>,<span style="color: #006080;">"open"</span>
Capture the window by QTP
Window( <span style="color: #006080;">"object class:=ConsoleWindowClass"</span> )
Print Window( <span style="color: #006080;">"object class:=ConsoleWindowClass"</span> ).GetROProperty( <span style="color: #006080;">"title"</span> )
Typing to DOS window
The Type method 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( <span style="color: #006080;">"object class:=ConsoleWindowClass"</span> ).Type <span style="color: #006080;">"Ping advancedqtp.com"</span>
Window( <span style="color: #006080;">"object class:=ConsoleWindowClass"</span> ).Type micReturn
Catching the Response
The GetVisibleText method 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( <span style="color: #006080;">"object class:=ConsoleWindowClass"</span> ).GetVisibleText()
print txt
Problems…
SystemUtil.Run <span style="color: #006080;">"cmd.exe"</span>,<span style="color: #006080;">""</span>,<span style="color: #006080;">"C:\WINDOWS\system32"</span>,<span style="color: #006080;">"open"</span>
Window( <span style="color: #006080;">"object class:=ConsoleWindowClass"</span> ).Type <span style="color: #006080;">"dir"</span>
Window( <span style="color: #006080;">"object class:=ConsoleWindowClass"</span> ).Type micReturn
txt = Window( <span style="color: #006080;">"object class:=ConsoleWindowClass"</span> ).GetVisibleText()
print txt
The GetVisibleText method “catches” only the text that is visible. large commands with large responses are useless…
Solution
Wscript.Shell Object
The WSript.Shell object provides functions to read system information and environment variables, work with the registry and manage shortcuts. You create a WSript.Shell object whenever you want to run a program locally, manipulate the contents of the registry, create a shortcut, or access a system folder.
<span style="color: #0000ff;">Dim</span> wShell
<span style="color: #0000ff;">Set</span> wShell = CreateObject( <span style="color: #006080;">"WScript.Shell"</span> )
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.
The Exec method returns a WshScriptExec object, which provides status and error information about a script run with Exec along with access to the StdIn, StdOut, and StdErr channels.
%compsec%
What if your colleague installed MS-Windows on 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
<span style="color: #0000ff;">Dim</span> wShell, exec
<span style="color: #0000ff;">Set</span> wShell = CreateObject( <span style="color: #006080;">"WScript.Shell"</span> )
<span style="color: #0000ff;">Set</span> exec = wShell.Exec( <span style="color: #006080;">"%comspec% /C ping advancedqtp.com"</span> )
WshScriptExec Object
Provides status information about a script run with Exec along with access to the StdIn, StdOut, and StdErr streams.
WshScriptExec.Status Property
Provides status information about a script run with the Exec() 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 the Exec() 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, the ExitCode property returns 0. The values returned from ExitCode depend on the application that was called.
WshScriptExec.StdOut Property
The StdOut property 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?
<span style="color: #0000ff;">Dim</span> wShell, exec
<span style="color: #0000ff;">Set</span> wShell = CreateObject( <span style="color: #006080;">"WScript.Shell"</span> )
<span style="color: #0000ff;">Set</span> exec = wShell.Exec( <span style="color: #006080;">"%comspec% /C png advancedqtp.com"</span> )
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 :
<span style="color: #0000ff;">Dim</span> wShell, exec
<span style="color: #0000ff;">Set</span> wShell = CreateObject( <span style="color: #006080;">"WScript.Shell"</span> )
<span style="color: #0000ff;">Set</span> exec = wShell.Exec( <span style="color: #006080;">"%comspec% /C png advancedqtp.com"</span> )
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 )?
<span style="color: #0000ff;">Dim</span> wShell, exec
<span style="color: #0000ff;">Set</span> wShell = CreateObject( <span style="color: #006080;">"WScript.Shell"</span> )
<span style="color: #0000ff;">Set</span> exec = wShell.Exec( <span style="color: #006080;">"%comspec% /C ping no-exists"</span> )
print exec.StdOut.ReadAll
in this case STDOUT will be filled, but not STDERR.
How could we know when an error occurred?
<span style="color: #0000ff;">Dim</span> wShell, exec, outStr
<span style="color: #0000ff;">Set</span> wShell = CreateObject( <span style="color: #006080;">"WScript.Shell"</span> )
<span style="color: #0000ff;">Set</span> exec = wShell.Exec( <span style="color: #006080;">"%comspec% /C ping arrdvancedqtp.com"</span> )
<span style="color: #0000ff;">Do</span> <span style="color: #0000ff;">While</span> exec.Status = 0
<span style="color: #0000ff;">If</span> <span style="color: #0000ff;">Not</span> exec.StdOut.AtEndOfStream <span style="color: #0000ff;">Then</span>
outStr = exec.StdOut.ReadAll
<span style="color: #0000ff;">If</span> exec.ExitCode = 1 <span style="color: #0000ff;">then</span>
Reporter.ReportEvent micWarning, <span style="color: #006080;">"Command failed"</span>, outStr
<span style="color: #0000ff;">End</span> <span style="color: #0000ff;">If</span>
<span style="color: #0000ff;">Exit</span> <span style="color: #0000ff;">Do</span>
<span style="color: #0000ff;">End</span> <span style="color: #0000ff;">If</span>
<span style="color: #0000ff;">If</span> <span style="color: #0000ff;">Not</span> exec.StdErr.AtEndOfStream <span style="color: #0000ff;">Then</span>
outStr = <span style="color: #006080;">"STDERR: "</span> & exec.StdErr.ReadAll
Reporter.ReportEvent micFail, <span style="color: #006080;">"Command failed"</span>, outStr
<span style="color: #0000ff;">Exit</span> <span style="color: #0000ff;">Do</span>
<span style="color: #0000ff;">End</span> <span style="color: #0000ff;">If</span>
Wait 1
<span style="color: #0000ff;">Loop</span>
Print outStr
Alternative .NET
<span style="color: #0000ff;">Set</span> nt = DotNetFactory.CreateInstance( <span style="color: #006080;">"Microsoft.VisualBasic.Devices.Network"</span> )
<span style="color: #0000ff;">If</span> <span style="color: #0000ff;">CBool</span>( nt.Ping( <span style="color: #006080;">"www.advancedqtp.com"</span> ) ) <span style="color: #0000ff;">Then</span>
MsgBox( <span style="color: #006080;">"Server pinged successfully."</span> )
<span style="color: #0000ff;">Else</span>
MsgBox( <span style="color: #006080;">"Ping request timed out."</span> )
<span style="color: #0000ff;">End</span> <span style="color: #0000ff;">If</span>
Demo
Print a list of “.exe” files under System32 without header information in lower case order by file name descending.
<span style="color: #0000ff;">Option</span> Explicit
<span style="color: #0000ff;">Const</span> SystemFolder = 1
<span style="color: #0000ff;">Dim</span> wShell, exec, fso
<span style="color: #0000ff;">Dim</span> dirList
<span style="color: #0000ff;">Set</span> wShell = CreateObject( <span style="color: #006080;">"WScript.Shell"</span> )
<span style="color: #0000ff;">Set</span> fso = CreateObject(<span style="color: #006080;">"Scripting.FileSystemObject"</span>)
dirList = fso.GetSpecialFolder( SystemFolder ) & <span style="color: #006080;">"\*.exe"</span>
<span style="color: #0000ff;">Set</span> exec = wShell.Exec( <span style="color: #006080;">"%comspec% /C dir "</span> & dirList & <span style="color: #006080;">" /B /O-N /L"</span> )
<span style="color: #0000ff;">Do</span> <span style="color: #0000ff;">While</span> <span style="color: #0000ff;">True</span>
<span style="color: #0000ff;">If</span> <span style="color: #0000ff;">Not</span> exec.StdOut.AtEndOfStream <span style="color: #0000ff;">Then</span>
dirList = exec.StdOut.ReadAll
<span style="color: #0000ff;">If</span> exec.ExitCode = 1 <span style="color: #0000ff;">then</span>
Reporter.ReportEvent micWarning, <span style="color: #006080;">"Command failed"</span>, dirList
<span style="color: #0000ff;">End</span> <span style="color: #0000ff;">If</span>
<span style="color: #0000ff;">Exit</span> <span style="color: #0000ff;">Do</span>
<span style="color: #0000ff;">End</span> <span style="color: #0000ff;">If</span>
<span style="color: #0000ff;">If</span> <span style="color: #0000ff;">Not</span> exec.StdErr.AtEndOfStream <span style="color: #0000ff;">Then</span>
dirList = <span style="color: #006080;">"STDERR: "</span> & exec.StdErr.ReadAll
Reporter.ReportEvent micFail, <span style="color: #006080;">"Command failed"</span>, dirList
<span style="color: #0000ff;">Exit</span> <span style="color: #0000ff;">Do</span>
<span style="color: #0000ff;">End</span> <span style="color: #0000ff;">If</span>
Wait 1
<span style="color: #0000ff;">Loop</span>
Print dirList



Recent Comments