QTP Framework:- Generic Functions examples
Generic Functions
1. Find the
difference between two time component and return the result in the format
2. Different
ways of launching applications.
3. Different
wait options.
4. Click on
any button.
5. Enter
data.
6.
CHECKORUNCHECK radio button
'####################################################################################
'# Purpose:
Find the difference between two time component and return the result in the
format
'# xxHours yyMins zzSecs
'#
Inputs: stTime (Datatype Time, Start
time), edTime (Datatype Time, End Time)
'#
Outputs: Datatype String, format xx
hours yy mins zz seconds
'####################################################################################
Public Function TimeDiff(byval stTime,
byval edTime)
Dim ts
logger.LogInfo "TimeDiff
start"
ts = DateDiff("s",stTime,
edTime)
TimeDiff = TimeString(ts)
If Err.Number <> 0 Then
logger.LogError "TimeDiff
" & Err.Number & " " & Err.Description
End If
logger.LogInfo "TimeDiff end"
End Function
Public Function TimeString(byval ts)
Dim th, tm, tsec
logger.LogInfo "TimeString
start"
th = CInt(ts / 3600)
tm = CInt((ts - th * 3600) / 60)
If tm < 0 Then tm = 60 - Abs(tm): th
= th - 1
tsec = CInt(ts - th * 3600 - tm * 60)
If tsec < 0 Then tsec = 60 -
Abs(tsec): tm = tm - 1
TimeString = ""
If th <> 0 Then
TimeString = Time_s & th &
" Hour"
End If
If tm <> 0 Then
TimeString = TimeString &
" " & tm & " Min"
End If
If tsec <> "" Then
TimeString = TimeString &
" " & tsec & " Sec"
End If
If Err.Number <> 0 Then
logger.LogError "TimeString
" & Err.Number & " " & Err.Description
End If
logger.LogInfo "TimeString
end"
End Function
2. Different
ways of launching applications.
Ø SystemUtil.Run
Ø InvokeApplication - InvokeApplication "C:\Program
Files\Internet Explorer\IEXPLORE.EXE http://www.yahoo.com"
Ø VBscript to invoke application
1. Create a
"WScript.shell" object.
2. Use the
"run" object to launch the application. If the path to your
executable contains spaces, use Chr(34) to ensure the path is contained within
double quotes.
3. When
done, set the shell object to nothing to release it.
Example:
Dim
oShellSet oShell = CreateObject ("Wscript.shell")'
Example 1 -
run a batch file:oShell.run "F:\jdk1.3.1\demo\jfc\SwingSet2.bat"'
Example 2 -
run a Java jar file:oShell.run "java -jar
F:\jdk1.3.1\demo\jfc\SwingSet2\SwingSet2.jar"'
Example 3 -
launch Internet Explorer:oShell.Run Chr(34) & "C:\Program
Files\Internet Explorer\IEXPLORE.EXE" & Chr(34)Set oShell = Nothing
Dim oShell
Set oShell =
CreateObject ("Wscript.shell")'
oShell.Run
Chr(34) & "C:\Program Files\Internet Explorer\IEXPLORE.EXE"
oShell.RUn
"www.yahoo.com"
Set oShell =
Nothing
Dim oShell
Set oShell =
CreateObject ("Wscript.shell")'
oShell.Run "www.google.com"
Set oShell =
Nothing
Set abc =
CreateObject("WScript.Shell")
abc.Run("iexplore
http://yahoo.com")
Set abc =
CreateObject("WScript.Shell")
abc.Run("firefox
http://yahoo.com")
Set IE =
CreateObject("InternetExplorer.Application")
IE.Navigate
“http:// Deriwiser/login.aspx”
Do While
IE.Busy
Debug.WriteLine
"busy"
Loop
IE.visible =
True
set obj =
createobject("internetexplorer.application")
obj.navigate
"http://www.google.com"
Do while
obj.busy
wait(1)
Loop
obj.visible
= "TRUE"
'Systemutil.Run("IEXPLORE.EXE")
'Browser("Browser").Navigate
"http://www.gmail.com"
'Browser("Browser").Page("Gmail:
Email from Google").Sync
'msgbox
"p"
Ø Trivial but useful method
If nothing
works out you might try this
You can use
the Start -> Run dialog of Windows.
1. Add the
Windows Start button to the Object Repository using the "Add Objects"
button in Object Repository dialog.
2. Open the
Run dialog (Start -> Run), and learn the "Open" edit field and the
"OK" button into the Object Repository.
3. Switch to
the Expert View, and manually add the lines to open the Run dialog.
Example:Window("Window").WinButton("Button").ClickWindow("Window").Type("R")
4. Manually
enter the lines to enter the information to launch the application, and click
the "OK" button of the Run dialog.
Example:
Dialog("Run").WinEdit("Open:").Type
"c:\WINNT\system32\notepad.exe"
Dialog("Run").WinButton("OK").Click
3.
Wait(seconds)
4. Case
"CLICK"
strActionCmd = strObjectName + ".Click"
a=inputbox
("hi")
abc(a)
'enter a as
'Browser("Browser").Page("Gmail: Email from
Google").WebButton("Sign in")'
Function
abc(byval strObjectName)
strActionCmd = strObjectName + ".Click"
Execute
strActionCmd
End Function
5. Enter
Case
"ENTER"
If ucase(strInput) =
"""NONE""" Then
strActionCmd =
strObjectName + ".Set"
ElseIf ucase(strInput)
= """<BLANK>""" Then
strActionCmd = strObjectName
+ ".Set """""
ElseIf ucase(strInput)
= """<NONE>""" Then
strActionCmd =
strObjectName + ".Set ""NONE"""
Else
strActionCmd =
strObjectName + ".Set " + strInput
End If
6.
CHECKORUNCHECK radio button
'strInput
should be ON or OFF
Case "CHECKORUNCHECK"
strActionCmd = strObjectName + ".Set " &
strInput