Monday, 6 August 2012

Vbscript level 2


VB Script Syntax

1                    Swap 2 numbers with out temporary variable

Program

Dim a,b
a= int(InputBox("Enter the First value to Swap"))
b= int(InputBox("Enter the Second value to Swap"))
a=a+b
b=a-b
a=a-b
print "After Swap :  a = " &a&"& "&" b = " & b





2                    Print prime numbers

Program
Dim A,t,i,j
int  Val
int i
Val =int(InputBox("Enter the Value"))
For i = 2 to Val
      t = 0
For j =1 to (i-1)
     A=i mod j 
           If (A=0) then          
         t = t + 1  
        End if
Next
 If t <=1 Then
                  print i
    End If
Next
      




3                    Sort Array elements

Program

Dim a(3)
a(0)=10
a(1)=30
a(2)=20
For i=0 to ubound(a)
      For j=i+1 to ubound(a)
                  If a (i)>a (j) Then
                              a(i)=a(i)+a(j)
a(j)=a(i)-a(j)
a(i)=a(i)-a(j)
                  End If
      Next
Next
For i=1 to 3
  print a(i)
next
                  
      
         

4                    Print elements in a collection
Program
Dim a(3)
For i=0 to 2
  a(i)=inputbox("Enter the numbers:")
Next
For i =0 to 2
 print a(i)
Next


5                    Find a character in a string
Program
Dim str, res, c
str=InputBox("Enter The String")
c = InputBox("Enter the Charecter to Find the Position in String")
res=instr(str,c)
print "The letter " & c & " is in " & res & " position in Word " & str


6                    Print the data in triangle shape
Program
Dim Str,I,j
str = InputBox("Enter The String to Ptrint in Triangle")
x=len(str)
            For j= x to 1 step -1
                        res=mid(str,j)
                         print res
            Next


7                    Replace space with tab in between the words of a string.
Program
MyStr="Born To Lead The World"
res1=replace(MyStr," ",chr(9))
print res1



8                    Convert string to Upper Case
Program
a = InputBox("Enter the String in Lower Case")
b = ucase(a)
Print b


9                    Returning Character specific to ASCII value
Program
print chr(65)
print chr(55)


10               Display current date and Time
Program
print now

11               Finding difference between 2 dates.
Program
a = InputBox("Enter Date in the format of MM/DD/YYYY")
print  datediff("d",a, now)


12               Converting an array to string
Program
Dim MyArray(3)
MyArray(0)="Born"
MyArray(1)="To"
MyArray(2)="Lead"
res=join(MyArray)
print res

13               Replace a word in a string with other word
Program
MyStr="Rajesh Kumar"
print replace(MyStr,"Rajesh","Sathish")

14               Create your own Class and Object

Program

Class a
Public function f
   MsgBox("Function Called")
End Function
End Class
Set obj = new a
obj.f


15               Read and display data from a text file
Program
Dim fso, f1, c
Set fso = CreateObject("Scripting.FileSystemObject")
Set f1 = fso.Opentextfile("D:\test.txt" )
c = f1.readall
print c

                 
16               Find all subfolders in a folder
Program
   Dim fso, f, f1, fc, s
   Set fso = CreateObject("Scripting.FileSystemObject")
   Set f = fso.GetFolder("D:\")    
   Set fc = f.SubFolders 
   For Each f1 in fc
      s = s & f1.name &" "
   Next
       print s




17               Remove all empty files in the folder
Program
Set a=createobject("scripting.filesystemobject")
                  set foldr=a.getfolder("D:\tpr")
                  Set subf=foldr.subfolders
       For each s in subf
         If s.size = 0 Then
                               print s.name & " is Empty. Hence it is Deleted"
                               s.delete
             End If
  next

18               From file Print all the lines that starts with “Error”
Program
Set a=createobject("scripting.filesystemobject")
Set b=a.createtextfile("D:\test.txt")
b.writeline("Error Born to Lead")
b.writeline("Hi How r U")
b.writeline("Error Have a nice day")
b.close
set c=a.opentextfile("D:\test.txt")
While not c.atendofstream
      x=c.readline
      res=split(x," ")
         If res(0)="Error" Then
                        print x                                  
         End If
Wend




19               Replace a word with another word
Program
word="Hai How R U"
res=replace(word,"Hai","Hi")
print res


20               Check whether string is in email format
Program
e = "vtprakash@yahoo.com"
   set re=new RegExp
  re.pattern="(\w+)@(\w+)\.(\w+)"
 Set a = re.execute(e)
 For each b in a
print b
 Next


21               Check whether given string is in date format
Program
a = "01/08/2008"
b = "hasjkdfk"
c = isdate(a)
d = isdate(b)
print c
print d



22               Place and retrieve data in a dictionary

Program
set a=createobject("scripting.dictionary")
a.add "t","Taminadu"
a.add "b","Bombay"
a.add "v","Visak"
addeditem=a.item("t")
print addeditem
print a("b")

23               Retrieve all the cols and rows in a table
Program
Dim a,b
Set a=createobject("ADODB.connection")
Set b=createobject("ADODB.recordset")
a.connectionstring="DSN=Prakash; UID=scott; pwd=tiger;"
a.open
b.open "select * from emp", a
While not b.eof
      print b("empno")&"       "&b("ename")
      b.movenext
Wend


                     

24               Find the name of columns in the table

Program
Dim a,b
Set a=createobject("ADODB.connection")
Set b=createobject("ADODB.recordset")
a.connectionstring="DSN=Prakash;UID=scott;pwd=tiger;"
a.open
b.open "select column_name as a  from user_tab_columns where table_name='EMP'",a
While not b.eof
      print b("a")
      b.movenext
Wend


25               Find number of rows in each column
Program
Dim a,b
Set a=createobject("ADODB.connection")
Set b=createobject("ADODB.recordset")
a.connectionstring="DSN=Prakash;UID=scott;PWD=tiger;"
a.open
b.open "select count(ename) as abc from emp",a
While not b.eof
      print "No of rows in table is: " &b("abc")
      b.movenext
Wend

26               Print data from oracle database
Program
Dim a,b
Set a=createobject("ADODB.connection")
Set b=createobject("ADODB.recordset")
a.connectionstring="DSN=Prakash;UID=scott;PWD=tiger;"
a.open
b.open "select * from emp",a
While not b.eof
      print b("ename")
      b.movenext
Wend
Output
27               Insert a new row into the database
Program
Dim a, b
Set a = CreateObject("ADODB.Connection")
Set b = CreateObject("ADODB.Recordset")
a.connectionstring = "DSN=Prakash;UID=scott;PWD=tiger;"
a.open
b.open  "insert into MARK  values(256)",a
b.open "select * from mark",a
While not b.eof
      print b("sno")
      b.movenext
 Wend

28               Update specific field in the database
Program
Dim a,b
Set a = CreateObject("ADODB.Connection")
Set b = CreateObject("ADODB.Recordset")
a.connectionstring = "DSN=Prakash;UID=scott;PWD=tiger;"
a.open
b.open "update mark set sno =257  where sno=256 ",a
b.open "select * from MARK"
While not b.eof
      print b("sno")
      b.movenext
Wend




29               Write a program to delete all records whose username starts with demo
Program
Dim a,b
Set a = CreateObject("ADODB.Connection")
Set b = CreateObject("ADODB.Recordset")
a.connectionstring = "DSN=Prakash;UID=scott;PWD=tiger;"
a.open
b.open "delete emp1  where ename='demo' ",a
b.open "select * from emp1"
While not b.eof
      print b("eno")&b("ename")
      b.movenext
Wend


30               Find the arguments passed to a Windows Script Host
Program
Set a = CreateObject("wscript.shell")
b = a.popup("Prakash",5,"hi")


31               Stop and Start alerter service
Program
Input
Output






QTP Scripting Examples


32               Write a Script to enter data in login screen
Program
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_2").WinEdit("Agent Name:").Set "prakash"
Dialog("Login_2").WinEdit("Agent Name:").Type  micTab
Dialog("Login_2").WinEdit("Password:").SetSecure "49537b37c47dab2a3a25ee0fa0bc756aecd34f0c"
Dialog("Login_2").WinButton("OK").Click




33               Find the x and y coordinates of a button
Program
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_2").WinEdit("Agent Name:").Set "prakash"
Dialog("Login_2").WinEdit("Agent Name:").Type  micTab
Dialog("Login_2").WinEdit("Password:").SetSecure "49537b37c47dab2a3a25ee0fa0bc756aecd34f0c"
Dialog("Login_2").WinButton("OK").Click
x = Window("Flight Reservation").WinButton("Button").GetROProperty("x")
y = Window("Flight Reservation").WinButton("Button").GetROProperty("y")
print x
print y


34               Read items in a list box
Program
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_2").WinEdit("Agent Name:").Set "prakash"
Dialog("Login_2").WinEdit("Agent Name:").Type  micTab
Dialog("Login_2").WinEdit("Password:").SetSecure "49537b37c47dab2a3a25ee0fa0bc756aecd34f0c"
Dialog("Login_2").WinButton("OK").Click
Window("Flight Reservation").WinObject("Date of Flight:").Type "122222"
Window("Flight Reservation").WinObject("Date of Flight:").Type  micTab
Window("Flight Reservation").WinComboBox("Fly From:").Select "Denver"
Window("Flight Reservation").WinComboBox("Fly To:").Select "London"
Window("Flight Reservation").WinButton("FLIGHT").Click
a = Window("Flight Reservation").Dialog("Flights Table").WinList("From").GetItemsCount
For i = 1 to a
b = Window("Flight Reservation").Dialog("Flights Table").WinList("From").GetItem(i)
print b
Next




35               Read items on a desktop
Program
a = Window("Program Manager").WinListsView("SysListView32").GetItemsCount
For i = 0 to a-1
b = Window("Program Manager").WinListView("SysListView32").GetItem (i)
print b
Next

Output
36               Capture Desktop Screen shot
Program

window("Program Manager").CaptureBitmap "D:\tp.bmp",True



37               Read items from a tabbed window
Program
a = Dialog("System Properties").WinTab("SysTabControl32").GetItemsCount
For i = 0 to a - 1
b = Dialog("System Properties").WinTab("SysTabControl32").GetItem(i)
print b
Next


 





38               Check whether scrollbars exists inside a editor
Program
Window("Microsoft Word").Activate
Window("Microsoft Word").WinObject("_WwG").Click 650,232
a = Window("Microsoft Word").WinScrollBar("ScrollBar").Exist
If a = true  Then
      Print "Scroll Bar Exist"
      else
      Print "Scroll Bar Not Exist"
End If
39               Check scroll bars in a web page
It is not possible to capture the scroll bar in Webpage by QTP
40               Check whether dialog is middle of  the window
Program
window("Flight Reservation").Activate
x = Window("Flight Reservation").GetROProperty("x")
y = Window("Flight Reservation").GetROProperty("y")
fh = Window("Flight Reservation").GetROProperty("height")
fw = Window("Flight Reservation").GetROProperty("width")
fcx = x + (fw/2)
fcy = y + (fh/2)
print fcx
print fcy
Window("Flight Reservation").WinButton("Button_4").Click
Window("Flight Reservation").Dialog("Open Order").Activate
x1 = Window("Flight Reservation").Dialog("Open Order").GetROProperty("x")
y1 = Window("Flight Reservation").Dialog("Open Order").GetROProperty("y")
dh = Window("Flight Reservation").Dialog("Open Order").GetROProperty("height")
dw = Window("Flight Reservation").Dialog("Open Order").GetROProperty("width")
dcx = x1 + (dw/2)
dcy = y1 + (dh/2)
print dcx
print dcy
If fcx = dcx and fcy = dcy Then
      print "Dialog Box is in Middle of Window"
      else
       print "Dialog Box is not in Middle of Window"
End If


41               Check whether edit box is focused
Program
Window("Flight Reservation").Activate
If Window("Flight Reservation").WinEdit("Name:").GetROProperty("focused") then
      MsgBox("Edit Box Focused")
      else
       MsgBox("Edit Box Not Focused")
Window("Flight Reservation").Minimize
end if

42               Check default selection in list box
Program
a = Browser("Flight Confirmation: Mercury").Page("Flight Confirmation: Mercury").WebList("delCountry").GetROProperty("default value")
If a = NULL Then
        MsgBox("Default is not  Working")
        else
        MsgBox("Default is Working")
End If


43               List all links in the web page
Program

Set a = Description.Create()
 a("micclass").Value = "Link"
Set b = Browser("Welcome: Mercury Tours").Page("Register: Mercury Tours").ChildObjects(a)
    c = b.Count()
      print "Total links:" &c
    print "links are: "
    print ""
For i = 0 To c - 1
d=b(i).getroproperty("name")
   print d
next

Output

44               Print URL name
Program
a = Browser("Flight Confirmation: Mercury").Page("Flight Confirmation: Mercury").GetROProperty("url")
Print "The URL of Flight Confirmation: Mercury Page is " & a
Output
45               Check whether webpage downloaded completely
Program
a = Browser("Flight Confirmation: Mercury").Page("Flight Confirmation: Mercury").Object.readystate
Output



46               Check given text displayed on the web page
Program

Browser("Flight Confirmation: Mercury").Page("Sign-on: Mercury Tours").WebEdit("userName").Set "prakash"
u = Browser("Flight Confirmation: Mercury").Page("Sign-on: Mercury Tours").WebEdit("userName").GetROProperty("Value")
Browser("Flight Confirmation: Mercury").Page("Sign-on: Mercury Tours").WebEdit("password").SetSecure "4954e23a039786d3e0cd54f84888a864"
p = Browser("Flight Confirmation: Mercury").Page("Sign-on: Mercury Tours").WebEdit("password").GetROProperty("value")
Browser("Flight Confirmation: Mercury").Page("Sign-on: Mercury Tours").WebEdit("userName").Output CheckPoint("userName")
Browser("Flight Confirmation: Mercury").Page("Sign-on: Mercury Tours").WebEdit("password").Output CheckPoint("password")
u1 = DataTable("a",1)
p1 = DataTable("p",1)
If  (u=u1) and (p=p1)Then
      print "The entered uname and pwd is displayed correctly"
      else
      print "The entered uname and pwd is not displayed correctly"
end if


47               Find whether image contains tool tip
a =Browser("Flight Confirmation: Mercury").Page("Find a Flight: Mercury").Image("contact").GetROProperty("alt")
print "The tooltip for the img Contact is:"&" "& a






48               Invoke Application in the Browser

Program


49               Read data from all sheets and all parameters of the file
Program
Datatable.ImportSheet "D:\prakash.xls","order",1
Window("Flight Reservation").WinButton("Button").Click
Window("Flight Reservation").Dialog("Open Order").WinCheckBox("Order No.").Set "ON"
Window("Flight Reservation").Dialog("Open Order").WinEdit("Edit").Set DataTable("orderno", dtGlobalSheet)
Window("Flight Reservation").Dialog("Open Order").WinButton("OK").Click
50               Import data to excel file from a database
Dim a,b,x
Set a = CreateObject("ADODB.Connection")
Set b = CreateObject("ADODB.Recordset")
a.Connectionstring="DSN=sql;UID=scott;PWD=tiger;"
a.open
b.open "select  * from prak",a
x=1
While not b.eof
      Datatable.GetSheet(1).setcurrentrow(x)
      datatable.Value("name")=b(0)
      datatable.Value("city")=b(1)
      x = x+1
      b.movenext
Wend
Datatable.ExportSheet "D:\testing.xls",1



No comments:

Post a Comment