Misc - QTP

This section contains several useful miscellaneous functions:

TypeName

This function returns a string that provides the Variant subtype name of a variable:

'Covert to double
x = CDbl(4)
'returns "Double"
MsgBoxTypeName(x)
'Convert to string x = CStr(4)
'returns "String"
MsgBoxTypeName(x)

VarType

This function returns a constant representing the Variant subtype of a variable:

'Convert to interger
x = CInt(4)
'Get the type of variable
If VarType(x) = vblnteger then

Msgbox "It's a integer"
End if
Refer to the VBScript Online HELP for a full list of TypeName and VarType values.

GetRef

GetRef method can be used to create a pointer object to a function. The Code below shows the usage of the same.

'Custom Function to do a text comparison
'we are worried about only first 5 characters Function ComparePartText(Textl, Text2)
If Left(Textl,5) = Left(Text2,5) Then
ComparePartText = True
Else
ComparePartText = False
End if
End Function

'Function to compare two string
Function CompareText(Textl, Text2) If Textl = Text2 Then
CompareT-ext = True Else
CorapareText = False
End if
End Function
'Function to compare 2 text based on comparison function
FunctionIsEqual(textlrtext2, compareFuncPointer)
IsEqual = compareFuncPointer(textl,text2)
End Function
Textl= "TarunLalwani"
Text2 = "TarunLalwani"
'Displays True
MsgBoxIsEqual(Textl,Text2, GetRef("CoaparePartText"))
'Displays False
MsgBoxIsEqual(Textl,Text2, GetRef("ComparePartText"))
In the above code we are passing a pointer to the comparison function to be used by IsEqual method when doing the text comparison.

CreateObject

This function returns a reference to a specified Automation object:

'Declare variable ...
Dim ExcelSheet
'Create an COM object for ProgID "Excel.Application"
Set ExcelSheet = CreateObject("Excel. Application")
sRemoteSever = "127.1.1.1"
'Create an COM object for ProgID "Excel.Application"
'on a remote server
Set ExcelSheet = CreateObject("Excel.Application", sRemoteSever )

GetObject

This function returns a reference to an Automation object from a file or an already open application:

'Get excel application form a running file
Set ExcelObject = GetObject("C:TestDefault.xls")
'Get excel application running instance
Set ExcelObject = GetObject(,"Excel.Application")

Problem . Get a sub-string in a string between two specified characters.

For example, a string contains "DEL (DELHI)", and we want to get the text between the parenthesis "(" and ")". Our function will also take the start position in the string to start the search

'Function to get a sub-string in a string between
'tospecifed characters
Public Function GetStrBetweenChar(ByVal Start, ByVals_Text, ByVals_StartChar, ByVals_EndChar)
GetStrBetweenChar = ""
Dim i, Count, s_Char
i = InStr(Start, s_Text, s_StartChar)
If i <> 0 Then
i = i + Len(s_StartChar)
Count = InStr(i, s_Text, s_EndChar)
If Count <> 0 Then
GetStrBetweenChar = Mid(s_Text, i. Count - i)
End If
End If
End Function

'Usage:

' This will return "DELHI"
MsgBoxGetStrBetweenChar(1,"DEL (DELHI)",("",")")

Problem:Function to get substring from a start charecter in astring:

For example - For this string "DEL (DELHI)" the function should return "DELHI)" when the start character is "(". The algorithm for this is much the same as the function discussed above but the difference is that we don't look for an end string this time:

'Get a sub string starting from a character

Public Function GetStrFromChar(ByVal Start ,„jSyVals_Text , ByVals_StartChar ByValReturnSameForNotFound)

If ReturnSameForNotFound = True Then
GetStrFromChar = s_Text
Else
GetStrFromChar = ""
End If
Dim i , Count , s_Char
i = InStr(Start, s_Text, s_StartChar)
If i <> 0 Then
i = i + Len(s_StartChar)
GetStrFromChar = Mid(s_Text, i)
End If
End Function

If the last argument, RerurnSameForNotFound=True, then the function returns the same string as if the start character is not found:

' This will return "(DELHI)"
MsgBoxGetStrFromChar (1, "DEL(DELHI)","DEL", False)
' This will return "DEL(DELHI)"
MsgboxGetStrFromChar (1, "DEL(DELHI)", "BEL", True)
1This will return ""
MsgboxGetStrFromChar (1," DEl(DELHI)","BEL", False)

Problem : Write a function that reads a string word by word where two words would be separated by one or more spaces.

The trick here would be to trim the string from the specified start position and then search for a space character in the string and then return the string till that space character

'Function to Get a word starting from a position
Public Function GetWordFromPos(ByVal Start, ByVals_Text)
GetWordFromPos = ""
Dim i, Count, s_Char, index i = Start
Count = Len(s_Text)
If Start >= Count Then GetWordFromPos = ""
s_Text = Trim(Mid(s_Text, i))
index = InStr(s_Text, " ")
If index Then

GetWordFromPos = Mid(s_Text, 1, index - l)
End If
End If
End Function
'this will return "Tarun"
MsgboxGetWordFromPos (l,"TarunLalwarsi wrote this book")
'this will return "Tarun"
MsgboxGetWordFromPos (2,"Tarun Lalwarsi wrote this book")
'this will return "Lalwani"
MsgboxGetWordFromPos (6,"Tarun Lalwani wrote this book")

Problem: Create a function TrimAII which trims not only spaces but other whitespace characters like new lines, camag e returns, and line feeds etc:

'Function to trim all type of white spaces from start
'and end of a string
Function TrimAII(ByValstrText)
Dim strLen
Dim strChar
strLen = Len(strText)
TrimAll = ""
Dim i
For i = 1 TostrLen
strChar = Mid(strText, i, 1)
Select Case strChar
Case vbCrLf, vbNewLine, vbTab, vbLf, vbFormFeed, vbCr, " Case
Else Exit For
End Select Next
strText = Mid(strText, i) strLen = Len(strText)
For i = strLen To 1 Step -1
strChar = Mid(strText, i, 1) Select Case strChar
Case vbCrLf, vbNewLine, vbTab, vbLf, vbFormFeed, vbCr, "
Case Else Exit For End Select Next
strText = Left(strText, i) TrimAll = strText
End Function
x= vbCrLf + vbNewLine + "Tarun" + vbLf

'this will assign "Tarun" to x x = TrimAll(x)

Problem : Create aFilterStr function which takes a string and character set as input and wili filter the string based on the characters in the character set. =or example, using the string "212-203-9587", and the character set "0123456789", the function will -eturn "2122039587" or "—" depending on the flag set. The algorithm evaluates each character in the -tring and checks if it is present in the character set and depending on the bInCharSet flag builds an appropriate return string:

'Function to filter a charset from a string
Function FilterStr(ByValstrText, ByValstrCharset, ByValbInCharSet)
Dim strLen
Dim strChar
strLen = Len(strText)
FilterStr = ""
Dim i
If blnCharSetThen 'return each strCharSeT. chars found in strTextFor i = 1 To strLen
strChar = Mid(strText, i, 1)
If InStr(strCharset, strChar) <> 0 Then
FilterStr = FilterStr + strChar
End If
Next
Else

'return each strCharSet chars NOT found in strText
For i = 1 TostrLen
strChar = Mid(strText, i, 1)
If InStr(strCharset, strChar) = 0 Then
FilterStr = FilterStr + strChar
End If Next End If
End Function
'this will return "2122122122"
MsgboxFilterStr("212-212-2122","0123456789",True)
'this will return "____"
MsgboxFilterStr("212-212-2122™,"0123458789",False)
'this will return "2122122122"
MsgBoxFilterStr(,,212-212-2122",, "-"", False)

Problem :Createtwofunctions,onewhichreturnsthepathportionandanother the file name, when passed a fuliy qualified path or fiiename:

'Function to get file path from file name Function GetFilePath(ByVals_FileName)
GetFilePath = ""
On Error Resume Next
GetFilePath = Left(s_FileName, Len(s_FileName) - InStr(StrReverse(s_ FileName) , "V") + 1 )
End Function
'Function to get file name from path Function GetFileName(ByVals_FileName)
GetFileName = ""
On Error Resume Next
GetFileName = Right(s_FileName, InStr(StrReverse(s_FileName), "") - l)
End Function
'Will return "Test.vbs"
MsgboxGetFileName(HCs TestTest. vbs")
'Will return "T:TestV"
MsgboxGetFilePath("CsTestTest. vbs")

Problem: Write a function that compares two string ignoring new line, line feeds, tab etc. whitescace characters:

'Function to clean up white space characters 'from inside a stringFunction CleanText(s_Text)
CleanText = s_Text
CleanText = Replace(CleanText, vbCrLf, "")
CleanText = Replace(CleanText, vbCr, vbLf)
CleanText = Replace(CleanText, Chr(7), " ")
CleanText = Replace(CleanText, vbCrLf, vbLf)
CleanText = Replace(CleanText, vbVerticalTab, vbLf)
CleanText = Replace(CleanText, vbTab, " ")
End Function
'Usage:
If CleanText(textl) = CleanText(text2) then
Msgbox "String equal after cleaning"
End if

Problem. Write af unction that replaces repeated occurrences of agiven char­acter by another character.

Example: this function will return "TarunLalwani" when a input string "TamnLalwani" is given for replacing repeated <Space> characters with a single underscore, '_', character:

'Function to replace continous character occurance of
'a specified character by a replacement character
Function ReplaceMultipleByOne(ByValstrText, ByValstrSearch, ByValstrReplace)
Dim i, strLen, strChar
Dim bFlag
bFlag = False
strLen = Len(strText)
For i = l TostrLen
strChar = Mid(strText, i, 1)
If strChar = strSearch Then If Not bFlag Then bFlag = True
ReplaceMultipleByOne = ReplaceMultipleByOne + strReplace
End If
Else
bFlag = False
ReplaceMultipleByOne = ReplaceMultipleByOne + strChar
End If Next End Function


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

QTP Topics