vb scripts examples

Download VB Scripts Examples

Post on 28-Apr-2015

28 views

Category:

Documents

9 download

Embed Size (px)

TRANSCRIPT

21-02-2008 1. Swap 2 numbers with out temporary variable a=cint(inputbox("Enter the value of a to swap:")) b=cint(inputbox("Enter the value of b to swap:")) print "Value of a&b befor swaping:"&vbnewline&"a="&a&vbnewline&"b="&b a=a+b b=a-b a=a-b Print "Value of a&b after swapping:"&vbnewline&"a="&a&vbnewline&"b="&b 2. Print prime numbers a=cint(inputbox("Enter the starting range of no.:")) b=cint(inputbox("Enter the ending range of no.:")) For i= a to b For j= 2 to i-1 If i mod j=0 Then flag=1 Exit for End If Next If flag=0 Then print(i) End If flag=0 Next 3. Print the data in triangle shape str=inputbox("Enter the string:") l=len(str) j=0 For i=l to 1 step -1 j=j+1 tri=mid(str,1,j) print space(i)&tri Next 4. Sort Array elements a=array(100,10,50,40,80,30) For j=lbound(a) to ubound(a)-1 For k=lbound(a) to ubound(a)-1 If a(k)Admin Browser("YellowPages").Page("YellowPages").WebElement("Login").GetTOProperty("Exist") Browser("YellowPages").Page("YellowPages").WebEdit("Login").Set "admin" Browser("YellowPages").Page("YellowPages").WebEdit("Password").Set "admin" Browser("YellowPages").Page("YellowPages").WebButton("Login").Click

32. Read items in a list box - YP -> Admin->Entries->Oracle->Category icount=Browser("YellowPages").Page("YellowPages").WebList("category_id").GetTOProperty("item s count") print icount For i=1 to icount itemlist=Browser("YellowPages").Page("YellowPages").WebList("category_id").GetItem(i) print itemlist Next 33. Read items on a desktop icount=Window("Program Manager").WinListView("SysListView32").GetItemsCount() For i=0 to icount-1 itemlist=Window("Program Manager").WinListView("SysListView32").GetItem(i) print itemlist Next 34. Place and retrieve data in a dictionary Set dict=createobject("Scripting.Dictionary") dict.add "A","Advance" dict.add "B","Brilliant" dict.add "C","Congrats" dict.add "D","Dynamic" val=dict.keys itm=dict.items For i=0 to dict.count-1 print val(i)&" "&"for"&" "&itm(i) Next 35. Read items from a tabbed window -> System->Properties icount=dialog("System Properties").WinTab("SysTabControl32").GetItemsCount() For i=0 to icount-1 itm=dialog("System Properties").WinTab("SysTabControl32").GetItem(i) print itm Next 36. Check whether scrollbars exists inside a editor -> Notepad sbar=window("Notepad").WinEditor("Edit").GetRoProperty("hasvscroll") If sbar=true Then msgbox "Yes, Existing" else msgbox "No, not Existing"

End If 37. Print data from oracle database Set db=createobject("ADODB.Connection") db.open="DSN=menaka;UID=scott;PWD=tiger;SERVER=oracle" If db.state=1 Then msgbox "Connected" else msgbox "Not Connected" End If Set result=db.execute("Select * from tony") colcount=result.fields.count colname=" " For i=0 to colcount-1 colname=colname&vbtab&result.fields(i).name Next print colname While not result.eof rval=" " For i=0 to colcount-1 rval=rval&vbtab&result.fields(i).value Next print rval result.movenext Wend 38. Insert a new row into the database Set db=createobject("ADODB.Connection") db.open="DSN=menaka;UID=scott;PWD=tiger;SERVER=oracle" Set result=db.execute("insert into tony values(105,'veena','QA',7000.00)") msgbox "Inserted" 39. Update specific field in the database Set db=createobject("ADODB.Connection") db.open="DSN=menaka;UID=scott;PWD=tiger;SERVER=oracle" Set result=db.execute("update tony set sal=9500.00 where sno=100") msgbox "updated" 40. Write a program to delete all records whose username starts with demo Set db=createobject("ADODB.Connection") db.open="DSN=menaka;UID=scott;PWD=tiger;SERVER=oracle" Set result=db.execute("delete from tony where name like demo")

msgbox "deleted" 41. Find the x and y coordinates of a button YP->Home-> search xval=Browser("YellowPages").Page("YellowPages").WebButton("Search").GetROProperty("x") print "Value of 'x' coordinate is:"&" "&xval yval=Browser("YellowPages").Page("YellowPages").WebButton("Search").GetROProperty("y") print "Value of 'y' coordinate is:"&" "&yval 42. Check whether edit box is focused -> YP->Home->Name focus=browser("YellowPages").Page("YellowPages").WebEdit("name").GetROProperty("focused") If focus=0 Then msgbox "Not focused" else msgbox "Focused" End If 43. Check default selection in list box - YP -> Admin->Entries->Oracle->Category defsel=browser("YellowPages").Page("YellowPages").WebList("category_id").GetTOProperty("defau lt value") print defsel 44. Print URL name YP ->Home url=browser("YellowPages").Page("YellowPages").GetROProperty("url") print url 45. List all links in the web page - YP->Home set obj=description.Create obj("html tag").value="A" set chobj=browser("YellowPages").Page("YellowPages").ChildObjects(obj) msgbox chobj.count For i=0 to chobj.count-1 list=chobj(i).GetROProperty("innertext") print list Next 46. List all applications opened in the system (window task manager) icount=dialog("Windows Task Manager").WinListView("SysListView32"). GetItemsCount() msgbox icount For i=0 to icount-1 itemlist=dialog("Windows Task Manager").WinListView("SysListView32").GetItem(i) print itemlist

Next 47. Check window resizable - FR resize=window("Flight Reservation").GetROProperty("hassizebox") If resize=false Then msgbox "Done" else msgbox "Wrong" End If 54.Check window resizable - FR a=window("Flight Reservation").GetROProperty("maximizable") b=window("Flight Reservation").GetROProperty("minimizable") print a print b if b= true and a = false Then print("not resizable") else print("resizable") End If 48. Write your own standard checkpoint If ( window("Flight Reservation").WinButton("Insert Order"). CheckProperty("enabled",true) eqv true) then msgbox "pass" else msgbox "fails" end if 49. List the process running in the computer (Services window) ccount=window("Services").Window("Services (Local)").WinListView("SysListView32").ColumnCount() icount=window("Services").Window("Services (Local)").WinListView("SysListView32").GetItemsCount() For i=0 to icount-1 rval=" " For j=0 to ccount-1 rval=rval&vbtab&window("Services").Window("Services (Local)").WinListView("SysListView32").GetSubItem(i,j) Next print rval Next

50. Check given text displayed on the web page YP->Home (dynamic site) Browser("YellowPages").Page("YellowPages").Check CheckPoint("YellowPages") 51. Capture Desktop Screen shot window("Program Manager"). WinListView("SysListView32"). CaptureBitmap("C:\scrshot.bmp") 52. Find whether image contains tool tip YP->Home->Yellow Pages Logo ttip=browser("YellowPages").Page("YellowPages").Image("CC-YelloPageslogo").GetROProperty("alt") If ttip=true Then msgbox "Exist" else msgbox "Not Exist" End If 53. Invoke Application in the Browser -> YP invokeapplication"C:\Program Files\Internet Explorer\IEXPLORE.EXE" browser("Cannot find server"). Navigate("http://localhost:8081/yellowpages/Default.jsp") (or) systemutil.Run("http://localhost:8081/yellowpages/Default.jsp") 54. Read and Update data from environmental variable systemutil.Run("C:\Program Files\Mercury Interactive\QuickTest Professional\samples\flight\app\flight4a.exe") environment.LoadFromFile("C:\Documents and Settings\j27menaka\Desktop\inficspractice.xml") dialog("Login").Activate dialog("Login").WinEdit("Agent Name:").Set environment ("AgentName") dialog("Login").WinEdit("Password:").Set environment ("Password") dialog("Login").WinButton("OK").Click 55. Find font type and size -> YP->Home->Top fnt=browser("YellowPages").Page("YellowPages").WebElement("Top").GetROProperty("outerhtml") print fnt

56. Read and display data from CSV file -> inficsinfo.csv (Open a text file then save that with extension .csv) Set fso=createobject("Scripting.FileSystemObject") Set fd=fso.opentextfile("E:\Menaka\inficsinfo.csv") Do until fd.atendofstream rd=fd.readline print rd Loop 57. Write a Program to print NOW I AM CONFIDENT IN VBSCRIPTING and QTP to print log, text file and Data table Dim a(8) For i=0 to 7 a(i)=inputbox("Enter the string:") Next str=join (a) print str Set fso=createobject("Scripting.filesystemobject") Set opfile=fso.opentextfile("E:\Menaka\test.txt",2) opfile.write(str) opfile.close datatable.GetSheet("Global").addparameter "Menaka",str wait 3 58. Add and Remove Repositories to the action reppath="E:\Menaka\share.tsr" (add objs in repo then export those objs repositoriescollection.Add(reppath) wait 4 59. Write a program to open and print a word document Set fso=createobject("Scripting.filesystemobject") Set fdr=fso.opentextfile("E:\Menaka\tony.doc",2) fdr.write "Hi everybody"&vbnewline&"How are you"&vbnewline&"How is your class going on" fdr.close 60. Find screen resolution scrhgt=window("Program Manager").WinListView("SysListView32"). GetROProperty("height")

scrwid=window("Program Manager").WinListView("SysListView32"). GetROProperty("width") print "Screen resolution is:"&vbnewline&"Height"&" "&scrhgt&vbnewline&"Width "&" "&scrwid 61. Read data from all sheets and all parameters of the file inficsinfo.xls datatable.ImportSheet "C:\Data.xls",1,1 set s=DataTable.GetSheet("Global") t=s.getrowcount msgbox t x=s.getparametercount MsgBox x for i=1 to t ch="" For j=1 to x val=datatable.Value(j) ch=ch&vbtab&val Next print ch datatable.SetNextRow next 62. Display entire data in the web table -> YP Admin -> Categories -> Categories Do rcount=browser("YellowPages").Page("YellowPages").WebTable("Categories").RowCount() For i=1 to rcount-1 gitem=browser("YellowPages").Page("YellowPages").WebTable("Categories").GetCellData(i,1) print gitem Next If browser("YellowPages").Page("YellowPages").link("Next").Exist Then continued=true browser("YellowPages").Page("YellowPages").Link("Next").Click else continued=false Exit do End If loop while continued=true 65. Report Results status to Results file a=inputbox("enter the value") If a=10 Then reporter.Repo