专注于自动化测试的培训、自动化测试解决方案、兼职自动化测试项目,欢迎志同道合的朋友一起合作共赢未来!电子信箱:yang-zhengjiang@163.com

The different of run and exec method

上一篇 / 下一篇  2009-04-07 13:05:16 / 个人分类:qtp

the run and exec method are very important method, I found a good article to introduce the two method, so I decided to share it, I hope this will help you if you need it, the detail as follows:
The fact that there are two ways to run programs from a script. leads to an obvious question: which method should you use in your scripts? The answer to that
question depends on the script. and what it needs to accomplish.
script. can use either the Run method or the Exec method to run a program in a
manner similar to using the Run dialog box from the Start menu. Regardless of the
method used, the program starts, and runs in a new process.
However, when you use the Run method, your script. will not have access to the
standard input, output, and error streams generated by the program being run. A
script. cannot use the Run method to run a command-line tool and retrieve its
output.
For example, suppose you want to run Ping.exe and then examine the output to
see whether the computer could be successfully contacted. This cannot be done
using the Run command. Instead, you would need to ping the computer, save the
results of the ping command to a text file, open the text file, read the results, and
then parse those results to determine the success or failure of the command.
The following script. uses the Run method to call Ping.exe, redirecting the output
to a temporary file. The script. opens and reads the text file, checks to see whether
the command succeeded (by determining whether any of the lines of output begin
with the word Reply), and then closes and deletes the temporary file:

Option Explicit
Dim oShell, oFSO, oTextFile
Dim sTempName, sText
Dim sComputer : sComputer = "62.241.53.16"
'--- Creating a shell object
Set Shell = CreateObject("WScript.Shell")
Set FSO = CreateObject("Scripting.FileSystemObject")
sTempName = oFSO.GetTempName
oShell.Run "%comspec% /c ping -n 3 -w 1000 " & sComputer & " > " & sTempName, 0, True
'--- Reading from output file
Set TextFile = oFSO.OpenTextFile(sTempName, 1)
Do While oTextFile.AtEndOfStream <> True
sText = oTextFile.ReadLine
If Instr(sText, "Reply") > 0 Then
MsgBox "Reply received."
Exit Do
End If
Loop
'--- Closing and deleting temporary file
oTextFile.Close : oFSO.DeleteFile(sTempName)
'--- Cleaning used objects
Set TextFile = Nothing : Set FSO = Nothing
Set Shell = Nothing

Although this approach works, it is somewhat complicated. If you need access to
command-line output, you should use the Exec method instead. The following
script. also parses the output generated by Ping.exe. However, it does so by using
the Exec method and by directly reading the output. There is no need to create,
open, read, and delete a temporary file, and the script. is only 9 lines long,
compared with the 15 lines required to perform. this same task using the Run
method:

Option explicit
Dim oShell, oExec
Dim sText
Dim sComputer : sComputer = "62.241.53.16"
'--- Creating a shell object
Set Shell = CreateObject("WScript.Shell")
Set Exec = oShell.Exec("cmd /c ping -n 3 -w 1000 " & sComputer)
'--- Reading from output stream
Do While Not oExec.StdOut.AtEndOfStream
sText = oExec.StdOut.ReadLine()
If Instr(sText, "Reply") > 0 Then
MsgBox "Reply received."
Exit Do
End If
Loop
'--- Cleaning used objects
Set Shell = Nothing

In many respects, this makes the Exec method a better choice than the Run
method. However, the Run method is still useful in a number of situations:
You might want to run the application in a specified window type, such as a
minimized window. Exec offers no control over window style; Run offers
Chapter 09 Scripting Quicktest Professional Page 43
Dani Vainstein Windows Script. Host Page 43 of 73
more options.
You might need to run a script. on computers that do not have WSH 5.6
installed. Exec is supported only on WSH 5.6.
You might want to wait for the application being called to finish running
before the script. resumes. This can be done with either Run or Exec but
requires less coding with Run.

TAG: and Run run exec method

 

评分:0

我来说两句

Open Toolbar