Sie sind auf Seite 1von 74

V Today almost all the software

applications use relational database


management systems (RDBMS) to
provide persistency to the program.

V It
could be Oracle, SQL, Access and
MySQL
V Database Concepts

V RDBMS used for the present application


Visual diagram of all the tables in your database
How integrity is maintained among the tables

V SQL for querying the database


Client 1

WEB APPLICATION Database


SERVER SERVER

Client 2
SERVER
V Inserting Database Checkpoints

V Throughscripting to connect to the


database and test the records
V ¢sed to test the contents of the database
accessed by application under test.
V It stores the expected data and compares this
with the data stored in the database.
V When you use a database checkpoint in your
script, it connects to the database and sends
the query to the database to retrieve the
current data into the record set.
V QTP now compares the current data with the
expected data stored in the checkpoint and
gives you the result as pass or fail.
Take a simple example for Flight
Application.
V Suppose you are updating an order by
changing the name.
V If you want to verify whether the record is
properly updated with the changed name
or not, you will use the database
checkpoint.
To create a database checkpoint in QTP
V Go to Insert > Checkpoint > Database Checkpoint.
You will see a database query wizard.
V Select either of the two option there
‡ Create query using Microsoft query ² Select this if
you want to use Microsoft query.
‡ Specify SQL statement manually ² Select this to
manually provide the sql query to the database.
‡ Click ¶Next·.
V Click ¶Create· button, which will open the data source
window, Select ¶Machine Data Source· and click new.
Continued «
V Create New Data Source window opens.
Select the type of data source and click Next
V Select the Driver depending on the database type from the
list. For example if your database is SQL ² select ¶SQL
Server·, for Oracle ² select ¶Microsoft ODBC for Oracle· and
follow the onscreen wizard with the details of your database
like server name database name etc. Finally Test the
connection and press ¶OK·
V You will see the data source name just created in the list at
Machine Data source. Select and Click ¶OK·
V Specify your sql query e.g. for above mentioned example ²
¶Select Customer_Name from Orders·. Click Finish
V It will Open the Database Checkpoint Properties, modify
your checkpoint settings, enter the expected data and Click
¶OK·
V It
will add a line in the expert view as:
DbTable("DbTable").Check
CheckPoint("DbTable")

V When you will run the script, QTP will check


the database whether the record is updated
with the customer name or not and will give
you the result as pass or fail.
If you don·t want to use the database
checkpoint in your database testing, you
will have to script it to connect to
database and test the records.
To connect to the database, you need
V Create the object using the CreateObject method
Set MyConnection =
CreateObject(´ADODB.Connectionµ)

V Set the connecting string for database connection


Continued«
A typical connecting string for database connection
will be like
V O 
Provider=Microsoft.Jet.OLEDB.4.0;
Data Source=C:\mydatabase.mdb;¢ser
Id=admin;Password=;
V 2 2
Provider=sqloledb;Data Source=myServerAddress;
Initial Catalog=myDataBase;
¢ser Id=my¢sername;Password=myPassword;
V i 
Provider=msdaora;Data Source=MyOracleDB;
¢ser Id=my¢sername;Password=myPassword;
Continued«

V Once you have the connection string you can open the
connection using Open method of Connection object
MyConnection.Open ´ <Connection String>µ

V After performing the necessary actions, you have to


close the connection
MyConnection.Close
Dim oConnection,ConnectionString
ConnectionString ="Provider=Microsoft.Jet.OLEDB.4.0;
Data Source=C:\Program Files\HP\QuickTest
Professional\samples\flight\app\flight32.mdb;
¢ser Id=admin;Password=;"
Set oConnection = CreateObject("ADODB.Connection")
oConnection.Open ConnectionString
Set getConnection = oConnection
If getConnection.Errors.Count = 0 then
msgbox "Database Connected"
Else
msgbox Err.Description
End If
oConnection.close
Dim oConnection
ConnectionString = "QT_Flight32µ
Set oConnection = CreateObject("ADODB.Connection")
oConnection.Open ConnectionString
Set getConnection = oConnection
If getConnection.Errors.Count = 0 then
msgbox "Database Connected"
Else
msgbox Err.Description
End If
oConnection.close
V you can use ADODB.Recordset object to get the
records of a table.

V Set MyRecordSet = CreateObject("ADODB.Recordset")


Continued«

The most commonly used property and functions are:

V =i ² to identify no records returned. You run a query


and need to determine the pointer is at last record or
not or no records has been returned.
V i ² To retrieve the RecordSet we use open method
which requires two arguments ² connection object and
command object
Syntax for Open would be
<RecordSetName>.Open source, connection, cursor,
lock, type
V Source ² is actually the command object and it could
be a sql statement
V Connection ² is the connection object connecting the
database
V Cursor ² optional parameter to define recordset cursor
type (default ² Forward only)
V Lock ² optional parameter to set the lock type
property(default ² Read Only)
V Type ² optional parameter to define the command type
(default unknown(8))
Generic function for retrieving the
recordset
Function getRecordset(strSQL)
Dim oConnection, oRecordSet
Set oConnection =
CreateObject("ADODB.Connection")
Set oRecordSet = CreateObject("ADODB.Recordset")
oConnection = getConnection()
oRecordSet.Open strSQL,oConnection, adOpenStatic
Set getRecordset = oRecordSet
End Function
Set MyRecordset = getRecordset("Select * from Orders where
Order_Number = 1")
If MyRecordset.EOF <> True Then
msgbox MyRecordset.Fields("Customer_Name").Value
End If

Result
We can use method O  in a loop to
traverse from first record to last record in
the recordset.
Set MyRecordset = getRecordset("Select * from Orders")
Do while MyRecordset.EOF <> True
Print MyRecordset.Fields("Customer_Name").Value
MyRecordset.MoveNext
Loop
Result
Generic function to get the number of records
in recordset

Function getRecordCount(ByRef RecordSet)


Dim Rows
Rows = 0
RecordSet.MoveFirst
Do ¢ntil RecordSet.EOF
Rows = Rows+1
RecordSet.MoveNext
Loop
getRecordCount = Rows
End Function
Generic function to execute a SQL Query
Function ExecuteQuery(strSQL)
On Error Resume Next
Set oConnection = CreateObject("ADODB.Connection")
oConnection = getConnection()
oConnection.Execute strSQL
If Err.Number <> 0 then
ExecuteQuery = False
Exit Function
End If
ExecuteQuery = True
End Function
Set MyRecordset = getRecordset("Select * from Orders")
nColumns = MyRecordset.Fields.Count
For n = 0 to nColumns - 1
Print MyRecordset.Fields(n).Name
Next
Result
Set MyRecordset = getRecordset("Select * from Orders")
nColumns = MyRecordset.Fields.Count
Datatable.AddSheet ("DBImport")
For n = 0 to nColumns ² 1
ParamName = MyRecordset.Fields(n).Name
Datatable.GetSheet("DBImport").AddParameter ParamName,""
nRow = 1
MyRecordset.MoveFirst
Do while MyRecordset.EOF <> True
Datatable.SetCurrentRow(nRow)
Datatable(ParamName,"DBImport") =
MyRecordset.Fields(ParamName)
nRow = nRow + 1
MyRecordset.MoveNext
Loop
Next
Result
DEMO
Function getRecordCount(ByRef RecordSet)
Dim Rows
Rows = 0
RecordSet.MoveFirst
Do ¢ntil RecordSet.EOF
Rows = Rows+1
RecordSet.MoveNext
Loop
getRecordCount = Rows
End Function
Dim order
Dialog("Login").Activate
Dialog("Login").WinEdit("Agent Name:").Set "sudhir"
Dialog("Login").WinEdit("Agent Name:").Type micTab
Dialog("Login").WinEdit("Password:").SetSecure "4d79084be825f492d8473036cdcc396697091be4"
Dialog("Login").WinEdit("Password:").Type micTab
Dialog("Login").WinButton("OK").Click
Window("Flight Reservation").ActiveX("MaskEdBox").Type "111111"
Window("Flight Reservation").ActiveX("MaskEdBox").Type micTab
Window("Flight Reservation").WinComboBox("Fly From:").Select DataTable("fromcity", dtGlobalSheet)
Window("Flight Reservation").WinComboBox("Fly From:").Type micTab
Window("Flight Reservation").WinComboBox("Fly To:").Select DataTable("tocity", dtGlobalSheet)
Window("Flight Reservation").WinButton("FLIGHT").Click
Window("Flight Reservation").Dialog("Flights Table").WinButton("OK").Click
Window("Flight Reservation").WinEdit("Name:").Set DataTable("customer", dtGlobalSheet)
Window("Flight Reservation").WinButton("Insert Order").Click
Window("Flight Reservation").Activate
Window("Flight Reservation").WinEdit("Order No:").Output CheckPoint("Order No:")
order = datatable.Value ("Order_No_text_out")
msgbox "Order No Generated:" & order
Window("Flight Reservation").WinMenu("Menu").Select "File;Exit"
Dim oConnection,ConnectionString, oRecordSet
ConnectionString = "QT_Flight32"
Set oConnection = CreateObject("ADODB.Connection")
oConnection.Open ConnectionString
Set oRecordSet = CreateObject("ADODB.Recordset")
oRecordSet.Open "Select * from Orders",oConnection, adOpenStatic
nColumns = oRecordset.Fields.Count
For n = 0 to nColumns - 1
Print oRecordset.Fields(n).Name
Next
Set oRecordset = nothing
' msgbox "Select * from Orders where Order_Number = " & order & " "
Dim MyRecordSet
Set MyRecordSet = CreateObject("ADODB.Recordset")
MyRecordset.open("Select * from Orders where Order_Number = " & order &"") , oConnection,
adOpenStatic
If MyRecordset.RecordCount <> 0 Then
msgbox MyRecordset.Fields("Customer_Name").Value
else
Reporter.ReportEvent micFail, "Insert Order", "Insert Order is Failed"
End If
Set MyRecordset = nothing
Set MyRecordSet = CreateObject("ADODB.Recordset")
MyRecordset.open("Select * from Orders") , oConnection, adOpenStatic
Do while MyRecordset.EOF <> True
print MyRecordset.Fields("Order_Number").Value & " " &
MyRecordset.Fields("Customer_Name").Value
MyRecordset.MoveNext
Loop
msgbox getRecordCount(MyRecordSet)
Dialog("Login").Activate
Dialog("Login").WinEdit("Agent Name:").Set "sudhir"
Dialog("Login").WinEdit("Agent Name:").Type micTab
Dialog("Login").WinEdit("Password:").SetSecure
"4d793033ff8eccc4936b8de697257b1184cba62e"
Dialog("Login").WinEdit("Password:").Type micTab
Dialog("Login").WinButton("OK").Type micReturn
Window("Flight Reservation").Activate
Window("Flight Reservation").WinMenu("Menu").Select "File;Open Order..."
Window("Flight Reservation").Dialog("Open Order").WinCheckBox("Order No.").Set "ON"
Window("Flight Reservation").Dialog("Open Order").WinEdit("Edit").Set "34"
Window("Flight Reservation").Dialog("Open Order").WinButton("OK").Click
Window("Flight Reservation").Activate
Window("Flight Reservation").WinEdit("Name:").Set "srinu"
Window("Flight Reservation").WinButton("¢pdate Order").Click
Window("Flight Reservation").Activate
DbTable("DbTable").Check CheckPoint("DbTable")
Window("Flight Reservation").WinMenu("Menu").Select "File;Exit"
¶Action1 -------------------------------------------

Dialog("Login").Activate
Dialog("Login").WinEdit("Agent Name:").Set "sudhir"
Dialog("Login").WinEdit("Agent Name:").Type micTab
Dialog("Login").WinEdit("Password:").SetSecure
"4d7985c622216d0f995cd7fb3d1c23b625ec2a52"
Dialog("Login").WinEdit("Password:").Type micTab
Dialog("Login").WinButton("OK").Type micReturn
¶Action2 --------------------------------------------
¶ Action2 > Right Click > Select Action Call properties > Click on Run TAB >
Select Run on all rows
Window("Flight Reservation").Activate
Window("Flight Reservation").WinMenu("Menu").Select "File;Open Order..."
Window("Flight Reservation").Dialog("Open Order").WinCheckBox("Order
No.").Set "ON"
Window("Flight Reservation").Dialog("Open Order").WinEdit("Edit").Set
DataTable("order", dtLocalSheet)
Window("Flight Reservation").Dialog("Open Order").WinButton("OK").Click
Window("Flight Reservation").Activate
Wait 10
Window("Flight Reservation").WinEdit("Name:").Set DataTable("customer",
dtLocalSheet)
Window("Flight Reservation").WinButton("¢pdate Order").Click
wait 10
Window("Flight Reservation").Activate
¶ Action3 ----------------------------------------------------------------------

Window("Flight Reservation").Activate

Dim oConnection,ConnectionString, oRecordSet, order,flag


ConnectionString = "QT_Flight32"
Set oConnection = CreateObject("ADODB.Connection")
oConnection.Open ConnectionString
Set oRecordSet = CreateObject("ADODB.Recordset")

Numrows=Datatable.getsheet("Action2").Getrowcount
msgbox Numrows

flag = 0
For i=1 to Numrows
Datatable.getsheet("Action2").SetCurrentRow(i)
msgbox Datatable.value("order","Action2")
msgbox Datatable.value("customer","Action2")
order = datatable.Value ("order","Action2")
oRecordset.Open ("Select * from Orders where Order_Number = " & order &"") , oConnection, adOpenStatic
msgbox oRecordset.Fields("Customer_Name").Value
If Datatable.value("customer","Action2") = oRecordset.Fields("Customer_Name").Value then
flag = 1
End If
oRecordset.Close
Next
If flag = 1 Then
msgbox "Pass"
Reporter.ReportEvent micPass, "¢pdate Order", "¢pdate Order is
Successful"
else
msgbox "Fail"
Reporter.ReportEvent micFail, "¢pdate Order", "¢pdate Order is
not Successful"
End If

Window("Flight Reservation").WinMenu("Menu").Select "File;Exit"


THANK YO¢

Das könnte Ihnen auch gefallen