Labels

Sunday 30 September 2012

QTP - DB Functions (MySQL DB)



'' DB Functions for MySql DB

Set dbObj = CreateObject("ADODB.Connection")
Set myrecordset = CreateObject("ADODB.Recordset")
Set outputSheet = DataTable.AddSheet("output")

'' Connection string as below
conString = "DATABASE=testDB;DRIVER={MySQL ODBC 5.1 Driver};OPTION=0;PWD=sundar;PORT=3306;SERVER=127.0.0.1;UID=root"
dbObj.open conString
strSQL = "Select * from emp"
myrecordset.open strSQL, dbObj, adOpenStatic
print myrecordset.Fields.Count

myrecordset.MoveFirst

While not myrecordset.EOF
print "Employee Name:" & myrecordset.Fields("empname").value ''' By Column name
print "Employee ID:" & myrecordset.Fields(1).value '' By Index. Note: Index starts with 0
myrecordset.MoveNext
Wend

'' Method 2- Use connection object directly to get the recordset
Dim conRecordSet
Set conRecordSet = dbObj.Execute(strSQL)
'print conRecordSet.Count

'' Exporting the results to DataTable
''  Adding column names
For each fld in conRecordSet.Fields
print fld.name
outputSheet.AddParameter Replace(fld.name," ","_"), fld.value
Next

conRecordset.MoveFirst
i =1
While not conRecordset.EOF
For each fld in conRecordset.Fields
print "Adding"&fld.name & fld.value
outputSheet.SetCurrentRow(i)
outputSheet.GetParameter(Replace(fld.name," ","_")).value = fld.value
Next
i = i+1
conRecordset.MoveNext
Wend

'' closing the DB connection
dbObj.close
Set dbObj = Nothing
Set myrecordset = Nothing


Useful Links http://www.connectionstrings.com/mysql

Wednesday 26 September 2012

QTP - Handling Files and Folders

QTP - Handling Files and Folders

Useful Methods:

Couple of useful methods available:
  • GetFolder
  • FolderExists
  • CopyFolder
  • FileExists

Public Function fileFn(ByVal fpath, ByVal fileName)

   Dim objnewFile
   Dim filePathName
  
   Set fso= CreateObject ("Scripting.FileSystemObject")
   print "******************************"
    '' Checking whether folder exits , otherwise create a new one
   If fso.FolderExists(fpath) Then
       print "Folder"&"-"&fpath&"..Exists"
    Else
        print "Folder does not exists"
        print "creating new folder"&"--"&fpath
        fso.createFolder(fpath)
   End If

    Set objFolder = fso.GetFolder(fpath)
    Set objFiles = objFolder.Files
    Set objSubFolder = objFolder.SubFolders
    print "Number of sub folders-"& objSubFolder.count
    print "Number of files-"& objFiles.count
    '' print the subfolders
    For each subfolder in objSubFolder
        print subfolder
    Next
    ' Get file name and last modified date and time
    For each nFile in objFiles
        print nFile &"--"& nFile.Path &"--"& nFile.DateLastModified
     Next
    print "New File"&fpath&"\"&fileName

    Set fso = Nothing
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set objnewFile = Nothing
    filePathName = fpath&"\"&fileName
    print filePathName

    '' check file exists , if not create and append text
    If fso.FileExists(filePathName) Then
        Set objnewFile= fso.OpenTextFile (filePathName, 8, True) ' 8 - For appending
        objnewFile.writeLine "test555"
    else
        Set objnewFile=fso.createtextfile(filePathName)
        '        Set objTxt = fso.opentextfile (fpath&"\"&fileName, Forappending, true)
        objnewFile.writeLine "test"
    End If
    ''' Copying the file
    fso.CopyFile filePathName,filePathName&"1"
   
Set fso = Nothing
Set objFolder = Nothing
Set objFiles = Nothing
Set objnewFile = Nothing
Set objTxt = Nothing

End Function

res = fileFn("c:\data","newfile66.txt")
''fileFn("c:\data4")

Monday 24 September 2012

QTP - Descriptive Programming (DP)

Driver Script

' Associate the library file via Test > Settings > Resources Tab 
'or Loading the library file from the driver script
ExecuteFile "C:\data\QTP scripts\DP_lib"

'Calling the function as simple as below
launchApp
loginApp
newOrder
enumerateAllObjects
'Main test ends here


Create a library file 'DP_lib'


'' Descriptive programming

Public Function launchApp
' Close all the existing instances of flight application
SystemUtil.CloseProcessByName "flight4a.exe"

' Launch the flight application
SystemUtil.Run "C:\Program Files\Mercury Interactive\QuickTest Professional\samples\flight\app\flight4a.exe"

End Function

Public Function loginApp

' Login to the application
Dialog("regexpwndtitle:=Login").Activate
Dialog("regexpwndtitle:=Login").WinEdit("attached text:=Agent Name:").Set "Sundar"
Dialog("regexpwndtitle:=Login").WinEdit("attached text:=Password:").Set "mercury"
Dialog("regexpwndtitle:=Login").WinButton("text:=OK").Click

'Reporting the login status to the results file
If Window("regexpwndtitle:=Flight Reservation").Exist(10) Then
reporter.ReportEvent micPass, "Is Login Successful","Logged in successfully"
Else
reporter.ReportEvent micFail, "Is Login Successful","Login failed"
End If

End Function

Public Function newOrder

Window("regexpwndtitle:=Flight Reservation").Activate
' Use of  'With'- Try to use With often as it helps to improve the performance
With Window("regexpwndtitle:=Flight Reservation")
.Activate
.WinMenu("menuobjtype:=2").Select "File;New Order"
.ActiveX("progid:=MSMask.MaskEdBox.1").Type "092012"
.WinComboBox("attached text:=Fly From:").Select "Denver"
.WinComboBox("attached text:=Fly To:").Select "London"
.WinButton("text:=FLIGHT").Click
   End with
 
End Function

Public Function enumerateAllObjects
' Description object will allow you to navigate through all child objects or particular class of child objects

' Enumerating through allChild Objects of Flight Reservation Window
    Set dpAllChilds = Description.Create
Set allChilds = Window("regexpwndtitle:=Flight Reservation").ChildObjects(dpAllChilds)
iCount= allChilds.Count -1
reporter.ReportEvent micInfo, "Total Number of child objects", iCount+1

For i=0 to iCount
reporter.ReportEvent micInfo, "Child Object:"&i, allChilds.item(i).GetTOProperty("micclass") ''''&allChilds.item(i).GetTOProperty("text")
Next

' Enumerating through WinComboBox Objects of Flight Reservation Window
Set dpWinComboBox = Description.Create
dpWinComboBox("micclass").Value = "WinComboBox"
Set allWinComboBoxChilds = Window("regexpwndtitle:=Flight Reservation").ChildObjects(dpWinComboBox)
iCount = allWinComboBoxChilds.Count
reporter.ReportEvent micInfo, "Total Number of WinComboBox objects", iCount

' Enumerating through WinEdit Objects of Flight Reservation Window
Set dpWinEdit = Description.Create
dpWinEdit("micclass").Value = "WinEdit"
Set dpWinEditChilds = Window("regexpwndtitle:=Flight Reservation").ChildObjects(dpWinEdit)
iCount = dpWinEditChilds.Count
reporter.ReportEvent micInfo, "Total Number of WinComboBox objects", iCount

End Function


Saturday 15 September 2012

QTP - Actions Vs Functions

Dear Friends,



In most of the QTP interviews, you will get this question: What is the difference between Actions and Functions.

Few interviewers will be satisfied, when you say 
- Actions are only available in QTP whereas Functions are available in both QTP and VBScript.
- Each action would have its own Object repository and Data table whereas Function does have it.

And if you go further and explains below points, I am sure you will get 100/100 :-)
- When parameters are used in actions, input parameters should be defined first and then followed by output parameters.
- A same parameter cannot be used for both input and output purposes whereas in function we can use it
- An array or object cannot be a parameter for Actions

BTW, don't forget the commonalities between Actions and Functions :
- Both are mainly used to increase the re-usability and helps to divide your tests into logical units or business processes
- Both are optionally accepts input and output parameters

Example:
Write a Function which returns more than one value in VBScript/QTP

Public Function myNumbers ()
   Dim arr(2)
   arr(0) =10
   arr(1) = 20
   myNumbers = arr
End Function

x = myNumbers()
print x(0)
print x(1)


P.S: This is my first write up in online blog, So I would like to get your feedback. Feel free to post your comments.Thanks




Book reference: "QTP Unplugged - By Tarun Lalwani"