In this article,I will discuss a few commonly used VBScript methods in UFT that you must know to write better optimized automation script in less time.So keep reading.
Frquently Used VBScript Methods in UFT
InStr Function in VBscript
The InStr method returns the position of the first occurrence of one string within another.We can use this method to check wheter a string exists in a given String as well as compare two strings.
Syntax: InStr([start, ]string1, string2[, compare])
Arguments
start: It is an optional argument.It accepts numeric expression that sets the starting position for each search. If you don’t mention any value, search begins at the first character position. The start argument becomes mandatory if compare (the fourth argument) is specified .
string1
Required parameter. String expression being searched.
string2
Required parameter.String expression that needs to be sreached.
compare
It an optional parameter. Accepts Numeric value indicating the kind of comparison to use when evaluating substrings. Valid values are 0 and 1 for binary and textual comparision.You can also use constant values vbBinaryCompare and vbTextCompare in place of 0 and 1.Refer the following table.
Constant | Value | Description |
---|---|---|
vbBinaryCompare | 0 | Perform a binary comparison. |
vbTextCompare | 1 | Perform a textual comparison. |
Return Value
If | Return Value |
---|---|
string1 is zero-length | 0 |
string1 is Null | Null |
string2 is zero-length | start |
string2 is Null | Null |
string2 is not found | 0 |
string2 is found within string1 | Position at which match is found |
start > Len(string2) | 0 |
Example
The following examples illustrates how to use InStr to search a string:
1 2 3 4 5 6 7 |
SearchString = "XXpXXpXXPXXP uft UFT" ' String to search in. SearchChar = "P" ' Search for "P". MyPos = InStr(4, SearchString, SearchChar, 1) ' A textual comparison starting at position 4. Returns 6. MyPos = InStr(1, SearchString, SearchChar, 0) ' A binary comparison starting at position 1. Returns 9. MyPos = InStr(SearchString, SearchChar) ' Comparison is binary by default (last argument is omitted). Returns 9. MyPos = InStr(1, SearchString, "C") ' A binary comparison starting at position 1. Returns 0 ("C" is not found). MyPos = InStr(1, SearchString, "UFT") ' A binary comparison starting at position 1. Returns 18. |
StrComp Function in VBscript
The StrComp method is used to compare two string and returns a value indicating the result of a string comparison.
Syntax: StrComp(string1, string2[, compare])
Arguments
string1
Required parameter. Any valid string expression.
string2
Required parameter. Any valid string expression.
compare
Optional parameter. Numeric value indicating the kind of comparison to use when evaluating strings. If the parameter not used, a binary comparison is performed. See the following table for valid values.
Constant | Value | Description |
---|---|---|
vbBinaryCompare | 0 | Perform a binary comparison. |
vbTextCompare | 1 | Perform a textual comparison. |
Return Value
The StrComp function has the following return values:
If | StrComp RETURN VALUE |
---|---|
string1 is zero-length | -1 |
string1 is equal to string2 | 0 |
string1 is greater than string2 | 1 |
string1 or string2 is Null | Null |
Example
The following examples illustrates how to use StrComp for a string comparison.
1 2 3 4 5 6 7 |
MyStr1 = "UFT": MyStr2 = "uft" ' Defining variables. MyComp = StrComp(MyStr1, MyStr2, 1) ' Returns 0. MyComp = StrComp(MyStr1, MyStr2, 0) ' Returns -1. MyComp = StrComp(MyStr2, MyStr1) ' Returns 1. |
LTrim; RTrim; and Trim Functions
The above three functions are used to return a copy of a string without leading spaces (LTrim), trailing spaces (RTrim), or both leading and trailing spaces (Trim).
Syntax: LTrim(string) RTrim(string) Trim(string)
Example
The following examples illustrates how to use LTrim,RTrim and Trim for removing spaces.
1 2 3 4 5 |
MyVar = LTrim(" UFT ") ' Returns all leading spaces and retunrs "UFT " MyVar = RTrim(" UFT ") ' Returns all trailing spaces and retunrs " UFT" MyVar = Trim(" UFT ") ' Returns all leading trailing spaces and retunrs "UFT" |
Left Function in VBscript
The Left function a specified number of characters from the left side of a string.
Syntax: Left(string, length)
Arguments
string
String expression from which the leftmost characters are returned. If string contains Null value, a Null value is returned.
length
Numeric expression indicating how many characters to return. If one provides 0, a zero-length string(“”) is returned.
The following examples illustrates the use of Left function to return a specified number of characters from the left side of a string:
1 2 3 |
MyString = "MySkillpoint" LeftStringVal = Left(MyString, 7) ' LeftStringVal contains "MySkill". |
Right Function in VBscript
The Right function a specified number of characters from the right side of a string.
Syntax: Right(string, length)
Arguments
string
String expression from which the rightmost characters are returned. If string contains Null value, a Null value is returned.
length
Numeric expression indicating how many characters to return. If one provides 0, a zero-length string(“”) is returned.
To determine the number of characters in a string, you can use the Len function.
Example
The following example illustrates how to use the Right function to return a specified number of characters from the right side of a string:
1 2 3 4 5 |
MyString = "Hello UFT" ' Define string. MyVar = Right(MyString, 2) ' Returns "FT". MyVar = Right(MyString, 15) ' Returns "Hello UFT" as 15 is more than the length of given string MyString |
Len Function in VBscript
The Len function returns the number of characters in a string or the number of bytes required to store a variable.
Syntax: Len(string | varName)
Arguments
string or varName
Any valid string expression or variable name. If string or variable contains a Null, Null is returned.
Example:
The following example shows how to get the length of a string.
1 2 3 |
MyString = "Hello UFT" ' Define string. MyVarLen = Len(MyString) ' Returns 9. |
Mid Function in VBScript
The Mid function returns a specified number of characters from a given string.This a very useful method.We can use this method to retrieve certain number of characters from any postion in a string.
Syntax: Mid(string, start[, length])
Arguments
string
String expression from which characters are returned. If string contains Null, Null is returned.
start
Numeric value.Starting position from which characters should be retrieved. If start is greater than the number of characters in string, Mid returns a zero-length string (“”).
length
Optinal value.Number of characters to return. If not privided or if there are fewer than length characters in the text (including the character at start), all characters from the start position to the end of the string are returned.
To determine the number of characters in a string, you can use the Len function.
Example
The following example illustrates the uses of the Mid function.
1 2 3 4 5 6 7 |
MyVar = Mid("myskillpoint", 3, 5) ' MyVar contains "skill". Getting 5 characters from 3rd postion. MyString = "I love myskillpoint" MyVar = Mid(MyString, 7, Len(MyString) - 11) ' MyVar contains "myskill".Calculate how it comes. MyVar = Mid("Coding is my passion", 10) ' MyVar contains "my passion". |
Replace Function in VBscript
The Replace is mostly used to return a string in which a specified substring has been replaced with another substring.
Syntax: Replace(expression, find, replacewith[, start[, count[, compare]]])
Arguments
expression
Required. String expression containing substring to replace.
find
Required. Substring being searched for.
replacewith
Required. Replacement substring
start
Optional Value. Position within expression where substring search is to begin. If not provided, 1 is assumed. Must be used in conjunction with count.
count
Optional Value. Number of substring substitutions to perform. If not provided, the default value is -1, which means make all possible substitutions. Must be used in conjunction with start.
compare
Optional Value. Numeric value indicating the kind of comparison to use when evaluating substrings. Refer the following for valid values. If not provided, the default value is 0, which means perform a binary comparison.
Constant | Value | Description |
---|---|---|
vbBinaryCompare | 0 | Perform a binary comparison. |
vbTextCompare | 1 | Perform a textual comparison. |
Return Value
If | Replace Function returns |
---|---|
expression is zero-length | Zero-length string (""). |
expression is Null | An error message. |
find is zero-length | Copy of expression. |
replacewith is zero-length | Copy of expression with all occurrences of find removed. |
start > Len(expression) | Zero-length string. |
count is 0 | Copy of expression. |
Example
The following example illustrates the uses of the Replace function to return a string after replacing required values.
1 2 3 4 5 6 7 |
' A binary comparison starting at the beginning of the string. MyString = Replace("ABCXpXXpZP", "p", "Y") ' Returns "ABCXYXXYZP". ' A textual comparison starting at position 3 and returns replaced string from position 3 MyString = Replace("ABCXpXXpZP", "p", "Y", 3, -1, 1) ' Returns CXYXXYZY |
StrReverse Function in VBscript
The StrReverse function returns a string in which the character order of a specified string is reversed.
Syntax: StrReverse(string1)
Example
The following example illustrates the StrReverse function to return a string in reverse order:
1 |
MyVar = StrReverse("Enjoy") ' MyVar contains "yojnE". |
Space Function in VBscript
The Space function creates and return a string with the specified number of spaces.
Syntax: Space(number)
The number argument is the number of spaces you want in the string.
Example:
The following example shows how to use Space function.
1 |
MyVar = "Welcome" & Space(10) & "All" ' Insert 10 spaces between two strings. |
CStr Function in VBscript
The CStr function is used to type cast a variant to a String.
Syntax: CStr(expression)
The expression argument is any valid expression.
Example:
The following example illustrates how to use the CStr function to convert a numeric value to a String:
1 2 3 |
MyDouble = 100.5 ' MyDouble is a Double. MyString = CStr(MyDouble) ' MyString contains "100.5". |
CInt Function in VBscript
The CInt function is used to type cast a variant to an Integer.
Syntax: Cint(expression)
The expression argument is any valid expression.
Example:
The following example illustrates how to use the Cint function to convert a value to an Integer:
1 2 3 4 5 6 7 |
MyDouble = 100.5 ' MyDouble contains 100.5 MyInt = CInt(MyDouble) ' MyInt contains 100 MyString = "200.25" ' MyString contains "200.25" MyInt = CInt(MyString) ' MyInt contains 200 |
CDbl Function in VBscript
The CDbl function is used to type cast a variant to a Double.
Syntax: CDbl(expression)
The expression argument is any valid expression.
Example:
The following example illustrates how to use the CDbl function to convert a value to a Double:
1 2 3 |
MyString = "100.55" ' MyString contains "100.55" MyDouble = CDbl(MyString) ' MyDouble contains 100.55. |
CDate Function in VBscript
The CDate function is used to type convert a variant to a Date.
Syntax: CDate(expression)
The expression argument is any valid expression.
You can use the IsDate function to check whether an expression can be converted to a date or time. CDate recognizes date literals and time literals as well as some numbers that fall within the range of acceptable dates. When converting a number to a date, the whole number portion is converted to a date. Any fractional part of the number is converted to a time of day, starting at midnight.
Example:
The following example illustrates how to use the CDate function to convert a value to a Date:
1 2 3 4 5 6 7 8 |
MyDate = "March 20, 2021" ' Define date. MyShortDate = CDate(MyDate) ' Convert to Date data type.MyShortDate contains 3/20/201.Date Format would be as per your system date format MyTime = "5:10:30 PM" ' Define time. MyShortTime = CDate(MyTime) ' Convert to Date data type.MyShortTime contains "5:10:30 PM" MyDateVar = "20/12/2021" ' Define date. MyDateVal = CDate(MyDate) ' Convert to Date data type.MyDateVal contains 12/20/2021. |
Abs Function in VBscript
The Abs function returns the absolute value of a number.
Syntax: Abs(number)
The absolute value of a number is its unsigned magnitude. For example, Abs(-1) and Abs(1) both return 1.
Example:
The following example illustrates how to use the Abs function to get the absolute value of a number irrespective of its sign.
1 2 3 |
MyNumber = Abs(30.5) ' Returns 30.5. MyNumber = Abs(-30.5) ' Returns 30.5. |
Split Function in VBscript
The Split function returns one-dimensional array containing a specified number of substrings.This is also a very important method.
Syntax: Split(expression[, delimiter[, count[, compare]]])
Arguments
expression
Required parameter. String expression containing substrings and delimiters.
delimiter
Optional parameter. String character used to identify substring limits. If no value provided, the space character (” “) is assumed to be the delimiter.
count
Optional parameter. Number of substrings to be returned; -1 indicates that all substrings are returned.
compare
Optional parameter. Numeric value indicating the kind of comparison to use when evaluating substrings. Refer the following table for valid values.
Constant | Value | Description |
---|---|---|
vbBinaryCompare | 0 | Perform a binary comparison. |
vbTextCompare | 1 | Perform a textual comparison. |
Example:
The following example illustrates the uses of Split function to return an array from a string. The function performs a textual comparison of the delimiter, and returns all of the substrings.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
MyString = "AutomationXisXfun" MyArray = Split(MyString, "x", -1, 1) 'x delimter is used to split the string into an array ' MyArray(0) contains "Automation". ' MyArray(1) contains "is". ' MyArray(2) contains "fun". Msg = MyArray(0) & " " & MyArray(1) MsgBox Msg 'Displays "Automation is" Msg = Msg & " " & MyArray(2) MsgBox Msg 'Displays "Automation is fun" MyString = "India is a great country" MyArray = Split(MyString, " ") 'Space delimter is used to split the string into an array MsgBox MyArray(0) ' MyArray(0) contains India |
UBound Function in VBscript
The UBound also known as Upper Bound of an array function is mostly used to get the length of the array.
Syntax:
Example:
The following example illustrates how to use Ubound function to get the length of the array.
1 2 3 |
MyArr = Split("I love my India", " ") 'Splitting the String into an array using space delimeter ArrLen = UBound(MyArr) ' ArrLen Contains 3. Array Indexing Starts from 0 |
Now Function in VBscript
The Now function returns the current date and time according to the setting of your computer’s system date and time.
The following example illustrates how to use Now function to get the current date and time.
1 |
MyVar = Now ' MyVar contains the current date and time. |
In practicle scenario,this function is mostly used to gnerate unique file names to save screenshots in an automation framework.
DateAdd Function in VBscript
The DataAdd function allows us to add (or subtract) days, months, years, etc. to Date or Time.
Syntax: DateAdd(interval, number, date)
Arguments
interval
Required parameter. String expression that is the interval you want to add. Refer the below table for valid values.
number
Required parameter. Numeric expression that is the number of interval you want to add. The numeric expression can either be positive, for getting future date, or negative, for getting dates in the past.
date
Required parameter. Variant represents the date value to which interval is added.
The interval argument has the following settings:
Settings | Description |
---|---|
yyyy | Year |
q | Quarter |
m | Month |
y | Day of year |
d | Day |
w | Weekday |
ww | Week of year |
h | Hour |
n | Minute |
s | Second |
Example:
The following example is adding 10 days to the current date
1 |
MyNewDate = DateAdd("d", 10, Date) |
The Date function returns current date that has been used as parameter value in the above example.
DateDiff Function in VBscript
The DateDiff function is used to calculate number of days between two dates.
Syntax: DateDiff(interval, date1, date2)
Example:
The following example uses the DateDiff function to display the number of days between two given dates:
1 |
MyNewDate = DateDiff("d", "30/04/2021", "5/05/2021")' MyNewDate contains 5 |
The interval argument settings has been given in DateAdd Function
DatePart Function in VBScript
The DatePart function is used get the specified part of a given date.
Syntax: DatePart(interval, date)
Example:
The following example uses the DatePart function to display the number of days between two given dates:
1 |
DateInterval = DatePart("d", "30/04/2021")' DateInterval contains 30 |
The interval argument settings has been given in DateAdd Function
Day Function in VBscript
The Day function in VBScript is used get day between 1 and 31 representing the day of the specified date.
The following example uses the Day function to get the value of day of a given date:
1 |
DayVal = Day("10/31/2020") ' DayVal contains 31 |
Concatenate Strings in VBScript
The & operator concatenates two strings in VBSript (VBA).The following example concatenates two strings Hello and World.The message box will display Hello World.
1 |
MsgBox "Hello" & "World" |
Conclusion
In this article,I have shared most frequently used VBScript functions in UFT.Hope you would have like this tutorial and find it helpful.If you have any queries please do mention them in the comment box and don’t forget to share this article.
Recommended Posts
- Four Kinds of Loops in UFT | VBScripting
- How to Use Insight Object in UFT
- How to Use Environment Variables in UFT
- Descriptive Programming in UFT with Examples
- All You Need to Know About Object Identification in UFT
- File System Object in UFT | VBA
- How to Use WaitProperty | Dynamic Wait in UFT
- Examples of Reporter.ReportEvent in UFT
- Transactions in UFT with Example
- 20 Useful VBA Date Functions in UFT You Must Know