Notes-VBscript with an emphasis on automated testing with UFT-Module 6-Functions and Subroutines

Copy and paste the notes below into your favorite UFT to follow along with our tutorials.

' QTP TUTORIAL
'______________________________________________________________________________________________
'_____________________________FUNCTIONS AND SUBS _______________________________________________
'_____________________________FUNCTIONS AND SUBS _______________________________________________
'_____________________________FUNCTIONS AND SUBS _______________________________________________
'_____________________________FUNCTIONS AND SUBS _______________________________________________
'_____________________________FUNCTIONS AND SUBS _______________________________________________
'_____________________________FUNCTIONS AND SUBS _______________________________________________
'_____________________________FUNCTIONS AND SUBS _______________________________________________
'_____________________________FUNCTIONS AND SUBS _______________________________________________
'_____________________________FUNCTIONS AND SUBS _______________________________________________
'_____________________________FUNCTIONS AND SUBS _______________________________________________
'------- NOTES
'------- NOTES
'GOAL: By the end of this module, you will be able write and understand functions

'Procedure - a collection of VBscript statements that when called are executed as a unit
'2 types:
'1. Sub - A VBscript procedure that executes a set of statements without returning a result
'2. Function - A VBscript procedure that executes a set of statements and, optionally, returns
'a result to the statement that called it

'Sub structure:
'{Public/Private} Sub {name} ({argumentList})
'{statements}
'End Sub

''Function structure:
''{Public/Private} Function {name} ({argumentList})
' '{statements}
' '{name} = {some result}
''End Function
''' RULES for using procedures
'''1. Always use a function instead of a sub
'''2. A function should always return something
'''3. If you repeat the same code 3 or more times, wrap it in a function
'''4. Try to wrap complicated pieces of code into a function
'''5. A function can have objects, arrays and dictionaries returned to it, not only primitive data types

'
''''------- NOTES END
''''------- NOTES END
''''------- CODE
'''''------- CODE
''fnDisplayA
''
''Public Sub fnDisplayA()
'' Msgbox "a"
''End Sub
'
'fn_openApplication "iexplore.exe","www.qtptutorial.net"
'fn_openApplication "iexplore.exe","https://www.qtptutorial.net/exclusive-member-pricing/"
'fn_openApplication "iexplore.exe","https://www.qtptutorial.net/freetrialprecart"
''' <summary>
''' QTP Tutorial
'''' Opens a brand new browser and url after closing any open browsers
'''' </summary>
'''' <param name="strBrowser" type="string"> browser process that should be opened. Ex. iexplore.exe </param>
'''' <param name="strUrl" type="string"> the URL of the application that you want to open </param>
'''' <example>
'''' fn_openApplication "firefox.exe","www.yahoo.com"
'''' </example>
'Sub fn_openApplication(strBrowser,strUrl) 'added
' 'If the browser process is not empty, then we can go ahead and close all the browsers of that type
' If strBrowser <> "" Then
' 'close
' systemUtil.CloseProcessByName strBrowser
' 'fn_killExecutable strBrowser
' 'open that browser type and go to the url specified
' SystemUtil.Run strBrowser,strUrl
' Else
' 'Otherwise, print that the browser is empty and that we cannot proceed
' Print "Browser process received an empty string."
'
' End If
'End Sub
'
'strBrowser = "iexplore.exe"
'strUrl = "www.qtptutorial.net"
' If strBrowser <> "" Then
' 'close
' systemUtil.CloseProcessByName strBrowser
' 'fn_killExecutable strBrowser
' 'open that browser type and go to the url specified
' SystemUtil.Run strBrowser,strUrl
' Else
' 'Otherwise, print that the browser is empty and that we cannot proceed
' Print "Browser process received an empty string."
'
' End If
'
'strBrowser = "iexplore.exe"
'strUrl = "www.qtptutorial.net"
' If strBrowser <> "" Then
' 'close
' systemUtil.CloseProcessByName strBrowser
' 'fn_killExecutable strBrowser
' 'open that browser type and go to the url specified
' SystemUtil.Run strBrowser,strUrl
' Else
' 'Otherwise, print that the browser is empty and that we cannot proceed
' Print "Browser process received an empty string."
'
' End If
'
'strBrowser = "iexplore.exe"
'strUrl = "www.qtptutorial.net"
' If strBrowser <> "" Then
' 'close
' systemUtil.CloseProcessByName strBrowser
' 'fn_killExecutable strBrowser
' 'open that browser type and go to the url specified
' SystemUtil.Run strBrowser,strUrl
' Else
' 'Otherwise, print that the browser is empty and that we cannot proceed
' Print "Browser process received an empty string."
'
' End If

Dim strDate
If fn_getDate() <> "x" Then
msgbox fn_getDate()
End If

strDate = fn_getDate()
Function fn_getDate() 'added
Dim sYear,sMonth,sDay,strHour,strMin,strSec
sYear= DatePart("yyyy",now)
sMonth = DatePart("m", now)
sDay = DatePart("d", now)
strHour = DatePart("h",now)
strMin = DatePart("n",now)
strSec = DatePart("s",now)
fn_getDate = " " & sMonth & "_" & sDay & "_" & sYear &"_" & strHour & "h" & strMin & "m" & strSec & "s"
End Function

'The function below returns an array
Function fn_getBrowserArray()
Dim k,arrBrowserProcess(), i
i = 0
For k = 1 To 3

DataTable.SetCurrentRow(k)
If DataTable.Value("Browsers",TCsheet) <> "" And DataTable.Value("Browsers",TCsheet) <> Empty Then
ReDim Preserve arrBrowserProcess(i)
arrBrowserProcess(i) = DataTable.Value("Browsers",TCsheet)
i = i +1
End If
Next
fn_getBrowserArray = arrBrowserProcess
End Function

'This function returns an object
'ADDED
''' <summary>
''' QTP Tutorial
''' A dynamic function to return any button based on the properties that are passed to it.
''' The properties may be passed as a string or as an array
''' It checks that the button exists and reports a pass or fail for the button and will capture a screenshot so that
''' each object's condition through the application may be known
''' </summary>
''' <param name="strProperties" type="array or string"> An array or string representing the properties for the object.
''' </param>
''' <returns type="Object"> The button if it is found, otherwise, nothing. If you want to do stuff to the object
''' check to make sure that it is an object, if it is, then that means it was found </returns>
''' <example>
''' 'arr(0) = "name:=Resources"
''' 'arr(1) = "text:=Resources"
''' Set object = fn_getWebElement (arr) OR Set object = fn_getWebElement("name:=Resources,text:=Resources")
''' object.someMethod
''' </example>
Function fn_getWebElement(strProperties)
Print "---------- fn_getWebElement ---------" & strProperties
Dim browserPage,obj
Set browserPage = browser(strIndex0).Page(strTitleDescr)
'create the object description so that I can use it to access the object
fn_createDescription "objWebElement",strProperties
Set obj = browserPage.WebElement(objWebElement)

If Environment("debugMode") Then
strResult = fn_getObjectExistence(obj, "GetWebElement_" & strProperties, 10, False,"", "")
Else
strResult = fn_getObjectExistence(obj, "GetWebElement_" & fn_removeSpecialCharacters(strProperties), 10, False, environment("ResultsPath"), DataTable.value("TCID",TCsheet))
End If

If strResult = p Then
Set fn_getWebElement = obj
Else
Set fn_getWebElement = Nothing
End If
End Function
'''''------- CODE END
'''------- CODE END

Pin It on Pinterest

Clef two-factor authentication