Most commonly used functions in QTP

'Close all browsers: IE, Firefox, and Chrome

Function CloseAllBrowsers()
Dim WshShell, oExec
Set WshShell = CreateObject("WScript.Shell")
Set oExec = WshShell.Exec("taskkill /f /im iexplore.exe")
Set oExec = WshShell.Exec("taskkill /f /im firefox.exe")
Set oExec = WshShell.Exec("taskkill /f /im chrome.exe")
Wait 2
End Function

'############################################################################

'Open the application URL

Function Login(URL)
Const SHOW_MAXIMIZED = 3
SystemUtil.Run "iexplore.exe", URL , , , SHOW_MAXIMIZED
End Function

'############################################################################

'Function to create a unique number which can be used while created any unique user

Function DateTimeStamp()
Dim yr, mth, dy, dateTime, dateTimeStamp1
dy = DatePart("D", Now)
If Len(dy) < 2 Then
    dy = "0" & dy
End If
mth = DatePart("M", Now)
If Len(mth) < 2 Then
    mth = "0" & mth
End If
dateTime =  dy & mth
Dim  arrDateTime, arrTime, UniqueValue
arrDateTime=Split(Now," ")
arrTime=Split(arrDateTime(1),":")
UniqueValue=arrTime(0)&arrTime(1)&arrTime(2)&dateTime
DateTimeStamp = UniqueValue
End Function

'Or you can use below code

'Create a unique number based on date and time
Daay = (Day(date()))
Monnth = (Month(Date()))
Yeaar = (Year(Date()))
Timee = Cstr(Time)
Strr1 = Replace (Timee, ":","")
Strr2 = Replace (Strr1, "AM","")
Strr3 = Trim (Replace (Strr2, "PM",""))
UniqueNumber = Daay&Monnth&Yeaar&Strr3
Msgbox UniqueNumber

'############################################################################

' Function to close the duplicate browser

Function CloseLatestOpenedBrowser()     
Dim oDescription  
Dim BrowserObjectList  
Dim oLatestBrowserIndex     
Set oDescription=Description.Create  
oDescription("micclass").value="Browser"  
Set BrowserObjectList=Desktop.ChildObjects(oDescription)  
oLatestBrowserIndex=BrowserObjectList.count-1     
Browser("creationtime:="&oLatestBrowserIndex).close     
Set oDescription=Nothing  
Set BrowserObjectList=Nothing     
End Function 

'############################################################################

' Get path of the root folder

'Specify the parent folder
FilePath = CreateObject("Scripting.FileSystemObject").GetAbsolutePathName(".")
folderspec = FilePath & "\Folders"

set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder(folderspec)
Set sf = f.SubFolders
LocateFolder1 = folderspec
Do while sf.Count >0
   For Each f1 in sf
      LocateFolder1 = LocateFolder1 & "\" & f1.name
      Fold = LocateFolder1         
   Exit For
Next
   Set f = fso.GetFolder(LocateFolder1)
   Set sf = f.SubFolders
Loop
Msgbox Fold
Set fso = Nothing
Set f = Nothing
Set sf = Nothing

'############################################################################

'Get root folder count, names, and their paths

'Specify the parent folder
FilePath = CreateObject("Scripting.FileSystemObject").GetAbsolutePathName(".")
strFolderPath = FilePath & "\Folders"

Set objFso = Createobject("Scripting.FileSystemObject")
Set objFolder = objFso.GetFolder(strFolderPath)
Set objFolders = objFolder.SubFolders
'Get number of folders in the Parent folder
intfoldercount = objFolders.count
Msgbox "Folder Counts: " & intfoldercount
'Collecting the name of the subfolders
For each objfold in objFolders
    strSubFoldername=objfold.Name
    msgbox "Folder Names: " & strSubFoldername
    'Collecting the path of the subfolders
    strSubFolderPath=objfold.path
    msgbox "Folder path: " & strSubFolderPath
Next

'############################################################################

'Database connectivity - Use database values as input parameters in your script

Option Explicit
Dim ObjConnection,ObjRecordSet,ServerName,server
'Create an Object in DB Connection Class that cab be used to Connect to DB
Set ObjConnection=CreateObject("Adodb.Connection")
'Create an Object in DB Recordset Class, that can be used to Perfotm Operation on DB Tables(Records)
Set ObjRecordSet=CreateObject("Adodb.Recordset")
'Exstablishing Connection Sring for SQL Server
server = "bil-s-sql05\staging2,1478"
ServerName="DRIVER={SQL SERVER}; Server=" & server & ";UserName=Corpnet\amol.kanthe;Password=Singa1pur222;DATABASE=VendorDB"
ObjConnection.Open ServerName
'Fetching the Test Data using the Recordset object
ObjRecordSet.Open "select top 5 * from dbo.Vendors where Name > 'LQITestVendor5000' and Name < 'LQITestVendor6000';",ObjConnection
Do while not ObjRecordSet.eof
    msgbox(ObjRecordSet.Fields("VendorID"))
Browser("name:= Expert Database - Search Experts").page("title:= Expert Database - Search Experts").WebEdit("html id:=ctl00_CB_tbVendorID","html tag:=INPUT").Set ObjRecordSet.Fields("VendorID")
Browser("name:= Expert Database - Search Experts").page("title:= Expert Database - Search Experts").Link("html id:=ctl00_CB_btnSearch1","html tag:=A","innerhtml:=Search").Click
'wait 30
ObjRecordSet.movenext
Loop
'Release objects'Release objects
Set ObjRecordSet= nothing
Set ObjConnection= nothing

'############################################################################

'Get Screen Resolution of the system

Set objWMIService = GetObject("Winmgmts:\\.\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * From Win32_DesktopMonitor where DeviceID = 'DesktopMonitor1'",,0)
    For Each objItem in colItems
        intHorizontal = objItem.ScreenWidth
        intVertical = objItem.ScreenHeight
    msgbox intHorizontal
    msgbox intVertical
    Next
   
'############################################################################

'Sort array in decendiing order

'Script 1
ArrayOfTerms = Array (40,50,15,8,100)

Call SortArray (ArrayOfTerms, "Ascd")

Function SortArray(ArrayOfTerms, SortType)
For a = UBound(ArrayOfTerms) -1 To -1 Step -1
    for j= 0  to a
    If SortType = "Ascd" Then
        if ArrayOfTerms(j)<ArrayOfTerms(j+1) then
            temp=ArrayOfTerms(j+1)
            ArrayOfTerms(j+1)=ArrayOfTerms(j)
            ArrayOfTerms(j)=temp                 
        end if
    Else
         if ArrayOfTerms(j)>ArrayOfTerms(j+1) then
            temp=ArrayOfTerms(j+1)
            ArrayOfTerms(j+1)=ArrayOfTerms(j)
            ArrayOfTerms(j)=temp                
        end if
    End If       
    next   
MsgBox ArrayOfTerms(j) 
next
End Function

'Script 2
ArrayOfTerms = Array (40,50,15,8,100)
For a = UBound(ArrayOfTerms) -1 To -1 Step -1
    for j= 0  to a
    if ArrayOfTerms(j)<ArrayOfTerms(j+1) then
            temp=ArrayOfTerms(j+1)
            ArrayOfTerms(j+1)=ArrayOfTerms(j)
            ArrayOfTerms(j)=temp                
    end if          
    next
MsgBox ArrayOfTerms(j)     
next

'############################################################################

'Get Internet Explorer Browser Version
IEversion = Browser("name:=Google").GetROProperty("application version")

'Or you can use below code, if you are not sure about the Browser name
IEversion = Browser("creationtime:=0").GetROProperty("application version")

'############################################################################

'Open an email in outlook with specific subject

'Open the Inbox and select the mail item
Dim olMailItem
'Create an Outlook Application
Set Application = CreateObject("Outlook.Application")
Set oInbox = Application.GetNameSpace("MAPI").GetDefaultFolder(6)
'Count the total number of items present in the outlook Inbox
Set oItems = oInbox.items
val  = oItems.Count
'Msgbox val
For ICounter =val to 1 Step - 1
    If oItems(ICounter).Subject = "[S2]Service Partner Portal  Please activate your account" or  oItems(ICounter).Subject = "[S2]Service Partner Portal  Account created" or oItems(ICounter).Subject = "[S1]Service Partner Portal  Please activate your account" or oItems(ICounter).Subject = "[S1]Service Partner Portal  Account created" then
        Set oFirst = oItems.Item(ICounter)
        oFirst.Display
Exit For
    Else
       'Reporter.ReportEvent  micFail, "Mail Notification","Mail Notification was not found in the Outlook inbox. Step failed"
    End if
Next
Set Application = Nothing
Set oInbox = Nothing

Comments

Popular Posts

Demystifying Automation Frameworks: A Comprehensive Guide to Building Scalable Solutions

The Art of Payments Testing: Ensuring Seamless and Secure Transactions

Guide to Database Testing

Elevating Your Automation: Best Practices for Effective Test Automation

Mastering Java Collections: Your Secret Weapon for Robust Automation Frameworks