Wednesday, 25 June 2014

QTP REAL TIME SCRIPTS



1) if condition

Dim x
x= 20
If x>10  Then
            msgbox "Hello akhil"
            msgbox "x value is: "&x
            msgbox "Bye Bye"
End If

2) if else

x= Inputbox (" akki")
If x>100  Then
            Msgbox "Hello akhil"
            Msgbox "X is a Big Number"
            Msgbox "X value is: "&X
Else
            Msgbox "akhilreddy"
    Msgbox "X is a Small Number"
   Msgbox "X value is: "&X
End If

3) else if

Dim x
x= Inputbox (" 1000")

If x>0 and x<=100 Then
            Msgbox "Hello srss"
            Msgbox "X is a Small Number"
            Msgbox  "X value is "&x

Else IF x>100 and x<=500 Then
            Msgbox "Hello srss"
            Msgbox "X is a Medium Number"

Else IF x>500 and x<=1000 Then
            Msgbox "Hello akhil"
            Msgbox "X is a Large Number"

Else
            Msgbox "Hello Sir"
            Msgbox "X is a Grand Number"
End If
End If
End If

4)  Executing a certain block of statements when two / more conditions are True (Nested If...)



Dim State, Region
State=Inputbox ("Ap")
Region=Inputbox ("telangana")

If state= "AP"  Then
            If Region= "Telangana" Then
                        msgbox "Hello srss"
                        msgbox "Dist count is 10"

Else if Region= "Rayalasema" Then
                        msgbox "Hello srss"
                        msgbox "Dist count is 4"

Else If Region= "Costal" Then
                        msgbox "Hello akhil "
                        msgbox "Dist count is 9"

End If
End If
End If
End If

5) Making Decisions with Select Case

Option explicit
Dim x,y, Operation, Result
x= Inputbox (" akhil")
y= Inputbox ("akki")
Operation= Inputbox ("add")

Select Case Operation

            Case "add"
                                    Result= cdbl (x)+cdbl (y)
                                    Msgbox "Hello Akhil Reddy"
                                    Msgbox "Addition of x,y values is "&Result

            Case "sub"
                                    Result= x-y
                                    Msgbox "Hello Hello Akhil Reddy "
                                    Msgbox "Substraction of x,y values is "&Result

            Case "mul"
                                    Result= x*y
                                    Msgbox "Hello Hello Akhil Reddy "
                                    Msgbox "Multiplication of x,y values is "&Result

            Case "div"
                                    Result= x/y
                                    Msgbox "Hello Hello Akhil Reddy "
                                    Msgbox "Division of x,y values is "&Result

            Case "mod"
                                    Result= x mod y
                                    Msgbox " Hello Akhil Reddy "
                                    Msgbox "Mod of x,y values is "&Result

            Case "expo"
                                    Result= x^y
                                    Msgbox " Hello Akhil Reddy "
                                    Msgbox"Exponentation of x,y values is "&Result

            Case Else
                                    Msgbox " Hello Akhil Reddy "
                                    msgbox "Wrong Operation"

End Select

6) Write a program for finding out whether the given year is a leap year or not?

Dim xyear
xyear=inputbox ("akhil")

If xyear mod 4=0 Then
            msgbox "This is a Leap year"
Else
            msgbox "This is NOT leap year"
End If

7) Write a program for finding out whether the given number is, Even number or Odd number?

num=inputbox ("akhil")

If num mod 2=0 Then
            msgbox "This is a Even Number"
Else
            msgbox "This is a Odd Number"
End If

8)  Display Odd numbers up to n?

Dim num,n
n=Inputbox ("akhil")
For num= 1 to n step 2                                     
msgbox num
Next

9) Display Even numbers up to n?

Dim num,n
n=Inputbox ("akhil")
For num= 2 to n step 2
msgbox num                                                                                                                        
Next

10) display natural numbers up to n and write in a text file?

Dim num, n, fso, myfile
n= inputbox ("akhil")
num=1
For num= 1 to n step 1
Set fso= createobject ("scripting.filesystemobject")
set myfile=fso.opentextfile ("E:\akhil.txt", 8, true)
myfile.writeline num
myfile.close
Next

11) Display Natural numbers in reverse order up to n?

Dim num,n
n=Inputbox ("akhil")
For num=n to 1 step -1
msgbox num
Next                                                                                                                                       

12) Display Natural numbers sum up to n?  (Using For...Next Loop)

Dim num, n, sum
n= inputbox ("1000")
sum=0
For num= 1 to n step 1
            sum= sum+num
Next
            msgbox sum

13) Display Natural numbers sum up to n? (using While...Wend Loop)

Dim num, n, sum
n= inputbox ("1000")
While num <=cdbl (n)
            sum= sum+num
            num=num+1
Wend
            msgbox sum


14) Display Natural numbers sum up to n? (Using Do...Until...Loop)

Dim num, n, sum
n= inputbox ("1000")
sum=0
num=1
Do
            sum= sum+num
            num=num+1
Loop Until num =cdbl (n+1)
            msgbox sum


15) We can use Do...Loop statements to run a block of statements an indefinite number of times.




Do While x<5 x=x+1
            Msgbox "Hello akhil"
            Msgbox "Hello QTP"
Loop

16) For...Next Statement


Dim x
For x= 1 to 5 step 1
Msgbox " Hello Akhil Reddy "
Next



17) array

MyArray = Array("one","two","three","four","five")
For Each element In MyArray
       msgbox element
Next



18) Repeats a group of statements for each element in an array or collection.
  


Dim a,b,x (3)
a=20
b=30
x(0)= "Addition is "& a+b
x(1)="Substraction is " & a-b
x(2)= "Multiplication is  " & a*b
x(3)= "Division is " &  a/b

For Each element In x
       msgbox element
Next



19) read a number and verify that number Range weather in between 1 to 100 or 101 to 1000?

Option explicit
Dim a,x
a=Inputbox ("akhil")
a=cdbl(a)
If a<= 100 Then
For x= 1 to 100
If a=x Then
            msgbox "a is in between 1 to 100 range"
End If
Next
else
For x= 101 to 1000
If a=x Then
msgbox "a is in between 101 to 1000 range"
End If
Next
End If

20) read Data and find that data size, If size <>4 then display invalid data message, if data size = 4 then verify “a” is there or not in that data?

Dim x
x=Inputbox ("akhil")
x1=Right(x,1)
x2=Left (x,1)
x3=mid (x,2,Len(1))
x4=mid (x,3,Len(1))
y=len(x)
If y=4 Then
If  x1="a" or x2="a" or x3="a" or x4="a" Then
msgbox "a is there"
else
msgbox "a is Not there"
End If
else
msgbox "Invalid Data"
End If


21) Count, how many Buttons and Edit boxes available in Flight Reservation main window.


     Dialog("Login").Activate

   Dialog("Login").WinEdit("Agent Name:").Set "akhilreddy"

     Dialog("Login").WinEdit("Password:").Set "mercury"

    Dialog("Login").WinButton("OK").Click



   Set Desc = Description.Create()

    Desc("micclass").Value = "WinButton"

 Set Buttons = Window("text:=Flight Reservation").ChildObjects (Desc)

 Num_Buttons = Buttons.Count()



Set Desc1=Description.Create()

 Desc1("micclass").Value="WinEdit"

 Set Editboxes=Window("text:=Flight Reservation").ChildObjects (Desc1)

 Num_Editboxes= editboxes.count ()

sum= Num_Buttons+Num_Editboxes

 Reporter.ReportEvent 2, "Res","Total Buttons: "& Num_Buttons &"Total Edit boxes: "& Num_Editboxes





Pass         0
Fail           1
Done       2
Warning  3

22) Count all opened Browsers on desktop and close all?

Set Desc = Description.Create()

    Desc("micclass").Value = "Browser"
     Set Browsers =Desktop.ChildObjects (Desc)

    NumberofBrowsers = Browsers.Count()

    Reporter.ReportEvent 2,"Res","Number of Browsers are: "&NumberOfBrowsers

    For Counter=0 to NumberofBrowsers-1

     Browsers(Counter).Close

     Next

23) Create an Excel file, enter some data and save the file through VB scripting?


    Set objExcel = createobject("Excel.application")

    objexcel.Visible = True

     objexcel.Workbooks.add

     objexcel.Cells(1, 1).Value = "akhil is a good boy"

     objexcel.ActiveWorkbook.SaveAs("f:\exceltest.xls")

     objexcel.Quit

24) Get Test Data from a Flat file and use in Data Driven Testing (through Scripting)
Dim fso,myfile
      Set fso=createobject("scripting.filesystemobject")
    Set myfile= fso.opentextfile ("f:\aa.xls",1)
      myfile.skipline
      While myfile.atendofline <> True
      x=myfile.readline
      s=split (x, ",")
      SystemUtil.Run "C:\Program Files\Mercury Interactive\QuickTest Professional\samples\flight\app\flight4a.exe"
  Dialog("text:=Login").Activate
Dialog("text:=login","width:=320").winedit("attached text:= Agent Name:").set"akhil"
Dialog("text:=login").winedit("attachedtext:=password:").setsecure"mercury"
Dialog ("text:=login").winbutton("text:=ok").click
Window("Flight Reservation").Close
wend

25)Checking weather the File is available or not, if not creating the File


strDirectory="E:\"
strFile="aaa.xls"
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists(strDirectory & strFile) Then
Set objFolder = objFSO.GetFolder(strDirectory)
Else
Set objFile = objFSO.CreateTextFile("E:\aaa1.xls")
End if

26) Creating a Flat File
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile=objFSO.CreateTextFile("E:\akhil.txt")





27)DELETE A FLAT FILE


Set objFSO=createobject("Scripting.filesystemobject")
Set txtFilepath = objFSO.GetFile("E:\AKHIL.txt")
txtFilepath.Delete()


28) Find how many alpha characters present in a string.


str = "ca1b2c3d4"
n = len(str)
cnt = 0
For i = 1 to n
If isnumeric((mid(str,i,1))) = false Then
cnt = cnt+1
End If
Next
msgbox cnt



29)To counting numeric characters present in a string

str = "ca1b2c3d4"
n = len(str)
cnt = 0
For i = 1 to n
If isnumeric((mid(str,i,1))) Then
cnt = cnt+1
End If
Next
msgbox cnt


30)Write a program to return ASCII value of a given character
msgbox asc("A")

31)Write a program to return character corresponding to the given ASCII value
msgbox Chr(65)

32) Convert string to Upper Case
msgbox ucase(“string”)

33)Convert string to lower case
Msgbox lcase(“string”)


34) Write a program to Replace a word in a string with another word

str = "Replace space with tab in between the words of a string"
replstr = "strings"
Set oregexp = new regexp
With oregexp
.pattern = "string"
.global = true
.ignorecase = true
End With
newstring = oregexp.replace(str,replstr)
msgbox newstring


35) Check whether the string is a POLYNDROM
36) Verify whether given two strings are equal

str1 = "the"
str2 = "the"
msgbox strcomp(str1,str2)
If  0 then
            MsgBox"hi"
            else
            MsgBox"hello"

            end if
           
   37) Print all values from an Array

a =array("a","b","c")
For each s in a
msgbox s
Next
38) Find number of words in string

str = "count how many words in a string"
s = split(str," ")
'For i = 0 to ubound(s)
'msgbox s(i)
'Next
'msgbox i

msgbox ubound(s)+1

39)Write a program to reverse the words of a given string.

str = "count how many words in a string"
s = split(str," ")
For i = ubound(s) to 0 step -1
revstring = revstring&" "&s(i)
Next
msgbox revstring

40) Write a program to insert 100values and to delete 50 values from an array

dim a(100)

For i = 1 to 100
a(i) = i

If a(i) = 51 Then
            Exit for
            Erase a(i)
End If
print a(i)
Next

41) Write a program to print all drives in the file system

Set fso = createobject(“scripting.filesystemobject”)
Set odrive = fso.drives
For each d in odrive
Print d.driveletter
Next

42 )Write a program to print the free space in a given drive

Set fso = createobject(“scripting.filesystemobject”)
Set odrive = fso.drives
For each d in odrive
Print d.availablespace
Next

43) Write a program to display all subfolders in a given folder

On error resume next
Set fso = createobject("scripting.filesystemobject")
Set odrive = fso.Drives

For each d in odrive
print d
print "______________________________________________________________________"
Set folderz = fso.GetFolder(d)
Set subfol = folderz.SubFolders
For each fol in subfol
print fol.name
print "______________________________________________________________________"
Set filez = fol.files
For each f in filez
print f.name
Next
Next
Next

44) Write a program to remove all empty files in the folder

Set fso = createobject("scripting.filesystemobject")
Set fold = fso.GetFolder("d:\testfolder")
Set filez = fold.Files
For each f in filez
fsize = f.size
If fsize = 0 Then
            f.delete
End If
Next
45) Swap 2 numbers with out a temporary variable

a = 2
b = 3
a = a+b
b = a-b
a = a-b
msgbox "value of a is       "&a
msgbox "value of b is       "&b

46) what is the difference between By Val and By ref

Function getval(byval x, byref y)
   x = x+1
   y = y+1
print "x="&x&"        "&"y="&y
End Function

Dim a: a = 5
dim b:  b = 5
call getval( a, b )
print "a="&a&"        "&"b="&b

47) Replace space with tab in between the words of a string.

str = "Replace space with tab in between the words of a string"
replstr = "      "
Set oregexp = new regexp
With oregexp
.pattern = " "
.global = true
.ignorecase = true
End With
newstring = oregexp.replace(str,replstr)
msgbox newstring

48)add ElementsAddDictionary

Dim cities
Set cities = CreateObject("Scripting.Dictionary")
cities.Add "h", "Hyderabad"
cities.Add "b", "Bangalore"
cities.Add "c", "Chennai"
akhil=cities.item("b")

msgbox akhil


49) List the Number of Items in a Dictionary

Dim cities
Set cities = CreateObject("Scripting.Dictionary")
cities.Add "h", "Hyderabad"
cities.Add "b", "Bangalore"
cities.Add "c", "Chennai"


abc=cities.item("b")
msgbox abc
msgbox cities.count


50) Verify the Existence of a Dictionary Key




Set objDictionary = CreateObject("Scripting.Dictionary")

objDictionary.Add "Printer 1", "Printing"  
objDictionary.Add "Printer 2", "Offline"
objDictionary.Add "Printer 3", "Printing"

If objDictionary.Exists("Printer 4") Then
    msgbox" Printer 4 is in dictionary"
Else
  msgbox "Printer 4 is not in the Dictionary."
End If



51) application
Set d=createobject("scripting.dictionary")
d.add "u", "sekhar"
d.add "p" ,"mercury"

'
'dialog("Login").WinEdit("Agent Name:").Set d.item ("u")
'dialog("Login").WinEdit("Password:").Set d.item ("p")
'dialog("Login").WinButton("OK").Click


abc= d.item ("u")
MsgBox abc




No comments:

Post a Comment