QTP Scripts 2

‘Adding work book to excel
Option explicit
Dim exo, wbo, wso, i
Set exo=createobject("Excel.Application")
exo.Visible=true
Set wbo=exo.Workbooks.Add
Set wso=wbo.Worksheets("Sheet1")
For i=1 to 56 step 1
            wso.cells(i,1)="mindq"
            wso.cells(i,1).font.colorindex=i
Next
wbo.SaveAs("C:\colors.xls")
exo.Quit
Set wso=nothing
Set wbo=nothing
Set exo=nothing

‘Image testing
Option explicit
Dim b1, b2, f1, f2, comp
b1="C:\Program Files\HP\QuickTest Professional\samples\flight\app\flight4a.exe"
b2="C:\Program Files\HP\QuickTest Professional\samples\flight\app\flight4b.exe"
f1="C:\img1.png"
f2="C:\img2.png"
invokeapplication b1
Dialog("text:=Login").Static("micclass:=Static").CaptureBitmap f1
Dialog("text:=Login").Close
invokeapplication b2
Dialog("text:=Login").Static("micclass:=Static").CaptureBitmap f2
Dialog("text:=Login").Close
Set comp=createobject("Mercury.filecompare")
If comp.IsEqualBin(f1,f2,1,1) Then
            reporter.ReportEvent micPass,"Image testing","Same"
else
            reporter.ReportEvent micFail,"Image testing","Different"
End If

‘Retrieving the data from database.
Option explicit
Dim cono, rso
Set cono=createobject("ADODB.Connection")
cono.Open("dsn=QT_Flight32")
Set rso=createobject("ADODB.Recordset")
rso.Open "select * from orders",cono
While rso.EOF<>true
            print rso.Fields("Order_Number").Value
            rso.MoveNext
Wend
rso.Close
cono.Close

‘Creating the charts
Option explicit
Dim exo, wbo, wso, cho, rngo
Set exo=createobject("excel.application")
exo.Visible=true
Set wbo=exo.Workbooks.Open("C:\runs.xls")
set wso=wbo.Worksheets("Sheet1")
Set rngo=wso.range("A2","B11")
Set cho=wbo.Charts.add
cho.charttype=64
cho.setsourcedata rngo,2
cho.location 2,"Sheet1"
wbo.Save
exo.Quit
Set cho=nothing
Set rngo=nothing
Set wso=nothing
Set wbo=nothing
Set exo=nothing


‘Using the Data table
Option explicit
Dim x, y, z, n, i
n=datatable.GetSheet(2).GetRowCount
For i=1 to n step 1
            datatable.GetSheet(2).SetCurrentRow(i)
            x=datatable.Value("input1",2)
            y=datatable.Value("input2",2)
            z=cint(x)+cint(y)
            datatable.Value("output",2)=z
Next

‘Data tables
Option explicit
Dim x,y,n,i
n=datatable.GetSheet(2).GetRowCount
For i=1 to n step 1
            datatable.GetSheet(2).SetCurrentRow(i)
            x=datatable.value("input1",2)
            y=datatable.Value("input2",2)
    If cint(x)=cint(y) Then
                        datatable.value("output",2)="pass"
            else
                        datatable.Value("output",2)="fail"
            End If
Next

'Data importing to DT from excel
Option explicit
Dim exo, wbo, wso, r, c, i, j, x, y
'create excel objects
Set exo=createobject("Excel.Application")
Set wbo=exo.Workbooks.Open("C:\mindq.xls")
Set wso=wbo.Worksheets("Sheet1")
r=wso.usedrange.rows.count
c=wso.usedrange.columns.count
'Datatable add sheet and create columns
datatable.AddSheet("Mindq")
For i=1 to c step 1
            x=wso.cells(1,i)
            datatable.GetSheet(3).AddParameter x,""
Next
'Data importing from excel to DT
For i=2 to r step 1
            For j=1 to c step 1
                        y=wso.cells(i,j)
                        datatable.GetSheet(3).SetCurrentRow(i-1)
                        datatable.Value(j,3)=y
            Next
Next
exo.Quit
Set wso=nothing
Set wbo=nothing
Set exo=nothing

'Read line by line in notepad and stored into DT
Option explicit
Dim fso, fo, i, x, y, r
'Flat file open
Set fso=createobject("scripting.filesystemobject")
Set fo=fso.OpenTextFile("c:\testresults.txt",1,false)
'Datatable add sheet and createcolumns
Datatable.AddSheet("Mindq")
x=fo.ReadLine
y=split(x," ")
For i=lbound(y) to ubound(y) step 1
            datatable.GetSheet(3).AddParameter y(i),""
Next
'Read line by line and stored into DT
r=1
While fo.AtEndOfStream<>true
            x=fo.ReadLine
            y=split(x," ")
            datatable.GetSheet(3).SetCurrentRow(r)
            For i=0 to ubound(y) step 1
                        datatable.Value(i+1, 3)=y(i)
            Next
            r=r+1
Wend
fo.Close
Set fo=nothing
Set fso=nothing

‘Working with XML Files
Option explicit
Dim xmlo, rooto, childso, noc, i,c
Set xmlo=XMLUtil.CreateXMLFromFile("C:\team.xml")
Set rooto=xmlo.GetRootElement
Set childso=rooto.ChildElements
noc=childso.Count
For i=1 to noc step 1
            c=childso.Item(i).ChildElements.Item(2).Value
            If c="Mumbai" Then
             childso.Item(i).ChildElements.Item(2).SetValue "Mumbai Indians"
            End If
Next
xmlo.SaveFile("C:\team.xml")
Set childso=nothing
Set rooto=nothing
Set xmlo=nothing



‘Using the keyboard

Option explicit
Dim wsho, bro, url, i
'Launch Google
bro="c:\program files\internet explorer\iexplore.exe"
url="http:\\www.google.co.in"
invokeapplication bro&" "&url
'Focus on editbox
with Browser("title:=Google").page("title:=Google")
            .webedit("name:=q").click
End with
'Perform key strokes
Set wsho=createobject("Wscript.shell")
wsho.SendKeys("mindq")
For i=1 to 4 step 1
            wsho.SendKeys("{DOWN}")
Next
wsho.SendKeys("{ENTER}")
'Destroy object
Set wsho=nothing

‘Using the web table
Option explicit
Dim x, i, rc, max, rn
With Browser("title:=Web Tours").page("title:=Web Tours")
            rc=.webtable("name:=outboundFlight").rowcount
            max=.webtable("name:=outboundFlight").getcelldata(2,3)
            rn=2
    max=mid(max,2,len(max)-1) 'to remove $
            max=ccur(max)
            For i=3 to rc step 1
                        x=.webtable("name:=outboundFlight").getcelldata(i,3)
                        x=mid(x,2,len(x)-1)
                        x=ccur(x)
                        If max<x Then
                                    max=x
                                    rn=i
                        End If
            Next
            .webradiogroup("name:=outboundFlight").Select "#"&rn-2
End With

Click to continue QTP Scripts 3



No comments:

Post a Comment