SlideShare ist ein Scribd-Unternehmen logo
1 von 6
www.gcreddy.com
       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
SettingsbannuDesktopgcreddy.mdb"
'Fetching Test Data using RecordSet Object
objRecordSet.Open "Select * From Login",objConnection

Do Until objRecordSet.EOF=True
SystemUtil.Run "C:Program FilesHPQuickTest
Professionalsamplesflightappflight4a.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




                      www.gcreddy.net
www.gcreddy.com
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 SettingsTest
classDesktopFlights.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
SettingsTest classDesktopdata1.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 SettingsTest
classDesktopFlights.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")


                   www.gcreddy.net
www.gcreddy.com
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
SettingsTest classDesktopdata1.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 SettingsTest
classDesktopFlights.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


                      www.gcreddy.net
www.gcreddy.com
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 SettingsTest
classDesktopdata1.txt",2)

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

objConnection.Provider = ("Microsoft.ACE.OLEDB.12.0")
objConnection.Open "C:Documents and SettingsTest
classDesktopFlights.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


                      www.gcreddy.net
www.gcreddy.com
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 SettingsTest
classDesktopdat1.txt",1)
objConnection.Provider = ("Microsoft.ACE.OLEDB.12.0")
objConnection.Open "C:Documents and SettingsTest
classDesktopFlights.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


                      www.gcreddy.net
www.gcreddy.com
objConnection.Close
Set objConnection = Nothing




                   www.gcreddy.net

Weitere ähnliche Inhalte

Andere mochten auch

Automation testing material by Durgasoft,hyderabad
Automation testing material by Durgasoft,hyderabadAutomation testing material by Durgasoft,hyderabad
Automation testing material by Durgasoft,hyderabadDurga Prasad
 
New features in qtp11
New features in qtp11New features in qtp11
New features in qtp11G.C Reddy
 
How Manual Testers Can Break into Automation Without Programming Skills
How Manual Testers Can Break into Automation Without Programming SkillsHow Manual Testers Can Break into Automation Without Programming Skills
How Manual Testers Can Break into Automation Without Programming SkillsRanorex
 
Manual Testing Notes
Manual Testing NotesManual Testing Notes
Manual Testing Notesguest208aa1
 
Qtp 92 Tutorial
Qtp 92 TutorialQtp 92 Tutorial
Qtp 92 Tutorialsasidhar
 
Manual Testing Material by Durgasoft
Manual Testing Material by DurgasoftManual Testing Material by Durgasoft
Manual Testing Material by DurgasoftDurga Prasad
 
Manual testing concepts course 1
Manual testing concepts course 1Manual testing concepts course 1
Manual testing concepts course 1Raghu Kiran
 
Testing documents
Testing documentsTesting documents
Testing documentssuhasreddy1
 
Manual QA Testing Interview Questions From H2KInfosys
Manual QA Testing Interview Questions From H2KInfosysManual QA Testing Interview Questions From H2KInfosys
Manual QA Testing Interview Questions From H2KInfosysH2kInfosys
 
Manual testing-training-institute-in-marathahalli
Manual testing-training-institute-in-marathahalliManual testing-training-institute-in-marathahalli
Manual testing-training-institute-in-marathahallisiyaram ray
 
Test Life Cycle - Manual Testing Concept.
Test Life Cycle - Manual Testing Concept.Test Life Cycle - Manual Testing Concept.
Test Life Cycle - Manual Testing Concept.guestf9bc
 
01. testing fresher-resume
01. testing fresher-resume01. testing fresher-resume
01. testing fresher-resumemuqtar12
 
TESTING LIFE CYCLE PPT
TESTING LIFE CYCLE PPTTESTING LIFE CYCLE PPT
TESTING LIFE CYCLE PPTsuhasreddy1
 

Andere mochten auch (15)

Ppt Qtp
Ppt QtpPpt Qtp
Ppt Qtp
 
Automation testing material by Durgasoft,hyderabad
Automation testing material by Durgasoft,hyderabadAutomation testing material by Durgasoft,hyderabad
Automation testing material by Durgasoft,hyderabad
 
New features in qtp11
New features in qtp11New features in qtp11
New features in qtp11
 
How Manual Testers Can Break into Automation Without Programming Skills
How Manual Testers Can Break into Automation Without Programming SkillsHow Manual Testers Can Break into Automation Without Programming Skills
How Manual Testers Can Break into Automation Without Programming Skills
 
Manual Testing Notes
Manual Testing NotesManual Testing Notes
Manual Testing Notes
 
Qtp 92 Tutorial
Qtp 92 TutorialQtp 92 Tutorial
Qtp 92 Tutorial
 
Manual Testing Material by Durgasoft
Manual Testing Material by DurgasoftManual Testing Material by Durgasoft
Manual Testing Material by Durgasoft
 
Manual testing concepts course 1
Manual testing concepts course 1Manual testing concepts course 1
Manual testing concepts course 1
 
Testing documents
Testing documentsTesting documents
Testing documents
 
Manual QA Testing Interview Questions From H2KInfosys
Manual QA Testing Interview Questions From H2KInfosysManual QA Testing Interview Questions From H2KInfosys
Manual QA Testing Interview Questions From H2KInfosys
 
Manual testing-training-institute-in-marathahalli
Manual testing-training-institute-in-marathahalliManual testing-training-institute-in-marathahalli
Manual testing-training-institute-in-marathahalli
 
Test Life Cycle - Manual Testing Concept.
Test Life Cycle - Manual Testing Concept.Test Life Cycle - Manual Testing Concept.
Test Life Cycle - Manual Testing Concept.
 
Fresher testing cv
Fresher testing cvFresher testing cv
Fresher testing cv
 
01. testing fresher-resume
01. testing fresher-resume01. testing fresher-resume
01. testing fresher-resume
 
TESTING LIFE CYCLE PPT
TESTING LIFE CYCLE PPTTESTING LIFE CYCLE PPT
TESTING LIFE CYCLE PPT
 

Mehr von G.C Reddy

QTP Training
QTP TrainingQTP Training
QTP TrainingG.C Reddy
 
Software+struc+doc
Software+struc+docSoftware+struc+doc
Software+struc+docG.C Reddy
 
Qtp (advanced)
Qtp (advanced)Qtp (advanced)
Qtp (advanced)G.C Reddy
 
Qtp (basics to advanced)
Qtp (basics to advanced)Qtp (basics to advanced)
Qtp (basics to advanced)G.C Reddy
 
Object Repository
Object RepositoryObject Repository
Object RepositoryG.C Reddy
 
Advanced Qtp
Advanced QtpAdvanced Qtp
Advanced QtpG.C Reddy
 
Advanced Qtp Book
Advanced Qtp BookAdvanced Qtp Book
Advanced Qtp BookG.C Reddy
 
File System Operations
File System OperationsFile System Operations
File System OperationsG.C Reddy
 
Web Dictionary
Web DictionaryWeb Dictionary
Web DictionaryG.C Reddy
 

Mehr von G.C Reddy (13)

Html
HtmlHtml
Html
 
QTP Training
QTP TrainingQTP Training
QTP Training
 
Software+struc+doc
Software+struc+docSoftware+struc+doc
Software+struc+doc
 
Qtp (advanced)
Qtp (advanced)Qtp (advanced)
Qtp (advanced)
 
Qtp (basics to advanced)
Qtp (basics to advanced)Qtp (basics to advanced)
Qtp (basics to advanced)
 
Object Repository
Object RepositoryObject Repository
Object Repository
 
Advanced Qtp
Advanced QtpAdvanced Qtp
Advanced Qtp
 
Advanced Qtp Book
Advanced Qtp BookAdvanced Qtp Book
Advanced Qtp Book
 
File System Operations
File System OperationsFile System Operations
File System Operations
 
Qtp Faq
Qtp FaqQtp Faq
Qtp Faq
 
Qtp Summary
Qtp SummaryQtp Summary
Qtp Summary
 
Qtp Scripts
Qtp ScriptsQtp Scripts
Qtp Scripts
 
Web Dictionary
Web DictionaryWeb Dictionary
Web Dictionary
 

Kürzlich hochgeladen

Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptxSherlyMaeNeri
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYKayeClaireEstoconing
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfSpandanaRallapalli
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 
Q4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxQ4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxnelietumpap1
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4MiaBumagat1
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfTechSoup
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxAshokKarra1
 
Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxMaryGraceBautista27
 

Kürzlich hochgeladen (20)

Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptx
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdf
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 
Q4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxQ4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptx
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
 
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptxFINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptx
 
Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptx
 

QTP Database Script Examples

  • 1. www.gcreddy.com 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 SettingsbannuDesktopgcreddy.mdb" 'Fetching Test Data using RecordSet Object objRecordSet.Open "Select * From Login",objConnection Do Until objRecordSet.EOF=True SystemUtil.Run "C:Program FilesHPQuickTest Professionalsamplesflightappflight4a.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 www.gcreddy.net
  • 2. www.gcreddy.com 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 SettingsTest classDesktopFlights.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 SettingsTest classDesktopdata1.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 SettingsTest classDesktopFlights.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") www.gcreddy.net
  • 3. www.gcreddy.com 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 SettingsTest classDesktopdata1.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 SettingsTest classDesktopFlights.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 www.gcreddy.net
  • 4. www.gcreddy.com 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 SettingsTest classDesktopdata1.txt",2) myFile.WriteLine "Agent "&" Password" myFile.WriteLine "--------------------" objConnection.Provider = ("Microsoft.ACE.OLEDB.12.0") objConnection.Open "C:Documents and SettingsTest classDesktopFlights.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 www.gcreddy.net
  • 5. www.gcreddy.com 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 SettingsTest classDesktopdat1.txt",1) objConnection.Provider = ("Microsoft.ACE.OLEDB.12.0") objConnection.Open "C:Documents and SettingsTest classDesktopFlights.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 www.gcreddy.net