【问题标题】:Pass a string as a variable name QTP将字符串作为变量名传递 QTP
【发布时间】:2014-12-29 17:57:38
【问题描述】:

我有一个函数对我的所有脚本执行相同的操作,但只有存储 Pass-Fail 值的变量会改变。 例如,在一个脚本中 -> 状态存储在 Envrionment.Value("Current_Status") 在另一个脚本中 -> 状态存储在 DataTable.Value("Status",1) 在另一个脚本中 -> 状态存储在 objRS("AddCriteria_Status").Value

所以我正在尝试创建一个函数,其中我将这些参数作为字符串传递,然后将它们用作变量名。这是示例代码:

Envrionment.Value("Current_Status") = "none"
Environment.Value("Fail_text") = "none"
Call AddCriteria("Environment.Value(""Current_Status"")","Environment.Value(""Fail_text"")")

Pubic Function AddCriteria(varStatus,varActual)

varTemp = ""

Execute(varStatus+ "=InProgress")    'change status to InProgress by the time execution is done
Execute(varActual + "=not_defined")   'this will have the reason the case failed

....code

If varTemp = "FAIL" Then
 Execute(varStatus+ "=PASS")
 Execute(varActual + "=PASS")
Else
 Execute(varStatus+ "=FAIL")
 Execute(varActual + "=Criteria did not get added")
End If

End Function

在调用子例程时,我希望 Environment.Value("Current_Status") 的值从“none”更改为“InProgress”,然后更改为“PASS” 但是在执行“Execute”命令后,Environment 变量变为空。

由于 VBScript 不支持 CVar,我无法使用它。

我尝试了 Eval,但它在另一个方向上不起作用,即: 如果更改 Environment.Value("Current_Status") 的值,则 Eval(varStatus) 的值会发生变化,但我找不到更改 Eval(varStatus) 值的方法,以便 Environment.Value(" Current_Status") 更改。

请帮忙。我被困了一个星期。

!!!我正在努力完成的事情!!!

在 .vbs 文件中,将任何字符串作为参数传递给函数;并将其转换为该函数中的变量名。简单示例:将字符串“abc”作为参数传递给函数 -> 在该函数中,将字符串转换为变量名以存储值 [例如,abc = “PASS”]

!!!我是如何尝试的!!!

我尝试使用 Execute 命令,因为这是我从上一篇文章中获得的解决方案 [vbscript Eval a string to a Variable in a loop?

使用“CVar”是一种方法,但在 VBScript 中不受支持。所以我的想法用完了

!!!我遇到的问题!!!

老实说,我不明白使用“执行”的逻辑,但我还是尝试了。可悲的是,它没有成功。使用执行命令时(如代码中所述),环境变量为空。

【问题讨论】:

    标签: vbscript parameter-passing eval qtp execute


    【解决方案1】:

    想法:

    • 使用ExecuteGlobal来执行你要执行的赋值—— 如果那是你想要的。 Eval 尤其是 Execute 对他们的生活范围有微妙的限制。

    • 目标变量(即在 ExecuteGlobal) 评估的分配必须是一个全局 变量。

    • 如果 ExecuteGlobal 调用发生在 Action 的全局范围内,则 目标变量也必须在那里声明。 (我认为。)

    • 如果ExecuteGlobal 调用发生在函数的例程中 库,目标变量也必须在那里声明。 (我肯定知道。但请继续阅读。)

    为了进一步帮助您,我需要更新您的问题,因为不清楚您想要完成什么,以及您看到什么问题。因为 -- Eval 不改变值,它只是计算作为字符串提供的表达式,并返回它的值。如果表达式有副作用,比如设置一个全局变量,那么你可能不走运,因为......嗯......这取决于该全局变量在哪里声明和初始化(如果有的话),以及在哪里ExecuteGlobal 调用发生。 动作和库不共享一个全局范围,即使它们看起来确实如此,这会产生很多奇怪的行为。

    但正如我所说,如果你澄清你想要完成什么(得到了 90%),你是如何尝试去做的(得到了 40%),以及你面临什么问题(得到了 10% ),我确信我可以更新这个答案,以便它接近解决方案。

    ** 更新**

    我将这个库代码用于所有运行时表达式评估,无论是来自库还是动作:

    ' Interpret (execute) a piece of VSH source code consisting of statements -- success?
    '   Code: String containing VBS source code. Passed by reference for performance reasons only
    Public Function ExecCode (ByRef Code)
        Dim ErrNumber
        Dim ErrDescription
    
        On error resume next ' Avoid getting kicked out by errors in the code contained in Code
        ExecuteGlobal Code
        ErrNumber=Err.Number
        ErrDescription=Err.Description
        On error goto 0 ' Re-enable RTE handling
    
        If ErrNumber <> 0 Then
            ExecCode=false
    
            Print "Code execution failed ('" & ErrDescription & "'), code:" & vbNewline & Code & "<eof>"
        else
            ExecCode=true
        End If
    End Function
    
    Dim GlobalVar
    
    ' Interpret (execute) a piece of VSH source code consisting of a single expression -- success?
    '   Expr; String containing a VBS expression. Passed by reference for performance reasons only.
    '   Target: Variable receiving the value to which the expression evaluates
    Public Function EvalCodeAndAssign (ByRef Expr, ByRef Target)
    
        ' In order to force "Option explicit", we don´t use Eval, but ExecCode (and thus ExecuteGlobal):
        Dim Code: Code="Option Explicit: GlobalVar=(" & Expr & ")"
        Dim Result: Result=ExecCode (Code)
        If Result Then
            Target=GlobalVar
        End If
        EvalCodeAndAssign=Result
    
    End Function
    

    更新 2:如果您传递给 ExecuteGlobal 的语句包含引号(我认为您的代码中缺少引号),则必须引用,即你必须使用双引号,比如在

    ExecuteGlobal "x=""This is a string"""
    

    因为 ExecuteGlobal/Execute/Eval 所做的是:获取一个字符串并将其解释为 VBScript 代码。 由于缺少引号,您尝试使用的代码无效。

    【讨论】:

      猜你喜欢
      • 2011-12-07
      • 2020-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-13
      • 2014-06-16
      • 2020-04-28
      • 2018-06-21
      相关资源
      最近更新 更多