QTP – VB Scripting
What is VBScript? What is VBScript? •VBScript is a scripting language •A scripting language is a lightweight programming language •VBScript is a light version of Microsoft's programming language Visual Basic •VBScript is the default language in ASP (Active Server Pages)
VBScript Variables VBScript variables are used to hold values or expressions. A variable can have a short name, like x, or a more descriptive name, like carname. Rules for VBScript variable names: -> Must begin with a letter -> Cannot contain a period (.) -> Cannot exceed 255 characters In VBScript, all variables are of type variant, that can store different types of data.
VBScript Variables Declaring (Creating) VBScript Variables Dim x Dim carname Assigning Values to Variables carname="Volvo"
VBScript Arrays An array variable is used to store multiple values in a single variable. Creating Arrays in VB Scripting Dim names(2) Adding values into an Array : names(0)="Tove" names(1)="Jani" names(2)="Stale" Reading Values from an Array : mother=names(0) Creating Two Dimensional Array Dim names(2,2) Adding values into an Array : names(0,0)="Tove" names(0,1)="Jani" names(1,0)="Stale" names(1,0)="Stale" Reading Values from an Array : mother=names(0,0)
VB - Option Explicit Option Explicit Dim carname carname=some value This statement forces you to declare all your variables with the dim If you misspell for example the "carname" variable to "carnime", the script will automatically create a new variable called "carnime". To prevent your script from doing this, you can use the Option Explicit statement.
VB - Functions A Function procedure: •is a series of statements, enclosed by the Function and End Function statements •can perform actions and can return a value •can take arguments that are passed to it by a calling procedure •without arguments, must include an empty set of parentheses () •returns a value by assigning a value to its name Syntax for Creating Functions: function myfunction(A, B) myfunction= A + B end function Syntax for Calling Function: Call myfunction(argument)
VB - Sub procedure A Sub procedure: •is a series of statements, enclosed by the Sub and End Sub statements •can perform actions, but does not return a value •can take arguments Example : Sub mysub(argument1,argument2) some statements End Sub Syntax for Calling Sub: Mysub argument
VB – Conditional Statements Conditional statements are used to perform different actions for different decisions. In VBScript we have four conditional statements: •If statement - executes a set of code when a condition is true •If...Then...Else statement - select one of two sets of lines to execute •If...Then...ElseIf statement - select one of many sets of lines to execute •Select Case statement - select one of many sets of lines to execute
VB – Conditional Statements Syntax If Statement : If i = 10 Then some statements ElseIf i = 11 Then some statements ElseIf i = 12 Then some statements Else some statements End If Syntax Select Statement: Select Case d Case 1 some statements Case 2 some statements Case else some statements End Select
VB – Looping Statements Looping statements are used to run the same block of code a specified number of times. In VBScript we have four looping statements: •For...Next statement - runs code a specified number of times •For Each...Next statement - runs code for each item in a collection or each element of an array •Do...Loop statement - loops while or until a condition is true •While...Wend statement - Do not use it - use the Do...Loop statement instead
VB – Looping Statements Syntax for Loop: For i=2 To 10 some code Next Exit a For...Next You can exit a For...Next statement with the Exit For keyword. For i=1 To 10 If i=5 Then some code Exit For End If Next
VB – String Functions Function Description InStr Returns the position of the first occurrence of one string within another. The search begins at the first character of the string InStrRev Returns the position of the first occurrence of one string within another. The search begins at the last character of the string LCase Converts a specified string to lowercase Left Returns a specified number of characters from the left side of a string Len Returns the number of characters in a string LTrim Removes spaces on the left side of a string RTrim Removes spaces on the right side of a string Trim Removes spaces on both the left and the right side of a string
VB – String Functions Mid Returns a specified number of characters from a string Replace Replaces a specified part of a string with another string a specified number of times Right Returns a specified number of characters from the right side of a string Space Returns a string that consists of a specified number of spaces StrComp Compares two strings and returns a value that represents the result of the comparison String Returns a string that contains a repeating character of a specified length StrReverse Reverses a string UCase Converts a specified string to uppercase
VB – Array Functions Function Description Array Returns a variant containing an array Filter Returns a zero-based array that contains a subset of a string array based on a filter criteria IsArray Returns a Boolean value that indicates whether a specified variable is an array Join Returns a string that consists of a number of substrings in an array LBound Returns the smallest subscript for the indicated dimension of an array Split Returns a zero-based, one-dimensional array that contains a specified number of substrings UBound Returns the largest subscript for the indicated dimension of an array
VB – Date Functions Function Description CDate Converts a valid date and time expression to the variant of subtype Date Date Returns the current system date DateAdd Returns a date to which a specified time interval has been added Day Returns a number that represents the day of the month (between 1 and 31, inclusive) FormatDateTime Returns an expression formatted as a date or time Hour Returns a number that represents the hour of the day (between 0 and 23, inclusive) IsDate Returns a Boolean value that indicates if the evaluated expression can be converted to a date
VB – Date Functions Minute Returns a number that represents the minute of the hour (between 0 and 59, inclusive) Month Returns a number that represents the month of the year (between 1 and 12, inclusive) MonthName Returns the name of a specified month Now Returns the current system date and time Second Returns a number that represents the second of the minute (between 0 and 59, inclusive) Time Returns the current system time TimeValue Returns a time Weekday Returns a number that represents the day of the week (between 1 and 7, inclusive) Year Returns a number that represents the year

Qtp vb scripting

  • 1.
    QTP – VBScripting
  • 2.
    What is VBScript? Whatis VBScript? •VBScript is a scripting language •A scripting language is a lightweight programming language •VBScript is a light version of Microsoft's programming language Visual Basic •VBScript is the default language in ASP (Active Server Pages)
  • 3.
    VBScript Variables VBScript variablesare used to hold values or expressions. A variable can have a short name, like x, or a more descriptive name, like carname. Rules for VBScript variable names: -> Must begin with a letter -> Cannot contain a period (.) -> Cannot exceed 255 characters In VBScript, all variables are of type variant, that can store different types of data.
  • 4.
    VBScript Variables Declaring (Creating)VBScript Variables Dim x Dim carname Assigning Values to Variables carname="Volvo"
  • 5.
    VBScript Arrays An arrayvariable is used to store multiple values in a single variable. Creating Arrays in VB Scripting Dim names(2) Adding values into an Array : names(0)="Tove" names(1)="Jani" names(2)="Stale" Reading Values from an Array : mother=names(0) Creating Two Dimensional Array Dim names(2,2) Adding values into an Array : names(0,0)="Tove" names(0,1)="Jani" names(1,0)="Stale" names(1,0)="Stale" Reading Values from an Array : mother=names(0,0)
  • 6.
    VB - OptionExplicit Option Explicit Dim carname carname=some value This statement forces you to declare all your variables with the dim If you misspell for example the "carname" variable to "carnime", the script will automatically create a new variable called "carnime". To prevent your script from doing this, you can use the Option Explicit statement.
  • 7.
    VB - Functions AFunction procedure: •is a series of statements, enclosed by the Function and End Function statements •can perform actions and can return a value •can take arguments that are passed to it by a calling procedure •without arguments, must include an empty set of parentheses () •returns a value by assigning a value to its name Syntax for Creating Functions: function myfunction(A, B) myfunction= A + B end function Syntax for Calling Function: Call myfunction(argument)
  • 8.
    VB - Subprocedure A Sub procedure: •is a series of statements, enclosed by the Sub and End Sub statements •can perform actions, but does not return a value •can take arguments Example : Sub mysub(argument1,argument2) some statements End Sub Syntax for Calling Sub: Mysub argument
  • 9.
    VB – ConditionalStatements Conditional statements are used to perform different actions for different decisions. In VBScript we have four conditional statements: •If statement - executes a set of code when a condition is true •If...Then...Else statement - select one of two sets of lines to execute •If...Then...ElseIf statement - select one of many sets of lines to execute •Select Case statement - select one of many sets of lines to execute
  • 10.
    VB – ConditionalStatements Syntax If Statement : If i = 10 Then some statements ElseIf i = 11 Then some statements ElseIf i = 12 Then some statements Else some statements End If Syntax Select Statement: Select Case d Case 1 some statements Case 2 some statements Case else some statements End Select
  • 11.
    VB – LoopingStatements Looping statements are used to run the same block of code a specified number of times. In VBScript we have four looping statements: •For...Next statement - runs code a specified number of times •For Each...Next statement - runs code for each item in a collection or each element of an array •Do...Loop statement - loops while or until a condition is true •While...Wend statement - Do not use it - use the Do...Loop statement instead
  • 12.
    VB – LoopingStatements Syntax for Loop: For i=2 To 10 some code Next Exit a For...Next You can exit a For...Next statement with the Exit For keyword. For i=1 To 10 If i=5 Then some code Exit For End If Next
  • 13.
    VB – StringFunctions Function Description InStr Returns the position of the first occurrence of one string within another. The search begins at the first character of the string InStrRev Returns the position of the first occurrence of one string within another. The search begins at the last character of the string LCase Converts a specified string to lowercase Left Returns a specified number of characters from the left side of a string Len Returns the number of characters in a string LTrim Removes spaces on the left side of a string RTrim Removes spaces on the right side of a string Trim Removes spaces on both the left and the right side of a string
  • 14.
    VB – StringFunctions Mid Returns a specified number of characters from a string Replace Replaces a specified part of a string with another string a specified number of times Right Returns a specified number of characters from the right side of a string Space Returns a string that consists of a specified number of spaces StrComp Compares two strings and returns a value that represents the result of the comparison String Returns a string that contains a repeating character of a specified length StrReverse Reverses a string UCase Converts a specified string to uppercase
  • 15.
    VB – ArrayFunctions Function Description Array Returns a variant containing an array Filter Returns a zero-based array that contains a subset of a string array based on a filter criteria IsArray Returns a Boolean value that indicates whether a specified variable is an array Join Returns a string that consists of a number of substrings in an array LBound Returns the smallest subscript for the indicated dimension of an array Split Returns a zero-based, one-dimensional array that contains a specified number of substrings UBound Returns the largest subscript for the indicated dimension of an array
  • 16.
    VB – DateFunctions Function Description CDate Converts a valid date and time expression to the variant of subtype Date Date Returns the current system date DateAdd Returns a date to which a specified time interval has been added Day Returns a number that represents the day of the month (between 1 and 31, inclusive) FormatDateTime Returns an expression formatted as a date or time Hour Returns a number that represents the hour of the day (between 0 and 23, inclusive) IsDate Returns a Boolean value that indicates if the evaluated expression can be converted to a date
  • 17.
    VB – DateFunctions Minute Returns a number that represents the minute of the hour (between 0 and 59, inclusive) Month Returns a number that represents the month of the year (between 1 and 12, inclusive) MonthName Returns the name of a specified month Now Returns the current system date and time Second Returns a number that represents the second of the minute (between 0 and 59, inclusive) Time Returns the current system time TimeValue Returns a time Weekday Returns a number that represents the day of the week (between 1 and 7, inclusive) Year Returns a number that represents the year