310 likes | 450 Views
Test Script Language. What is TSL. Proprietary language owned by HP/Mercury for use in their testing tools Heavily based on the C language and is very similar to Perl and JavaScript Interpreted Loosely-typed Implicitly declared (except in functions). Contents. Syntax Rules Naming Rules
E N D
What is TSL • Proprietary language owned by HP/Mercury for use in their testing tools • Heavily based on the C language and is very similar to Perl and JavaScript • Interpreted • Loosely-typed • Implicitly declared (except in functions)
Contents • Syntax Rules • Naming Rules • Comments • Data Types • Storage • Variables • Constants • Operators • Arithmetic • Comparison • Logical • Assignment • Concatenation • Branching • if • Looping • while • do…while • for
Syntax Rules • TSL is case sensitive • Statements can be either simple statements or compound statements • Simple statements are terminated with a semi-colon (;). • Compound statements are marked by { } (open and close curly braces
Naming Rules • Must begin with a letter or the underscore • Good: count, strInfo, _data • Bad: 2day, 4get, *count violate this rule. • Cannot contain special characters except the underscore • Good: Names using alphabets, numbers and the underscore. • Bad: Names using symbols and other special characters. • Cannot match a reserved word • Bad: for, if, Select • Must be unique within a context • Must be less than 48 characters • Names are case sensitive • You can have 3 variables named Data, data & DATA in the same test
Naming Convention objDescriptiveName • obj • Three letter prefix for the data type • DescriptiveName • A descriptive name for the variable • First letter of each word in upper case
Comments # This is a comment • Comments can be full line or partial line • Comments do not need to end with a ;
Literal Types • Numeric • 1 • 3.14 • 3E7 • String • “Hello World” • “123 Straw Lane” • “43210” • Boolean values are treated as numbers • True is represented as 1 • False is represented as 0
Variables staticvariableName; autovariableName; publicvariableName; externvariableName; • Variables hold values that can change during program execution • They function just like temporary storage locations
Constants const constantName=value; • Constant names hold values that cannot change during program execution • Constants should be used in place of hard-coded values
Operations • Operations are the many forms of computations, comparisons etc that can be performed in the language • Most operations are binary using the form: operand1 operator operand2 • Two operations are unary using the form operator operand1 • One operation is tertiary using the form operand1 operator operand2 operator operand3 • When an operator has 2 symbols, you cannot separate them with a space: • Good: 2 != 3 • Bad: 2 ! = 3
Arithmetic • Used to perform calculations • Both operands must be numeric values • The result is a numeric value
Comparison • Used to compare the value of two items • Both operands must be of the same data type • The result is a Boolean value
Logical • Used to reduce Boolean values into a single Boolean value • Both operands must be Boolean values • The result is a Boolean value
And (&&) Truth Table • Conjunction • Used when both conditions are required
Or (||) Truth Table • Disjunction • Used when either condition is sufficient
Not (!) Truth Table • Unary Operand • Used to change the value of a condition
Assignment • Changes the value of a variable • = • Several shortcut forms exist:
Concatenation • Combines 2 data types for display as a string • &
Branching • Allows you to avoid running certain sections of your code • Code is only executed when a condition (or conditions) evaluate to True • Provides a single application the ability to react differently to different input values
if If (condition) { statement(s); } • Performs an operation only if the condition evaluates to True • Used when an action is either performed or not performed.
if…else if (condition){ statement(s); } else { statement(s); } • Performs the If portion only if the condition evaluates to True and the Else portion otherwise. • Used in an either or scenario when an operation is always performed.
if…else..if if (condition1){ statement(s); } else if (condition2){ statement(s); } else { statement(s); } • Only one section can be executed even if many conditions evaluate to True. • Used in a multiple choice scenario • Inclusion of the last Else is optional
Loops • Allows you to repeat running certain sections of your code • Code executes when a condition (or conditions) evaluate to True • Be careful with Loops. They can result in infinite processing. • Forms • Entry Condition • Entry only when a initial condition is met • Exit Condition • The loop is always executed at least once • Iterated • Repeats for a specific number of times
Loop Questions • Can this loop ever be entered • If no, then you don’t need the loop • Can this loop ever be exited • If no, then you have an infinite loop
while while (condition) { statement(s); } • Entry Condition Loop • Simplest form of the loop • Requires manual modification of the loop condition
do..while do { statement(s); } while (condition); • Exit Condition Loop • Requires manual modification of the loop condition
for for (variable = start; condition; modifier) { statement(s); } • Iterated Loop • Favored because all the loop details are in the definition statement
Functions function FunctionName(parameters) { statement(s); } • Allow you to define a set of operations • Must be defined before they are invoked
Exercise 1 Convert the following VBScript code to TSL ‘Declare a variable Dim numData Const PI = 3.142 ‘Use create_input_dialog instead of InputBox numData = InputBox(“Please enter a radius”) numAnswer = PI * (numData ^ 2) ‘Use pause instead of Msgbox ‘In TSL, the () is mandatory Msgbox “The area of a circle of radius “ & numData & “ is “ & numAnswer
Exercise 2 Convert the following TSL code to VBScript static numData; numData = create_input_dialog(“Enter a number”); numDouble = numData * 2; numSquare = numData ** 2; strAnswer = “The double of “ & numData & “ is “ & numDouble; #Use vbCrLf instead of \n strAnswer = strAnswer & “\n”; strAnswer &= “The square of “ & numData & “ is “ & numSquare; pause(strAnswer);