开发者

How can I pass a line of code as a string without replacing every quote with doublequotes? (VBScript in QTP)

I am attempting to create a VBScript function (within the context of QuickTest Pro 10) that is able to take in a line开发者_Go百科 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?


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜