QTP Scripts

Embed Size (px)

Citation preview

'Code for printing the files in a folder Option Explicit Dim fso,fld,sFiles,sFile Set fso=CreateObject("Scripting.FileSystemObject") Set fld=fso.GetFolder("C:\Documents and Settings\Administrator\Desktop\Project") Set sFiles=fld.Files For each sFile in sFiles print sFile.Name Next Set fso=nothing ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 'Code for Action Parameters by using Input,Output Parameters a=10 b=20 Datatable.LocalSheet.AddParameter "Sum","" Datatable.LocalSheet.AddParameter "Diff","" Datatable.LocalSheet.AddParameter "Prod","" RunAction "Action2", oneIteration,a,b,sum1,diff1,prod1 'RunAction "Action2", oneIteration,a,b,Datatable(1,2),Datatable(2,2),Datatable(3,2) wait 3 Msgbox "Sum: "&sum1 Msgbox "Diff: "&diff1 Msgbox "Prod: "&prod1 'Code for creating input,output parameters Option Explicit Dim n1,n2,sum,diff,prod n1=Parameter("Num1") n2=Parameter("Num2") sum=n1+n2 diff=n2-n1 prod=n1*n2 Parameter("s")=sum Parameter("d")=diff Parameter("p")=prod Code for actions SystemUtil.Run "C:\Program Files\Mercury Interactive\QuickTest Professional\samples\flight\app\flight4a.exe","","C:\Program Files\Mercury Interactive\QuickTest Professional\samples\flight\app\","open" Dialog("Login").WinEdit("Agent Name:").Set "jampani007" @@ hightlight id_;_1049510_;_script infofile_;_ZIP::ssf1.xml_;_ Dialog("Login").WinEdit("Agent Name:").Type micTab @@ hightlight id_;_1049510_;_script infofile_;_ZIP::ssf2.xml_;_

Dialog("Login").WinEdit("Password:").SetSecure "4a3f0a7be1e01cd14cdbff178464bf5992d35800" @@ hightlight id_;_393928_;_script infofile_;_ZIP::ssf3.xml_;_ Dialog("Login").WinEdit("Password:").Type micReturn @@ hightlight id_;_393928_;_script infofile_;_ZIP::ssf4.xml_;_ RunAction "Copy of Create_order", oneIteration RunAction "Fax_order", oneIteration RunAction "Copy of Logout", oneIteration 'Code for finding the Broken links by using child objects Option Explicit Dim brlnk,desc,cnt,i,coll,oShell,link SystemUtil.Run "www.yatra.com" Set oShell=CreateObject("wscript.shell") Set desc=Description.Create desc("html tag").Value="A" Set coll=Browser("Yatra.com My Bookings").Page("Yatra.com My Bookings").ChildObjects(desc) cnt=coll.count oShell.popup "No of Links: "&cnt,2,"Links Info" For i=0 to cnt-1 Set coll=Browser("Yatra.com My Bookings").Page("Yatra.com My Bookings").ChildObjects(desc) 'link=coll(i).GetRoProperty("name") oShell.popup coll(i).GetRopRoperty("name"),1,"Link Info" coll(i).click If (coll(i).GetRoProperty("title")="The page cannot be disp") Then brlnk=brlnk+1 End If Browser("Yatra.com My Bookings").Back Next Msgbox "No of Broken Links: "&brlnk Set oShell=nothing 'Code for Built-In Enviroment Variables to read system info Msgbox "Computer Name: "&Environment.Value("LocalHostName") Msgbox "Operating System: "&Environment.Value("OS") Msgbox "OS Version: "&Environment.Value("OSVersion") Msgbox "Action Name: "&Environment.Value("ActionName") Msgbox "Action Iteration: "&Environment.Value("ActionIteration") Msgbox "TestName: "&Environment.Value("TestName") Msgbox "Test Iteration: "&Environment.Value("TestIteration") Msgbox "ProductName: "&Environment.Value("ProductName") Msgbox "Product Version: "&Environment.Value("ProductVer") Msgbox "Product Dir: "&Environment.Value("ProductDir") Msgbox "Test Dir: "&Environment.Value("TestDir")

'Code for capturing the text forom a webpage by using GetCellData() Option Explicit Dim rowcnt,colcnt,rowno,colno,celldata,oShell Set oShell=CreateObject("wscript.shell") rowcnt=Browser("IN Offers E Mail, News,").Page("IN Offers E Mail, News,").WebTable("index:=2").RowCount 'Msgbox "Row count: "&rowcnt oShell.popup "Row count: "&rowcnt,2,"Row Info" For rowno=1 to rowcnt colcnt=Browser("IN Offers E Mail, News,").Page("IN Offers E Mail, News,").WebTable("Index:=2").ColumnCount(rowno) ' Msgbox "Column Count: "&colcnt oShell.popup "Column count: "&colcnt,2,"Col Info" For colno=1 to colcnt celldata=Browser("IN Offers E Mail, News,").Page("IN Offers E Mail, News,").WebTable("Index:=2").GetCellData(rowno,colno) If (celldata"") Then oShell.popup celldata,1,"Cell Info" End If Next Next 'Code for capturing the text from a window by using GetVisibleText() Option Explicit Dim str1,str2,pos,res str1=window("Flight Reservation").Dialog("About Flight Reservation").GetVisibleText() Msgbox str1 str2="Version 4.0" pos=Instr (1,str1,str2,VBTextCompare) Msgbox pos If (pos0) Then res=Mid(str1,pos,11) If (res=str2) Then Msgbox "The string exist: "&res Else Msgbox "The string does not exist: "&pos End If End If 'Code for capturing the text from a webpage by using GetCellData() Option Explicit Dim web,rowcnt,colcnt,rowno,colno,celldata,cnt,coll,oShell,i Set oShell=CreateObject("wscript.shell") Set web=Description.Create 'web("micclass").value="WebTable" web.add "html tag","TABLE"

Set coll=Browser("India Hotels| Star Hotels|").Page("India Hotels| Star Hotels|").ChildObjects(web) cnt=coll.count oShell.popup "Web Tables Count: "&cnt,2,"Web Table Info" For i=0 to cnt rowcnt=coll(i).Rowcount 'oShell.popup "Row Count: "&rowcnt,2,"Row Info" For rowno=1 to rowcnt colcnt=coll(i).columncount(rowno) ' oShell.popup "Column Count: "&colcnt,2,"Col Info" For colno=1 to colcnt celldata=coll(i).GetCellData(rowno,colno) If (celldata"") Then 'oShell.popup celldata,1,"Cell Info" print celldata End If Next Next Next 'Code for checking the DNS services by using oShell Object Option Explicit Dim fso,myfile,str,pos,oShell,res Const ForReading=1 Set oShell=CreateObject("wscript.shell") oShell.run "CMD /c net start > D:\myfile123.txt" Set fso=CreateObject("Scripting.FilesystemObject") Set myfile=fso.OpenTextfile("D:\myfile123.txt",ForReading,False) While myfile.AtEndOfStreamTrue str=myfile.Readall pos=Instr (1,str,"DNS Client",VBTextCompare) If (pos0) Then res=Mid(str,pos,10) msgbox res If (res="DNS Client") Then Msgbox "DNS Services Running "&res Else Msgbox "DNS Services not Running" End If End If Wend set oShell=nothing browser("Google Accounts").Page("Google Accounts").Image("Visual verification").CaptureBitmap "D:\img.bmp" @@ hightlight id_;_3146758_;_script infofile_;_ZIP::ssf1.xml_;_ 'Code for finding the Hyper Links in a webpage by using Child Objects

Option Explicit Dim oLink,coll,cnt,i,link,oShell Datatable.GlobalSheet.AddParameter "MyLinks","" set oShell=CreateObject("wscript.shell") Set oLink=Description.Create 'oLink("micclass").value="Link" oLink("html tag").value="A" Set coll=Browser("CreationTime:=0").Page("index:=0").ChildObjects(oLink) cnt=coll.Count 'Msgbox "No of Hyper Links: "&cnt oShell.popup "No of Hyper Links: "&cnt,2,"Links Info" For i=0 to cnt-1 'Datatable.SetCurrentRow(i+1) link=coll(i).GetROProperty("name") If (link"") Then oShell.popup link,1,"Links Info" End If If (link="blogs") Then coll(i).click Exit For End If Exit For Datatable.Value("MyLinks","Global")=link Print coll(i).GetROProperty("name") Next datatable.ExportSheet "D:\shruthi.xls","Global" Set oLink=Nothing 'Code for closing the multiple Browsers Option Explicit Msgbox Browser("CreationTime:=0").Exist While Browser("CreationTime:=0").Exist Browser("CreationTime:=0").page("Index:=0").webedit("index:=0").Set "raghuqa" wait 1 Browser("CreationTime:=0").page("Index:=0").webedit("index:=1").SetSecure "4a2b3fd87cbee9b873cbb15580f68489d5e426f73945c9806993" wait 1 Browser("CreationTime:=0").page("Index:=0").webbutton("index:=0").Click wait 1 Browser("CreationTime:=0").page("Index:=1").Link("index:=0").Click wait 1 Browser("CreationTime:=0").Close Wend ' code for closing multiple windows

Option explicit Msgbox Dialog("index:=0","text:=Login").Exist While Dialog("index:=0","text:=Login").Exist Dialog("index:=0","text:=Login").WinEdit("index:=0","attached text:=Agent Name:").Set "asdf" wait 1 Dialog("index:=0","text:=Login").WinEdit("index:=1","attached text:=Password:").Set "mercury" wait 1 Dialog("index:=0","text:=Login").WinButton("index:=0","text:=OK").Click wait 1 window("text:=Flight Reservation").Close Wend 'Code for creating the text file by using the FSO Option Explicit Dim fso,myFolder,myfile Const Forwriting=2,ForAppending=8 Set fso=CreateObject("scripting.filesystemobject") Set myFolder=fso.CreateFolder("D:\shruthi") Set myfile=fso.Createtextfile("D:\shruthi\file123.txt",Forwriting,True) 'Set myfile=fso.OpenTextFile("D:\shruthi\file123.txt",ForAppending ,True) myfile.writeline "My Name is Krish" myfile.writeblanklines(1) myfile.writeline "My Name is Raghu" myfile.writeblanklines(1) myfile.writeline "My Name is Srinivas" myfile.close Set fso=Nothing Set myfile=Nothing Set myFolder=Nothing 'Code for DataBase Connection first method with DSN Option Explicit Dim conn,recobj,recordcnt,fieldscnt,col,i,j Datatable.AddSheet("Jampani") Set conn=CreateObject("ADODB.Connection") conn.open "DSN=QT_Flight32" 'conn.open "Server=jampani;Driver=Microsoft Access Driver (*.mdb);DBQ=C:\Program Files\Mercury Interactive\QuickTest Professional\samples\flight\app\flight32.mdb;Trusted_Connection=yes" 'conn.open "Server=jampani;Driver=SQL Server;DataBase=Master;UserID=sa;Password=sa" 'conn.open "Server=jampani;Driver=OracleDriver;DataBase=Master;UserID=sa;Password=sa" Msgbox "Connection Status: "&conn.State

If (conn.state=1) Then Reporter.ReportEvent micPass,"Checking the DataBase Connections","DB Connection success" Else Reporter.ReportEvent micFail,"Checking the DataBase Connections","DB Connection failure" End If Set recobj=CreateObject("ADODB.Recordset") recobj.open "Select * from Flights where Flight_Number