QTP Scripting


 QTP Database Script Examples
-----------------------------------------------------------------------------
1) Data Driven Testing for Login Operation by Fetching Test Data directly From a Database

Option Explicit
Dim objConnection, objRecordSet
'Creating an Automation Object in Database Connection Class, that can be used to connect to Databases
Set objConnection=CreateObject("Adodb.Connection")
'Creating an Automation Object in Database RecordSet Class, that can be used to Perform Operations on Database Tables (Records)
Set objRecordSet=CreateObject("Adodb.RecordSet")
'Establishing Connection String for MS Access Database
objConnection.Provider=("Microsoft.Jet.OLEDB.4.0")
objConnection.Open "C:\Documents and Settings\bannu\Desktop\gcreddy.mdb"
'Fetching Test Data using RecordSet Object
objRecordSet.Open "Select * From Login",objConnection

Do Until objRecordSet.EOF=True
SystemUtil.Run "C:\Program Files\HP\QuickTest Professional\samples\flight\app\flight4a.exe"
Dialog("text:=Login").Activate
Dialog("text:=Login").Winedit("attached text:=Agent Name:").Set objRecordSet.Fields("Agent")
Dialog("text:=Login").Winedit("attached text:=Password:").Set objRecordSet.Fields("Password")
Wait 2
Dialog("text:=Login").Winbutton("text:=OK","width:=60").Click
Window("text:=Flight Reservation").Close
objRecordSet.MoveNext
Loop

objRecordSet.Close
objConnection.Close

Set objRecordSet=Nothing
Set objConnection=Nothing

 2) Write Data to a Database Table

Dim objConnection, objCommand
Set objConnection=CreateObject("Adodb.Connection")
Set objCommand=CreateObject("Adodb.Command")
objConnection.Provider=("Microsoft.ACE.OLEDB.12.0")
objConnection.Open "C:\Documents and Settings\Test class\Desktop\Flights.mdb"

objCommand.ActiveConnection=objConnection

objCommand.CommandText ="Insert into Login values (8,'Chennai','Mercury')"
objCommand.Execute

objConnection.Close
Set objCommand=Nothing
Set objConnection=Nothing

-----------------------------------------------------
3) Write Multiple Sets of Data to a Database Table

Dim objConnection, objCommand
Dim objExcel, objWorkBook, objWorksheet

Set objExcel=CreateObject("Excel.Application")
Set objWorkBook=objExcel.Workbooks.Open ("C:\Documents and Settings\Test class\Desktop\data1.xls")
Set objWorkSheet=objWorkBook.Worksheets("Sheet1")

Set objConnection=CreateObject("Adodb.Connection")
Set objCommand=CreateObject("Adodb.Command")

objConnection.Provider=("Microsoft.ACE.OLEDB.12.0")
objConnection.Open "C:\Documents and Settings\Test class\Desktop\Flights.mdb"

objCommand.ActiveConnection=objConnection
Rows_Count=objWorkSheet.Usedrange.Rows.Count
For i= 2 To Rows_Count Step 1
SNO=objWorkSheet.Cells(i,"A")
Agent=objWorkSheet.Cells(i,"B")
Password=objWorkSheet.Cells(i,"C")
objCommand.CommandText ="Insert into Login values ('"&SNO&"','"&Agent&"','"&Password&"')"
objCommand.Execute
Next
objWorkBook.Close
objExcel.Quit
Set objExcel=Nothing
objConnection.Close
Set objCommand=Nothing
Set objConnection=Nothing

---------------------------------------------------------

4) Export Data from a Database Table to an Excel file (2nd Sheet)

Option Explicit
Dim objConnection, objRecordset
Dim objExcel, objWorkbook, objWorksheet, i
Set objConnection = Createobject ("Adodb.Connection")
Set objRecordset = Createobject ("Adodb.Recordset")

Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("C:\Documents and Settings\Test class\Desktop\data1.xls")
Set objWorksheet = objWorkbook.Worksheets("Sheet2")

objWorksheet.cells(1,"A") = "Agents"
objWorksheet.cells(1,"B") = "Password"

objConnection.Provider = ("Microsoft.ACE.OLEDB.12.0")
objConnection.Open "C:\Documents and Settings\Test class\Desktop\Flights.mdb"

objRecordset.Open "Select Agent, Password from Login", objConnection

i = 2 'Rows
Do Until objRecordset.EOF

objWorksheet.cells(i, "A") = objRecordset.Fields("Agent")
objWorksheet.cells(i, "B") = objRecordset.Fields("Password")
i = i + 1
objRecordset.MoveNext
Loop

objWorkbook.Save
objWorkbook.Close
objExcel.Quit
Set objExcel = Nothing

objRecordset.Close
objConnection.Close

Set objRecordset = Nothing
Set objConnection = Nothing

----------------------------------------------------
5) Export Data from a Database Table to a Text file

Option Explicit
Dim objConnection, objRecordset
Dim objFso,myFile
Set objConnection = Createobject ("Adodb.Connection")
Set objRecordset = Createobject ("Adodb.Recordset")

Set objFso = CreateObject("Scripting.Filesystemobject")
Set myFile=objFso.OpenTextFile("C:\Documents and Settings\Test class\Desktop\data1.txt",2)

myFile.WriteLine "Agent  "&" Password"
myFile.WriteLine "--------------------"

objConnection.Provider = ("Microsoft.ACE.OLEDB.12.0")
objConnection.Open "C:\Documents and Settings\Test class\Desktop\Flights.mdb"

objRecordset.Open "Select Agent, Password from Login", objConnection


Do Until objRecordset.EOF

myFile.WriteLine objRecordset.Fields("Agent")&" ," & objRecordset.Fields("Password")
objRecordset.MoveNext
Loop

myFile.Close
Set objFso=Nothing

objRecordset.Close
objConnection.Close

Set objRecordset = Nothing
Set objConnection = Nothing

------------------------------------------------
6) Export Data from a Text File to a Database Table

Option Explicit
Dim objConnection, objCommand
Dim objFso,myFile,myLine,myField,SNO,Agent,Password
Set objConnection = Createobject ("Adodb.Connection")
Set objCommand = Createobject ("Adodb.Command")

Set objFso = CreateObject("Scripting.Filesystemobject")
Set myFile=objFso.OpenTextFile("C:\Documents and Settings\Test class\Desktop\dat1.txt",1)
objConnection.Provider = ("Microsoft.ACE.OLEDB.12.0")
objConnection.Open "C:\Documents and Settings\Test class\Desktop\Flights.mdb"
myFile.SkipLine
myFile.SkipLine

Do While myFile.AtEndOfStream = FALSE
myLine = myFile.ReadLine
myField = Split(myLine,",")
SNO = myField(0)
Agent = myField(1)
Password = myField(2)

objCommand.ActiveConnection = objConnection
objCommand.CommandText = "Insert into Login2 values('"&SNO&"','"&Agent&"','"&Password&"')"
objCommand.Execute

Loop


myFile.Close
Set objFso=Nothing

Set  objCommand = Nothing
objConnection.Close
Set objConnection = Nothing

---------------------------------------------------------------
QTP Training in Scripting Orientation 

0 comments:

Post a Comment