database testing in qtp modified

74
Database Testing in QTP

Upload: sambasivarao-miriyala

Post on 25-Nov-2014

119 views

Category:

Documents


1 download

TRANSCRIPT

Today

almost all the software applications use relational database management systems (RDBMS) to provide persistency to the program. could be Oracle, SQL, Access and MySQL

It

Database RDBMS

Concepts

used for the present application

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

for querying the database

Client 1 WEB SERVER APPLICATION SERVER

Database

Client 2

SERVER

Inserting Through

Database Checkpoints

scripting to connect to the database and test the records

to test the contents of the database accessed by application under test. It stores the expected data and compares this with the data stored in the database. 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. QTP now compares the current data with the expected data stored in the checkpoint and gives you the result as pass or fail. Used

Take a simple example for Flight Application. Suppose you are updating an order by changing the name. 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

Go to Insert > Checkpoint > Database Checkpoint. You will see a database query wizard. 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. Click Create button, which will open the data source window, Select Machine Data Source and click new.

Continued

Create New Data Source window opens. Select the type of data source and click Next 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 You will see the data source name just created in the list at Machine Data source. Select and Click OK Specify your sql query e.g. for above mentioned example Select Customer_Name from Orders. Click Finish It will Open the Database Checkpoint Properties, modify your checkpoint settings, enter the expected data and Click OK

It

will add a line in the expert view as: DbTable("DbTable").Check CheckPoint("DbTable") 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.

When

If you dont 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

Create the object using the CreateObject method Set MyConnection = CreateObject(ADODB.Connection) Set the connecting string for database connection

ContinuedA typical connecting string for database connection will be like Access Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\mydatabase.mdb;User Id=admin;Password=; SQL Server Provider=sqloledb;Data Source=myServerAddress; Initial Catalog=myDataBase; User Id=myUsername;Password=myPassword; Oracle Provider=msdaora;Data Source=MyOracleDB; User Id=myUsername;Password=myPassword;

Continued Once you have the connection string you can open the

connection using Open method of Connection object MyConnection.Open

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; User 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

you can use ADODB.Recordset object to get the records of a table. Set MyRecordSet = CreateObject("ADODB.Recordset")

ContinuedThe most commonly used property and functions are:

EOF 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. Open To retrieve the RecordSet we use open method which requires two arguments connection object and command object

Syntax for Open would be .Open source, connection, cursor, lock, type

Source is actually the command object and it could be a sql statement Connection is the connection object connecting the database Cursor optional parameter to define recordset cursor type (default Forward only) Lock optional parameter to set the lock type property(default Read Only) Type optional parameter to define the command type (default unknown(8))

Generic function for retrieving the recordsetFunction 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 MoveNext 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 recordsetFunction getRecordCount(ByRef RecordSet) Dim Rows Rows = 0 RecordSet.MoveFirst Do Until RecordSet.EOF Rows = Rows+1 RecordSet.MoveNext Loop getRecordCount = Rows End Function

Generic function to execute a SQL QueryFunction 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 Until 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("Update 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("Update 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, "Update Order", "Update Order is Successful" else msgbox "Fail" Reporter.ReportEvent micFail, "Update Order", "Update Order is not Successful" End If Window("Flight Reservation").WinMenu("Menu").Select "File;Exit"

THANK YOU