12
QTP/UFT Data Driven Tests

QTP/UFT Data Driven Tests

Embed Size (px)

DESCRIPTION

QTP/UFT Data Driven Tests

Citation preview

Page 1: QTP/UFT Data Driven Tests

QTP/UFT Data Driven Tests

1) Example Dynamic Data Submission (Using Loop)

Page 2: QTP/UFT Data Driven Tests

'Verify Total, In Flight Reservation (In Flight Reservation, Total = Tickets * Price)

'Verify Total, In Flight Reservation (In Flight Reservation, Total = Tickets * Price)

Option ExplicitDim t,p,tot,n,i

For i= 1 to 5 step 1

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 i Window("Flight Reservation").Dialog("Open Order").WinButton("OK").Click  t=Window("Flight Reservation").WinEdit("Tickets:").GetROProperty("text") p=Window("Flight Reservation").WinEdit("Price:").GetROProperty("text") tot=Window("Flight Reservation").WinEdit("Total:").GetROProperty("text")

t=Cdbl(t)

p=Cdbl(mid(p,2,len (p-1)))tot=Cdbl(mid(tot,2,len (tot-1)))

If tot=t*p Then Reporter.ReportEvent 0,"Res","Calculation Passed" else Reporter.ReportEvent 1,"Res","Calculation Failed" End IfNext

2) Example (Verify Flight From & Flight To Combo Boxes)

'Verify Flight From & Flight To Combo Boxes (In Flight reservation, select an item from Fly From: combo box and verify weather that item available or not in Fly To: combo box, like this select all items one by one in Fly From and verify weather selected items available or not in Fly To.)

Option explicitDim qtp,flight_app,f,t,i,j,x,y

If Not Window("text:=Flight Reservation").Exist (7)= True ThenUFT=Environment("ProductDir")   ' return C:\Program Files (x86)\HP\Unified Functional Testing\Flight_app="\samples\flight\app\flight4a.exe"

Page 3: QTP/UFT Data Driven Tests

SystemUtil.Run UFT & Flight_app

Dialog("text:=Login").ActivateDialog("text:=Login").WinEdit("attached text:=Agent Name:").Set "mercury"Dialog("text:=Login").WinEdit("attached text:=Password:").SetSecure "4aa5ed3daf680e7a759bee1c541939d3a54a5b65"Dialog("text:=Login").WinButton("text:=OK").ClickEnd If

Window("text:=Flight Reservation").Activate Window("text:=Flight Reservation").WinButton("window id:=6").Click Window("text:=Flight Reservation").ActiveX("acx_name:=MaskEdBox","window id:=0").Type "091613"  f=Window("text:=Flight Reservation").WinComboBox("attached text:=Fly From:").GetItemsCount  For i= 0 to f-1 step 1 Window("text:=Flight Reservation").WinComboBox("attached text:=Fly From:").Select (i) x=Window("text:=Flight Reservation").WinComboBox("attached text:=Fly From:").GetROProperty ("text") t=Window("text:=Flight Reservation").WinComboBox("attached text:=Fly To:").GetItemsCount For j= 0 to t-1 step 1 Window("text:=Flight Reservation").WinComboBox("attached text:=Fly To:").Select (j) y=Window("text:=Flight Reservation").WinComboBox("attached text:=Fly To:").GetROProperty ("text") If x <> y Then Reporter.ReportEvent 0,"Res","Test Passed" Else Reporter.ReportEvent 1,"Res","Test Failed" End If Next Next

3) Basic NOTE PAD Example

1. Open Text File

2. Writing data into Text File

3. Reading data from Text File

Page 4: QTP/UFT Data Driven Tests

Set FSO = CreateObject("Scripting.FileSystemObject")

Set File= FSO.OpenTextFile("E:\Training\QTP\TestData\sample.txt",8)

File.WriteLine("This is sample text file")

File.Close

SET Fike = NOTHING

SET FSO = NOTHING

Set FSO = CreateObject("Scripting.FileSystemObject")

Set File = FSO.OpenTextFile("E:\Training\QTP\TestData\sample.txt", 1)

Do Until File.AtEndOfStream

data = File.Readline

msgbox data

Loop

4) Data Driven Test Using Notepad

'**************************************************'Test Requirement: Data Driven Testing by Fetching Test data directly from a Text file.'Author: Pavan'Pre-requisites:'Logindata.txt (Test Data)

'Test Flow:'Creating an Automation Object in FileSystem class'Opening the External Test Data file using the Object'Read the Data & Split the Data'Generating the Login Operation'Pass Parameters'*************************************************

Dim objFso, myFile, myLine, myFieldSet objFso=CreateObject("Scripting.FileSystemObject")

Page 5: QTP/UFT Data Driven Tests

Set myFile=objFso.OpenTextFile("E:\Training\QTP\TestData\Logindata.txt",1) '1 for Read, 2-Write & 8-Append

Do Until myFile.AtEndOfStreammyLine=myFile.ReadLinemyField=Split(myLine,",")

    SystemUtil.Run "C:\Program Files (x86)\HP\Unified Functional Testing\samples\flight\app\flight4a.exe"    Dialog("text:=Login").Activate    Dialog("text:=Login").WinEdit("attached text:=Agent Name:").Set myField(0)    Dialog("text:=Login").WinEdit("attached text:=Password:").Set myField(1)    wait(2)    Dialog("text:=Login").WinButton("text:=OK").Click    wait(2)

If Dialog("text:=Login").Dialog("text:=Flight Reservations").Exist(5) then    Dialog("text:=Login").Dialog("text:=Flight Reservations").winbutton("text:=OK").Click    Reporter.ReportEvent micFail, "User Login", "Failed"

ElseIf Window("text:=Flight Reservation").exist(3) Then    Window("text:=Flight Reservation").Close    Reporter.ReportEvent micPass, "User Login", "Passed"

End If

Loop

myFile.CloseSet objFso=Nothing

5) Open 1 to 10 orders in Flight Reservation, Capture Customer names and Export into a Text file

Page 6: QTP/UFT Data Driven Tests

'Test Requirement: Open 1 to 10 orders in Flight Reservation , Capture Customer names and Export into a Text file

'Test Flow:'Login Operation'Open Order Operation and form the Loop to open 1 to 10 Orders'Capture Cusomer names using GetROProperty Method'Create File system Object and Open the Text file using  the Object and Export Cusomer names'----------------------------------------------------------------------------Option ExplicitDim Order_Number, Customer_Name, objFso, myFileSet objFso=CreateObject("Scripting.FileSystemObject")Set myFile= objFso.CreateTextFile ("E:\Training\QTP\TestData\CustomerNames.txt",2)myFile.WriteLine "Cusomer Names"myFile.WriteLine "--------------------"

If Not Window("Flight Reservation").Exist(3)  ThenSystemUtil.Run "C:\Program Files (x86)\HP\Unified Functional Testing\samples\flight\app\flight4a.exe"Dialog("text:=Login").ActivateDialog("text:=Login").WinEdit("attached text:=Agent Name:").Set "mercury"Dialog("text:=Login").WinEdit("attached text:=Password:").Set "mercury"Dialog("text:=Login").WinButton("text:=OK").ClickEnd If

For Order_Number= 1 to 5 step 1

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 Order_Number

Window("Flight Reservation").Dialog("Open Order").WinButton("OK").ClickCustomer_Name = Window("Flight Reservation").WinEdit("Name:").GetROProperty("text")wait (2)myFile.WriteLine Customer_Name

NextmyFile.CloseSet objFso=Nothing

6. Basic EXCEL SHEET Example

1. Open Excel File

2. Writing data into Excel File

Page 7: QTP/UFT Data Driven Tests

3. Reading data from Excel File

Set XLFile=CreateObject("Excel.Application")

Set wb=XLFile.workbooks.open("E:\Training\QTP\TestData\sample.xlsx")

Set ws=wb.worksheets(1)

Writevalue=" This is the example for Excel AOM"

ws.cells(1,1).value=Writevalue

wb.save

Readvalue=ws.cells(1,1)

msgbox Readvalue

wb.close

Set XLFile=NOTHING

Set wb=NOTHING

Set ws=NOTHING

7) Data Driven Testing through an External Excel Sheet

'Data Driven Testing through an External Excel Sheet

Set myExcel=Createobject("Excel.Application")Set myFile=myExcel.workbooks.open ("E:\Training\QTP\TestData\Login.xls")Set mySheet=myFile.worksheets("Sheet1")Rows_Count=mySheet.usedrange.rows.countmsgbox Rows_CountFor i= 1 to Rows_CountAgent=mySheet.cells(i,"A")pwd=mySheet.Cells(i,"B")    SystemUtil.Run "C:\Program Files (x86)\HP\Unified Functional Testing\samples\flight\app\flight4a.exe"    Dialog("text:=Login").Activate    Dialog("text:=Login").WinEdit("attached text:=Agent Name:").Set Agent    Dialog("text:=Login").WinEdit("attached text:=Password:").Set pwd

Page 8: QTP/UFT Data Driven Tests

    wait(2)    Dialog("text:=Login").WinButton("text:=OK").Click    wait(2)

If Dialog("text:=Login").Dialog("text:=Flight Reservations").Exist(5) then    Dialog("text:=Login").Dialog("text:=Flight Reservations").winbutton("text:=OK").Click    Reporter.ReportEvent micFail, "User Login", "Failed"    mySheet.Cells(i,"C").value="Failed"    ElseIf Window("text:=Flight Reservation").exist(3) Then    Window("text:=Flight Reservation").Close    Reporter.ReportEvent micPass, "User Login", "Passed"    mySheet.cells(i,"C").value="Passed"    End IfmyFile.saveNext

8) Data Driven Testing using Data Table methods

'Data Driven Testing using Data Table methods

'SYNTAX: Data table. Import sheet “path of the excel file, source sheet id, destination sheet id”Datatable.ImportSheet "E:\Training\QTP\TestData\Login1.xls",1,1

n=datatable.GetSheet (1).GetRowCountmsgbox nFor i= 1 to nDatatable.SetCurrentRow(i)

    SystemUtil.Run "C:\Program Files (x86)\HP\Unified Functional Testing\samples\flight\app\flight4a.exe"    Dialog("text:=Login").Activate    Dialog("text:=Login").WinEdit("attached text:=Agent Name:").Set datatable("Agent",1)    Dialog("text:=Login").WinEdit("attached text:=Password:").Set datatable("Password",1)

Page 9: QTP/UFT Data Driven Tests

    wait(2)    Dialog("text:=Login").WinButton("text:=OK").Click    wait(2)

If Dialog("text:=Login").Dialog("text:=Flight Reservations").Exist(3) then    Dialog("text:=Login").Dialog("text:=Flight Reservations").winbutton("text:=OK").Click    datatable("Res",1)="Failed"        ElseIf Window("text:=Flight Reservation").exist(3) Then    Window("text:=Flight Reservation").Close      datatable("Res",1)="Passed"End ifNext

datatable.Export "E:\Training\QTP\TestData\Login1.xls"

Database Table creation in

Oracle/SQL Server

create table Flight(AgentName varchar(10),Passwrd varchar(8));

Insert into Flight values('MERCURY','MERCURY');Insert into Flight values('MERCURY','MERCURY');Insert into Flight values('WAR','Mer');Insert into Flight values('JO','Mercury');Insert into Flight values('BLAKE','Mer');

Page 10: QTP/UFT Data Driven Tests

COMMIT;select * from flight;

9) Reading data from Oracle database

Option ExplicitDim con,rs

Set con=createobject("adodb.connection")Set rs=createobject("adodb.recordset")

con.open "Driver={Microsoft ODBC for Oracle};Server=INVRLX61ILM40; Uid=ilmadmin;Pwd=ilmadmin;"rs.open "select * from flight;",con

Do while not rs.eof

    SystemUtil.Run "C:\Program Files (x86)\HP\Unified Functional Testing\samples\flight\app\flight4a.exe"    Dialog("text:=Login").Activate    Dialog("text:=Login").WinEdit("attached text:=Agent Name:").Set rs.Fields("AgentName")    Dialog("text:=Login").WinEdit("attached text:=Password:").Set rs.Fields("Passwrd")    wait(2)    Dialog("text:=Login").WinButton("text:=OK").Click    wait(2)

If Dialog("text:=Login").Dialog("text:=Flight Reservations").Exist(5) then    Dialog("text:=Login").Dialog("text:=Flight Reservations").winbutton("text:=OK").Click    Reporter.ReportEvent micFail, "User Login", "Failed"        ElseIf Window("text:=Flight Reservation").exist(3) Then    Window("text:=Flight Reservation").Close    Reporter.ReportEvent micPass, "User Login", "Passed"        End If

rs.movenextLoop

'Release objects

Page 11: QTP/UFT Data Driven Tests

Set rs= nothingSet con= nothing