Showing posts with label Database. Show all posts
Showing posts with label Database. Show all posts

Data Driven Testing using Notepad in QTP

Set
f=createobject("scripting.filesystemobject")
Set f1=f.createtextfile("d://file1.txt")
f1.writeline"aaa bbb"
f1.writeline"ccc ddd"
f1.writeline"eee fff"
f1.writeline"ggg hhh"
f1.writeline"iii jjj"

The above script creates a notepad in "d" drive with name file1

aaa bbb
ccc ddd
eee fff
ggg hhh
iii jjj

values are stored in file1.txt.

Set f2=f.opentextfile("d://file1.txt")

While f2.atendofstream<>true
f3=f2.readline
x=split(f3," ")
msgbox x(0)
msgbox x(1)

Wend

The above script is used for data driven using notepad directly.here we are not importing data to excel sheet.

directly values are retreived from notepad.

we are using while loop and reading each line till the end.

split function splits the line where space(" ")occurs.

line is divided to 2 parts.one before space and other after space

for example we have 1st line in notepad as aaa bbb

here aaa is 1st part and bbb is 2nd part

x(0)=aaa

x(1)=bbb

all values are read this way using while loop.
One point to note here is if any line is empty in notepad datadriven testing is stopped before that line.

It will not proceed further.so we have to give values without any empty lines in notepad.

To make u more clear,
suppose u have

aaa bbb

ccc ddd

Datadriven is stopped at aaa and bbb only because next line is empty.datadriven is stopped after 1st line.

Connecting to a database in QTP

Use the below code for connecting a database from QTP.Const adOpenStatic = 3
Const adLockOptimistic = 3
Const adUseClient = 3
Set objConnection = CreateObject("ADODB.Connection")
Set objRecordset = CreateObject("ADODB.Recordset")
objConnection.Open "DRIVER={Microsoft ODBC for Oracle};UID=;PWD="
objRecordset.CursorLocation = adUseClient
objRecordset.CursorType = adopenstatic
objRecordset.LockType = adlockoptimistic
ObjRecordset.Source="select field1,field2 from testTable"
ObjRecordset.ActiveConnection=ObjConnection
ObjRecordset.Open 'This will execute your Query
If ObjRecordset.recordcount>0 then
Field1 = ObjRecordset("Field1").Value
Field2 = ObjRecordset("Field2").Value
End if