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:
- It makes test maintenance easier (you maintain only the structure of the test, not the code's syntax)
- It enables users who are unfamiliar with VBS to author and maintain tests
- 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.