如何将一行代码作为字符串传递而不用双引号替换每个引号? (QTP 中的 VBScript)

发布于 2024-12-01 15:43:56 字数 620 浏览 0 评论 0原文

我正在尝试创建一个 VBScript 函数(在 QuickTest Pro 10 的上下文中),该函数能够接受一行代码作为参数,例如: JavaWindow("Export Control NOTICE").JavaButton("OK ").Click

问题是,当我尝试将该行作为字符串传递时,VBScript 会因引号而卡住,这是可以理解的。问题是:我有很多行与上面的代码类似的代码,所以我想获取该行代码并按原样传递它。我不想将这些代码行的引号加倍,更不用说 QTP 记录的每个新行了。 (查找/替换全部很容易失控)

我怎样才能通过上面的代码行? 如果将其转换为字符串是可行的方法,那么我该如何对该行进行编码,以便 VBscript 不会因引号而阻塞?

此外,我还没有找到任何方法将分隔符更改为引号以外的其他内容,以将代码行指定为字符串。找到一种更改分隔符的方法可能会解决这个问题。

或者,我也尝试将 JavaWindow("Export Control NOTICE").JavaButton("OK") 作为对象传递到函数中,并且效果良好。不幸的是,似乎没有办法将该对象转回字符串,以便将 ".Click" (或存储为字符串的其他操作)重新附加到其末尾。我是否忽略了什么?

I am attempting to create a VBScript function (within the context of QuickTest Pro 10) that is able to take in a line of code as a parameter, such as: JavaWindow("Export Control Notice").JavaButton("OK").Click

Problem is, when I try passing that line as a string, VBScript understandably chokes on the quotes. Here is the catch: I have many lines of code similar to the one above, so I would like to take the line of code and pass it as is. I don't want to go around doubling the quotes for these lines of code, let along for every new line that QTP records. (Find/Replace All can easily go out of control)

How can I pass the above line of code?
If turning it into a string is the way to go, how can I encode that line so VBscript doesn't choke on the quote marks?

Additionally, I haven't been able to find any way to change the delimiter to something other than quote marks to assign the line of code as a string. Finding a way to change the delimiter would probably solve this issue.

Alternately, I've also tried passing JavaWindow("Export Control Notice").JavaButton("OK") as an object into the function and that has worked. Unfortunately, there doesn't seem to be a way to turn that object back into a string in order to append ".Click" (or some other action stored as a string) back onto the end of it. Am I overlooking something?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

糖果控 2024-12-08 15:43:56

根据您“拥有”这些代码行的具体方式(在您的头脑中、在文本文档中……)以及您是否喜欢键入或编程,您可以

  • 使用正确的转义将它们放入 VBScript 代码中( “”):

    Dim sCodeLine : sCodeLine = "JavaWindow(""导出控制通知"").JavaButton(""确定"").Click"
    WScript.Echo sCodeLine
    
    JavaWindow("导出控制通知").JavaButton("确定").Click
    
  • 使用您选择的分隔符和 Replace() 将它们放入 VBScript 代码中:

    Dim sCodeLine : sCodeLine = "JavaWindow('导出控制通知').JavaButton('确定').Click"
    WScript.Echo Replace( sCodeLine, "'", """" )
    
    JavaWindow("导出控制通知").JavaButton("确定").Click
    
  • 将它们放入外部文件(.txt、.xls、.xml、...)(resp.使用给定的文档);加载文件并将其解析为合适的数据结构(数组、字典等)。使用该集合来满足您的功能。

Depending on how exactly you 'have' those lines of code (in your head, in a text document, ...) and whether you prefer to type or to program you can

  • put them into VBScript code using the proper escape (""):

    Dim sCodeLine : sCodeLine = "JavaWindow(""Export Control Notice"").JavaButton(""OK"").Click"
    WScript.Echo sCodeLine
    
    JavaWindow("Export Control Notice").JavaButton("OK").Click
    
  • put them into VBScript code using a delimiter of your choice and Replace():

    Dim sCodeLine : sCodeLine = "JavaWindow('Export Control Notice').JavaButton('OK').Click"
    WScript.Echo Replace( sCodeLine, "'", """" )
    
    JavaWindow("Export Control Notice").JavaButton("OK").Click
    
  • put them in an external file (.txt, .xls, .xml, ...) (resp. use the given document); load and parse the file into a suitable data structure (array, dictionary, ...). Use that collection to feed your function.

心如狂蝶 2024-12-08 15:43:56

我们最终传入 QTP 对象的方式&将操作添加到函数中的目的是将 QTP 对象保留为对象,然后将操作作为字符串传递给 Select Case 进行排序。我们没有那么多操作,因此选择案例在这种情况下效果很好。
例如,以下是执行操作的代码:


Before:

If existCheck JavaWindow("Certificate Management").JavaWindow("Import Key Store").JavaEdit("* Certificate Name").Exist(2) Then
    existCheck JavaWindow("Certificate Management").JavaWindow("Import Key Store").JavaEdit("* Certificate Name").Set "Personal"
    Reporter.ReportEvent micPass,"OK button clicked on Export Control Notice","Ok"
Else Reporter.ReportEvent micFail,"Export Control Notice is not found","Step Fail" End If

After:

existCheck JavaWindow("Certificate Management").JavaWindow("Import Key Store").JavaEdit("* Certificate Name"), "Set", "Personal", Null, 2,  Null, Null

我们现在的操作仅占用一行,这对代码的可读性有很大帮助,但也使我们能够在一处改进错误检查/报告功能。


这是一些错误检查功能,减去报告部分。

功能:

'existCheck provides error-checking by determining if the object exists right before we interact with it.
'Afterwards, it reports on the results. This function has parameters for:
'object:    an object (such as a button or field)
'strAction: the action to perform on that object (such as "Click")
'strParameter1 (and two):   the parameter that is passed when the action is performed
'intWait:   the time in seconds to wait for the object to appear
'strPass:   the string to report for a success. If not string is provided, a default one will be used.
'strFail:   the string to report for a failure. If not string is provided, a default one will be used.
Function existCheck(object, strAction, strParameter1, strParameter2, intWait, strPass, strFail)

    'Before we can perform any action, we must see if the object exists. This determines much of what we do next.
    If object.Exist(intWait) Then

        'Chooses the action to be performed on the object (and then performs it)
        Select Case strAction
        Case "Click"
            object.Click
        Case "Select"
            object.Select strParameter1
        Case "Press"
            object.Press strParameter1
        Case "Set"
            object.Set strParameter1
        Case "SetSecure"
            object.SetSecure strParameter1
        Case "Type"
            object.Type strParameter1
        Case "SelectCell"
            object.SelectCell strParameter1, strParameter2
        End Select

...等等。

The way we eventually passed in QTP objects & actions into the function was to keep the QTP object as an object, then pass in the action as a string that a Select Case would sort through. We don't have that many actions, so a Select Case works well in this situation.
For example, here is the code for performing an action:


Before:

If existCheck JavaWindow("Certificate Management").JavaWindow("Import Key Store").JavaEdit("* Certificate Name").Exist(2) Then
    existCheck JavaWindow("Certificate Management").JavaWindow("Import Key Store").JavaEdit("* Certificate Name").Set "Personal"
    Reporter.ReportEvent micPass,"OK button clicked on Export Control Notice","Ok"
Else Reporter.ReportEvent micFail,"Export Control Notice is not found","Step Fail" End If

After:

existCheck JavaWindow("Certificate Management").JavaWindow("Import Key Store").JavaEdit("* Certificate Name"), "Set", "Personal", Null, 2,  Null, Null

We are now have the action occupying only one line, which helps immensely for code readability, but also allows us to improve the error-checking / reporting function all in one place.


Here is the some of the error-checking function, minus the reporting portion.

The Function:

'existCheck provides error-checking by determining if the object exists right before we interact with it.
'Afterwards, it reports on the results. This function has parameters for:
'object:    an object (such as a button or field)
'strAction: the action to perform on that object (such as "Click")
'strParameter1 (and two):   the parameter that is passed when the action is performed
'intWait:   the time in seconds to wait for the object to appear
'strPass:   the string to report for a success. If not string is provided, a default one will be used.
'strFail:   the string to report for a failure. If not string is provided, a default one will be used.
Function existCheck(object, strAction, strParameter1, strParameter2, intWait, strPass, strFail)

    'Before we can perform any action, we must see if the object exists. This determines much of what we do next.
    If object.Exist(intWait) Then

        'Chooses the action to be performed on the object (and then performs it)
        Select Case strAction
        Case "Click"
            object.Click
        Case "Select"
            object.Select strParameter1
        Case "Press"
            object.Press strParameter1
        Case "Set"
            object.Set strParameter1
        Case "SetSecure"
            object.SetSecure strParameter1
        Case "Type"
            object.Type strParameter1
        Case "SelectCell"
            object.SelectCell strParameter1, strParameter2
        End Select

... and so on.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文