Wednesday, May 21, 2008

VBScript Syntax Checking


This post describes how to use VBScript to check the syntax of a given block of VBScript code.


The Motivation:


At APT, one software suite we use to run automated GUI tests of our web-based software is Mercury QuickTest Professional. QuickTest runs tests written in VBScript that click through a prescribed path in our software to make sure we don't throw any errors.

Rather than store hundreds of static .vbs files containing the definition of tests, we store our test definitions in a SQL database and dynamically generate the corresponding .vbs files on demand. We do this for a few reasons:

  1. It makes test maintenance easier (you maintain only the structure of the test, not the code's syntax)
  2. It enables users who are unfamiliar with VBS to author and maintain tests
  3. By introducing separation between the test's definition and execution, we can change how we execute our test without changing our test's definition (for example, migrating test execution to another platform such as Selenium, which can execute different languages)


The Problem:


One of the challenges of dynamically generating code is making sure that what you generate is syntactically valid. Making the assumption that your generated code will be perfect is generally a bad idea, because:

  • Your code-generation code may contain errors
  • As you modify your test definition model, the code-generation code can fall out of sync, resulting in invalid generated code
  • You'll likely want to support user-entered "custom" sections of code, which you can't assume will be syntactically valid


The Solution:


After we generate the VBS code for the requested test, we check its syntax prior to executing it by calling the function defined below:





Public Function checkVBSSyntax(vbsCode)
    ' Save any pre-existing error so we can revert to it
    oldErr = Err
    Err.Number = 0

    On Error Resume Next
    ' Execute the vbs code to see if it throws an error
    ExecuteGlobal vbsCode
    hasError = Err.Number <> 0

    On Error Goto 0

    ' Revert to the pre-existing error
    Err = oldErr

    checkVBSSyntax = (Not hasError)
End Function




What we're doing here is simply executing the questionable code in the global scope to see if it throws an error. If it throws an error, we assert that it must be due to a syntax error.

A couple of notes:


  • This function assumes that the questionable code contains only Function and Sub definitions; that is, no code exists outside of a Function or Sub declaration. If it does contain code outside of a Function or Sub declaration, that code will actually be executed (not just checked for its syntax). If you have such code, wrap it in a Public Sub main() ... End Sub statement.

  • This has the side effect of defining all the Functions / Subs in your questionable code in global scope; that is, after calling this function, you'll be able to call any functions defined in your questionable code, as if you included it as a function library. As a result, you'll want to be careful that Function / Sub names in the questionable code don't collide with Function / Sub names in your syntax checking code.


1 comment:

Aaron Silverman said...

If you have very large files of automatically generated functions, sometimes it may help to know which one has an error--either inside a function block or rouge code that is outside function blocks when it shouldn't be.

A modification of the below code may be helpful:

Private Function checkVBSSyntaxByFunction(vbsCode)

    fxnStartText = "Public Function"
    currStart = 1
    currEnd = 1
    hasError = false

    while currStart > 0 and currEnd > 0 and currStart < len(vbsCode)

        currStart = inStr(currStart,vbsCode,fxnStartText,1)
        If currStart > 0 Then
            currEnd = inStr(currStart+1,vbsCode,fxnStartText,1)
            If currEnd > 0 Then
                currFxnCode = mid(vbscode,currStart,currEnd-currStart)
            Else
                currFxnCode = mid(vbscode,currStart)
            End If

            fxnCtr+1
            On Error Resume Next
            ' Execute the vbs in the file to see if it throws an error
            ExecuteGlobal currFxnCode
            hasError = Err.Number <> 0
            On Error Goto 0
            Err = oldErr
            If hasError Then
                msgbox "Error With: " & currFxnCode
                currStart = -1
            Else
                currStart = currEnd
            End If
        End If
    WEnd

    checkVBSSyntaxByFunction = (Not hasError)

End Function