Common Scripting Problems

Here are some common problems that occur with scripts.

Typographical errors

It is very easy to make typographical errors that the 4Test compiler cannot catch. If a line of code does nothing, this might be the problem.

Global variables with unexpected values

When you write a function that uses global variables, make sure that each variable has an appropriate value when the function exits. If another function uses the same variable later, and it has an unexpected value on entry to the function, an error could occur.

To check that a variable has a reasonable value on entry to a function, set a breakpoint on the line that calls the function and use the command View > Global Variables to check the variable's value.

Uninitialized variables

Silk Test Classic does not initialize variables for you. So if you have not initialized a variable on entry to a function, it will have the value <unset>. It is better to explicitly give a value to a variable than to trust that another function has already initialized it for you. Also, remember that 4Test does not keep local variables around after a function exits; the next time the function is called, its local variables could be uninitialized.

If you are in doubt about whether a variable has a reasonable value at a particular point, set a breakpoint there and use View > Global Variables or View > Local Variables to check the variable's value.

Global and local variables with the same name

It is usually not good programming practice to give different variables the same names. If a global and local variable with the same name are in scope (accessible) at the same time, your code can only access the local variable.

To check for repeated names, use View > Local Variables and View > Global Variables to see if two variables with the same name are in scope simultaneously.

Incorrect values for loop variables

When you write a for loop or a while loop, be sure that the initial, final, and step values for the variable that controls the loop are correct. Incrementing a loop variable one time more or less than you really want is a common source of errors.

To make sure a control loop works as you expect, use Debug > Step Into to step through the execution of the loop one statement at a time, and watch how the value of the loop variable changes using View > Local Variables.

Checking the precedence of operators

The order in which 4Test applies operators when it evaluates an expression may not be what you expect. Use parentheses, or break an expression down into intermediate steps, to make sure it works as expected. You can use View/Expression to evaluate an expression and check the result.

Incorrect uses of break statements

A break statement transfers control of the script out of the innermost nested for, for each, while, switch, or select statement only. In other words, break exits from a single loop level, not from multiple levels. Use Debug > Step Into to step through the script one line at a time and ensure that the flow of control works as you expect.

Infinite loops

To check for infinite loops, step through the script with Debug > Step Into.

Code that never executes

To check for code that never executes, step through the script with Debug > Step Into.