1

嗨,我有一个旧的 VBscript,里面有一个已实现的函数。

此函数包含一个对象,该对象可以执行和停止名为 QTP 的程序以及针对该程序的其他更高级的部分。

因为我无法在 Powershell 中找到任何方法来执行此操作,所以我想为什么不重用旧 VBscript 文件中的那个函数。事实证明这比我想象的要复杂一些。

因为我不能使用 MSScriptControl.ScriptControl 来访问我的 VBscript,所以我想也许我可以在 .net 中找到更可取的 C# 的东西,但我发现的几种方法可以执行整个脚本,而不仅仅是一个函数。例如:

System.Diagnostics.Process.Start(@"cscript //B //Nologo c:\yourfile.vbs");

所以我现在在这里问我如何从我的 Powershell 文件中执行该函数或将其转换为 Powershell 这是函数代码:

Public Sub ExecuteTest(ByVal TestPath , ByVal TestName)
 Dim qtApp 'As QuickTest.Application ' Declare the Application object variable
 Dim qtTest 'As QuickTest.Test ' Declare a Test object variable
 Dim qtResultsOpt 'As QuickTest.RunResultsOptions ' Declare a Run Results Options object variable
 strLog = "Start " & TestName
 WriteLogToFile(strLog)
 Set qtApp = CreateObject("QuickTest.Application") ' Create the Application object
 qtApp.Launch ' Start QuickTest
 qtApp.Visible = False ' Make the QuickTest application invisible
 qtApp.Visible = True ' Make the QuickTest application visible

 ' Set QuickTest run options: 
 ' Always--Captures images for all steps in the run. 
 ' OnError--Captures images only for failed steps. 
 ' OnWarning--Captures images only for steps that return a failed or warning status. 
 ' Never--Never captures images. 

   qtApp.Options.Run.ImageCaptureForTestResults = "Always"
 ' qtApp.Options.Run.ImageCaptureForTestResults = "OnError"
 ' qtApp.Options.Run.ImageCaptureForTestResults = "OnWarning"
 ' qtApp.Options.Run.ImageCaptureForTestResults = "Never"


 qtApp.Options.Run.RunMode = "Fast"
 qtApp.Options.Run.ViewResults = False

 'This will Open a script
 ' Wscript.Echo  TestPath
  qtApp.Open TestPath , False ' Open the test in edit mode
 ' qtApp.Open TestPath , True ' Open the test in read-only mode

 ' set run settings for the test

 Set qtTest = qtApp.Test

 ' qtTest.Settings.Run.IterationMode = "rngIterations" ' Run only iterations 2 to 4
 ' qtTest.Settings.Run.StartIteration = 1
 ' qtTest.Settings.Run.EndIteration = 1
 ' qtTest.Settings.Run.OnError = "NextStep" ' Instruct QuickTest to perform next step when error occurs

 'For Viewing Results
 Set qtResultsOpt = CreateObject("QuickTest.RunResultsOptions") ' Create the Run Results Options object

 ' WScript.Echo  letter & testresult & TestName & "\Result_"  & GetDateTimeStr

 qtResultsOpt.ResultsLocation = letter & testresult & TestName & "\Result_"  & GetDateTimeStr ' Set the results location
 qtTest.Run qtResultsOpt ' Run the test

 While qtTest.IsRunning
   'Wait For Test To Finish
 Wend

 strLog = "Slut " & TestName  &  qtTest.LastRunResults.Status 

 ' qtApp.Options.Run.ViewResults = True
 WriteLogToFile(strLog)


 'Close QTP
 qtTest.Close() ' Close the test

 'Set the options to nothing
 Set qtResultsOpt = Nothing ' Release the Run Results Options object
 Set qtTest = Nothing ' Release the Test object
 Set qtApp = Nothing ' Release the Application object

End Sub 

感谢您的帮助,我在这里使用了我从大卫马丁那里得到的东西,并找到了解决隐形方法和属性问题的方法。这是工作方法:

  function ExecuteTest($TestPath,$letter,$testresult,$TestName)
{
    #Update-TypeData -Prepend .\ComObject.Types.ps1xml
    #param($TestPath=[string],$TestName=[string])

    $qtApp = New-Object -comobject QuickTest.Application -strict

    $strLog = "Start $TestName" 
    WriteLogToFile($strLog)
    # Start QuickTest
    [System.__ComObject].InvokeMember(“Launch”,[System.Reflection.BindingFlags]::InvokeMethod,$null,$qtApp,$null,$null,$null,$null)

    Start-Sleep -Seconds 10
    $obj = New-Object System.Object
    # Make the QuickTest application invisible
    $obj = $False
    #Make the QuickTest application visible
    $obj = $True
    $myArray = @($nul)
    $myArray[0] =$obj
    #Wait for the aplication to be initialized 

    #Apply the Visibility Variable to the QuickTest application
    [System.__ComObject].InvokeMember("Visible",[System.Reflection.BindingFlags]::SetProperty,$null,$qtApp,$myArray)

    #Make the QuickTest application visible
    #$qtApp.Visible = $True 



    # Set QuickTest run options: 
    # Always--Captures images for all steps in the run. 
    # OnError--Captures images only for failed steps. 
    # OnWarning--Captures images only for steps that return a failed or warning status. 
    # Never--Never captures images. 

    $qtApp.Options.Run.ImageCaptureForTestResults = "Always"
    # $qtApp.Options.Run.ImageCaptureForTestResults = "OnError"
    # $qtApp.Options.Run.ImageCaptureForTestResults = "OnWarning"
    # $qtApp.Options.Run.ImageCaptureForTestResults = "Never"
    $obj = "Fast"
    $myArray[0] =$obj
    [System.__ComObject].InvokeMember("RunMode",[System.Reflection.BindingFlags]::SetProperty,$null,$qtApp.Options.Run,$myArray)

    #$qtApp.Options.Run.RunMode = "Fast"

    #$qtApp.Options.Run.ViewResults = $False
    $obj = $False
    $myArray[0] =$obj
    [System.__ComObject].InvokeMember("ViewResults",[System.Reflection.BindingFlags]::SetProperty,$null,$qtApp.Options.Run,$myArray)

    #This will Open a script
    # Wscript.Echo  TestPath
    $obj = $False
    $myArray[0] = $TestPath
    $myArray += $obj
    #Invoke-Method - inputobject $qtApp -MethodName Open -MethodParameters $myArray -Static
    $qtApp.GetType().InvokeMember(“Open”,#Method Name
        [System.Reflection.BindingFlags]::InvokeMethod,#BindingFlag
        $null,#Binder
        $qtApp,#Target
        ($TestPath),#Args
        $null,#Modifiers
        $null,#culture
        $null) #[string[]]("TestPath"))#namedParameters
  # [System.__ComObject].InvokeMember(“Open”,[System.Reflection.BindingFlags]::InvokeMethod,$null,$qtApp,([Object[]]$myArray),$null,$null,$null)
    #$qtApp.Open #$TestPath, $False #' Open the test in edit mode
    # $qtApp.Open $TestPath , $True #' Open the test in read-only mode
    # set run settings for the test
    $qtTest = $qtApp.GetType().InvokeMember("Test",[System.Reflection.BindingFlags]::GetProperty,$null,$qtApp,$null)

    # $qtTest.Settings.Run.IterationMode = "rngIterations" ' Run only iterations 2 to 4
    # $qtTest.Settings.Run.StartIteration = 1
    # $qtTest.Settings.Run.EndIteration = 1
    # $qtTest.Settings.Run.OnError = "NextStep" ' Instruct QuickTest to perform next step when error occurs

    # WScript.Echo  letter & testresult & TestName & "\Result_"  & GetDateTimeStr

    # The following variables have not been defined
    #letter
    #testresult
    # This function doesn't exist
    #GetDateTimeStr
    $qtResultsOpt = New-Object -comobject QuickTest.RunResultsOptions -strict
    #$qtResultsOpt = FileSystemObject QuickTest.RunResultsOptions
    $ParentPath = $letter + $testresult + $TestName 
    $ChildPath = "\Result_" + $("{0:yyyymmddTHHMMss}" -f $(get-date))
    $ResultOptArgs = join-path -path $ParentPath -childpath  $ChildPath
    [System.__ComObject].InvokeMember(“ResultsLocation”,[System.Reflection.BindingFlags]::SetProperty,$null,$qtResultsOpt,$ResultOptArgs)
    #$qtResultsOpt.GetType().InvokeMember(“ResultsLocation”,#Method Name
     #   [System.Reflection.BindingFlags]::InvokeMethod,#BindingFlag
      #  $null,#Binder
       # $qtResultsOpt,#Target
        #($ResultOptArgs),#Args
       # $null,#Modifiers
        #$null,#culture
       # $null) #[string[]]("TestPath"))#namedParameters


    # Run the test
    $qtTest.GetType().InvokeMember(“Run”,[System.Reflection.BindingFlags]::InvokeMethod,$null,$qtTest,($qtResultsOpt),$null,$null,$null)
    #$qtTest.Run # $qtResultsOpt 


    do
    {
        Start-Sleep -m 100
    }
    while($qtTest.IsRunning)

    $strLog = "Slut $TestName $(qtTest.LastRunResults.Status)"

    # qtApp.Options.Run.ViewResults = True
    WriteLogToFile($strLog)


    #Close QTP
    #$qtTest.Close() 
    [System.__ComObject].InvokeMember(“Close”,[System.Reflection.BindingFlags]::InvokeMethod,$null,$qtTest,$null,$null,$null,$null)
    #Set the options to nothing
    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($qtResultsOpt)
    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($qtTest)
    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($qtApp)
}
4

1 回答 1

2

我已经快速为你翻译了这个,我没有 QuickTest,所以我不能为你测试这个。

function WriteLogToFile
{
    param ($message)
    write-host message
}

function ExecuteTest
{
    param
    (
        $TestPath,
        $TestName
    )

    $qtApp = New-Object -comobject QuickTest.Application -strict
    $qtTest = New-Object -comobject QuickTest.Test -strict
    $qtResultsOpt = New-Object -comobject QuickTest.RunResultsOptions

    $strLog = "Start $TestName" 
    WriteLogToFile($strLog)

    # Start QuickTest
    $qtApp.Launch 

    # Make the QuickTest application invisible
    $qtApp.Visible = $false 

    #Make the QuickTest application visible
    $qtApp.Visible = True 


    # Set QuickTest run options: 
    # Always--Captures images for all steps in the run. 
    # OnError--Captures images only for failed steps. 
    # OnWarning--Captures images only for steps that return a failed or warning status. 
    # Never--Never captures images. 

    $qtApp.Options.Run.ImageCaptureForTestResults = "Always"
    # $qtApp.Options.Run.ImageCaptureForTestResults = "OnError"
    # $qtApp.Options.Run.ImageCaptureForTestResults = "OnWarning"
    # $qtApp.Options.Run.ImageCaptureForTestResults = "Never"

    qtApp.Options.Run.RunMode = "Fast"
    qtApp.Options.Run.ViewResults = False

    #This will Open a script
    write-host "TestPath: [$TestPath]"
    $qtApp.Open $TestPath , $false ' Open the test in edit mode
    # $qtApp.Open TestPath , True ' Open the test in read-only mode

    # set run settings for the test
    $qtTest = $qtApp.Test

    # $qtTest.Settings.Run.IterationMode = "rngIterations" ' Run only iterations 2 to 4
    # $qtTest.Settings.Run.StartIteration = 1
    # $qtTest.Settings.Run.EndIteration = 1
    # $qtTest.Settings.Run.OnError = "NextStep" ' Instruct QuickTest to perform next step when error occurs

    # WScript.Echo  letter & testresult & TestName & "\Result_"  & GetDateTimeStr

    # The following variables have not been defined
    #letter
    #testresult
    # This function doesn't exist
    #GetDateTimeStr

    $qtResultsOpt.ResultsLocation = join-path -path "c:\results" -childpath $("{0:yyyymmddTHHMMss}" -f $(get-date))

    # Run the test
    $qtTest.Run qtResultsOpt 


    do
        Start-Sleep -m 100
    while($qtTest.IsRunning)

    $strLog = "Slut $TestName $(qtTest.LastRunResults.Status)"

    # qtApp.Options.Run.ViewResults = True
    WriteLogToFile($strLog)


    #Close QTP
    $qtTest.Close() 

    #Set the options to nothing
    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($qtResultsOpt)
    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($qtTest)
    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($qtApp)
}
于 2013-07-29T13:46:06.783 回答