QTP Scripts 3

‘Working with XML Files
Option explicit
Dim xmlo, rooto, childso, noc, i, x, y
Set xmlo=XMLUtil.CreateXMLFromFile("C:\mindq.xml")
Set rooto=xmlo.GetRootElement
Set childso=rooto.ChildElements
noc=childso.Count
For i=1 to noc step 1
 x=childso.Item(i).ChildElements.Item(1).Value
 y=childso.Item(i).ChildElements.Item(1).Attributes.Item(1).Value
 print x&" was released by "&y
Next
Set childso=nothing
Set rooto=nothing
Set xmlo=nothing

‘Verify the database data updating
Option explicit
Dim cono, rso, onum, expected, actual
'Read testdata
onum=inputbox("Enter order number")
expected=inputbox("Enter customer name")
expected=cstr(expected)
'Connect to DB
Set cono=createobject("ADODB.Connection")
cono.Open("DSN=QT_Flight32")
'Update DB
cono.Execute "Update orders set Customer_Name='"&expected&"' where order_number="&onum
'select data from DB
Set rso=createobject("ADODB.Recordset")
rso.Open "select Customer_Name from orders where order_number="&onum,cono
actual=rso.Fields("Customer_Name").Value
If cstr(expected)=cstr(actual) Then
            reporter.ReportEvent micPass,"DB testing","Correctly updated"
else
            reporter.ReportEvent micFail,"DB testing","Incorrectly updated"
End If
rso.Close
cono.Close
Set rso=nothing
Set cono=nothing

‘Using conditions on the database
Option explicit
Dim cono, rso, x
Set cono=createobject("ADODB.Connection")
cono.Open("DSN=QT_Flight32")
Set rso=createobject("ADODB.Recordset")
rso.Open "Select Order_number from orders where tickets_ordered>5",cono
While rso.EOF<>true
 x=rso.Fields("Order_Number").Value
 cono.Execute "Update orders set tickets_ordered=5 where order_number="&x
 rso.MoveNext
Wend
rso.Close
cono.Close

‘Using conditions on the database
Option explicit
Dim cono, rso, x
'connect to database
Set cono=createobject("adodb.connection")
cono.Open ("dsn=qt_flight32")
'select all customer names
Set rso=createobject("adodb.recordset")
rso.Open "select customer_name from orders",cono
'Disply customer names which starts with capital
While rso.EOF<>true
            x=rso.Fields("customer_name").Value
            If left(x,1)>="A" and left(x,1)<="Z" Then
                        print x
            End If
            rso.MoveNext
Wend
rso.Close
cono.Close
 
‘Deleting the data on the database using conditions
Option explicit
Dim cono, rso, x, y, flag
'Connect to DB
x=inputbox("Enter order number")
Set cono=createobject("ADODB.Connection")
cono.Open("DSN=QT_Flight32")
'Delete specified record
cono.Execute "Delete from orders where Order_Number="&x
'Select all Orders
Set rso=createobject("ADODB.Recordset")
rso.Open "Select Order_number from orders",cono
flag=0
While rso.EOF<>true
            y=rso.Fields("Order_Number").Value
            If x=y Then
                        flag=1
            End If
            rso.MoveNext
Wend
If flag=0 Then
            Reporter.ReportEvent micPass,"DB test","Deleted"
else
            Reporter.ReportEvent micFail,"DB test","Not Deleted"
End If
rso.Close
cono.close

‘Display the data in database
Option explicit
Dim cono, rso, noc, i, cn
Set cono=createobject("ADODB.Connection")
cono.Open("DSN=QT_Flight32")
Set rso=createobject("ADODB.Recordset")
rso.Open "select * from orders",cono
noc=rso.Fields.Count
For i=0 to noc-1 step 1
            cn=rso.Fields(i).Name
            print cn
Next
rso.Close
cono.Close
Set rso=nothing
Set cono=nothing


‘Using the web table
option explicit
Dim x, i, rc, avg, sum
With Browser("title:=Web Tours").page("title:=Web Tours")
            rc=.webtable("name:=outboundFlight").rowcount
            sum=0
    For i=2 to rc step 1
                        x=.webtable("name:=outboundFlight").getcelldata(i,3)
                        x=mid(x,2,len(x)-1) 'to remove $
                        x=ccur(x)
                        sum=ccur(sum)+x
            next
            avg=sum/(rc-1)
            print avg
end with

‘Using the web table find the expected data
Option explicit
Dim rc, x, i, flag
flag=0
With Browser("title:=.*").Page("title:=.*")
            rc=.webtable("name:=toggleAll").rowcount
            For i=2 to rc step 1
                        x=.webtable("name:=toggleAll").GetCellData(i,7)
                        If instr(x,"My Name is khan") Then
                                    flag=1
                        End If
            Next
End With
If flag=1 Then
            reporter.ReportEvent micPass,"Mailbox test","Mail was found"
else
            reporter.ReportEvent micFail,"Mailbox test","Mail was not found"
End If

‘Cookie test for Google
Option explicit
Dim bro, url, x
'Delete existing cookies
webutil.DeleteCookies
'Launch site
bro="C:\Program Files\Internet Explorer\iexplore.exe"
url="http:\\www.google.co.in"
invokeapplication bro&" "&url
'verify cookies
x=Browser("title:=Google").page("title:=Google").Object.cookie
If x<>"" Then
            reporter.ReportEvent micPass,"Cookie test for Google","created"
else
            reporter.ReportEvent micFail,"Cookie test for Google","Not created"
End If

‘Count the number of mails having attachments in Web table
Option explicit
Dim rc, x, i, amc
amc=0
With Browser("title:=.*").Page("title:=.*")
            rc=.webtable("name:=toggleAll").rowcount
            For i=2 to rc step 1
                        x=.webtable("name:=toggleAll").GetCellData(i,6)
                        If instr(x,"Attachments") Then
                                    amc=amc+1
        End If
            Next
End with
print "No: of mails have attachments is "&amc

         

Click to continue QTP Scripts 4

No comments:

Post a Comment