Search This Blog

Thursday, 27 October 2011

Dictionary Objects with Examples


Dictionary objects:-

          Vbscript can support dictionary concept to store your required data as pairs. In every pair first value called as “KEY” and second value called as “ITEM”.

            In a dictionary keys must be unique,because items will be accessable by using  keys.


                                
           
            To create a dictionary ,
                                                            Option explicit
                                                            Dim d
                                                            Set d=createobject(“scripting.dictionary”)

*.To store pairs of data to dictionary, we can use below code.

            Static                                                                           Dynamic

Option explicit                                                            option explicit
Dim d                                                                          dim d,i,j,k
Set d=createobject(“scripting.dictionary”)     set d=createobject(“scripting.dictionary)
d.add “English”,98                                                     for i=1 to 3
d.add “telugu”,78                                                                   j=inputbox(“enter key”)
d.add “maths”,99                                                                    k=inputbox(“enter item”)
                                                                                                d.add j,k
                                                                                    next


           




Ex:-1
                        To create a dictionary ,to store subject names and marks and then calculate total marks.

                       
Option explicit
                        Dim marklist, i, j, k, total, nos, x, y
                        Set marklist=createobject(“scripting.dictionary”)
                        Nos=inputbox(“no:of subjects”)
                        For i=1 to nos
                                    J=inputbox(“enter the subject name”)
                                    K=inputbox(“enter the subject markx”)
                                    Marklist.add j,k
                        Next
                        Total=0
                        X=marklist.items
                        For each y in x
                                    Total=total+y
                        Next
                        Print(“total marks” &total)
                        Set marklist=nothing

            Ex:-2
                        To create dictionary , to store specify no:of subjects and marks and then display given dictionary.

                                    Option explicit
                                    Dim d, i, j, k, nos, x, y
                                    Set d=createobject(“scripting.dictionary”)
                                    Nos=inputbox(“enter no:of subjects”)
                                    For i=1 to nos
                                                J=inputbox(“enter the subject names”)
                                                K=inputbox(“enter the subject marks”)
                                                d.add j,k
                                    next
                                    x=d.keys
                                    for each y in x
                                                print(y&” “&d.item(y))
                                    next
                                    set d=nothing

Details of "Redim" & "Redim preserve"


NOTE:-
            QTP’s vbscript can allows you to resize array using “redim” stmt.

                                    Option explicit
                                    Dim x( )
                                    --------
                                    --------
                                    Redim x(10)
                                    --------
                                    --------
NOTE:-
            QTP’s vbscript allows you to resize array without losing existing data by using “preserve”.

                                    Option explicit
                                    Dim x( )
                                    ----------
                                    ----------
                                    Redim x(10)
                                    -----------
                                    -----------
                                    Redim preserve x(20)
                                    -----------
                                    -----------
                                    -----------

For loop & For Each loop with Examples


A)    For loop:-
We can use this loop to run a block of code specified number of times.

                        Syntax:-          for i=1 to n
                                                     ---------                ====è loop runs n times.
                                                     ---------         
                                                Next

EX:-    for i=1 to 10 step 2
                        ---------             =====è loop runs  5 times
                        ---------
                        Next



EX:-    for i=10 to 1 step (-1)
                        ---------             =====è loop runs 10 times
                        ---------
                        Next
B)    For Each loop:-
We can use this loop to execute a block of code for each item in given list.

                        Syntax:-          for each i in list/array
                                                            ---------
                                                            ---------
                                                Next
Arrays:-
                        Array is a list of values in any type.

                        EX:-    x(4)
101
Kishore
23.4
A
23a4
                              X(0)           x(1)             x(2)             x(3)                x(4)

From the above example, x is an Array with 5 elements . every array first element index is zero, called as Lbound. In index of last element in an array Ubound. So the size of array is Ubound+1.

To store data to an array , we can use below code.

Method:-1                                                                               Method:-2
Static Array                                                                           Dynamic Array

Option explicit                                                                        option explicit
Dim x                                                                                      dim x(4),i
X=array(“kish”,23,43.22)                                                       for i=0 to 4
                                                                        X(i)=inputbox(“enter value”)
                                                                        Next

            EX:-
                        To read 5 subject marks of a student and then display total marks.
                       
                                    Option explicit
                                    Dim x(5),I,total
                                    For i=0 to 4
                                                X(i)=inputbox(“enter the subject marks”)
                                    Next
                                    Total=0
                                    For i=0 to 4
                                                Total=total+x(i)
                                    Next
                                    Print(“total=”&total)

           


Ex:-2
                        To read 5 subject marks of  a student and then display highest marks in list.

                                    Option explicit
                                    Dim x(5),I,max
                                    For i=0 to 4
                                                X(i)=inputbox(“enter the subject marks”)
                                    Next
                                    Max=x(0)
                                    For i=1 to 4
                                                If  cint(max)<cint(x(i)) then
                                                            Max=x(i)
                                                End if
                                    Next
                                    Print(“maximum marks=”&max)

Ex:-3
            To read specify no:of employees & to read every emp salary. If salary graters then equal 30000 the comm. 10% . sal<30000 and sal>=15000, then comm is 5% .If  sal<15000, then  comm is 200. Here gross salary= bsal+comm. We can replace bsal with gross in that array.

                                    Option explicit
                                    Dim emp(10), noe, i, comm., gsal
                                    noe=inputbox(“enter no:of employess”)
                                    for i=0 to noe-1
                                                emp(i)=inputbox(“enter emp bsal”)
                                    next
                                    for i=0 to noe-1
                                                if(emp(i)>=30000) then
                                                            comm.= emp(i)*10/100
                                                            gsal=emp(i)+comm.
                                                            Emp(i)=gsal
                                                Elseif(emp(i)<30000 and emp(i)>=15000) then
                                                            Comm.=emp(i)*5/100
                                                            Gsal=emp(i)+comm.
                                                            Emp(i)=gsal
                                                Else
                                                            Comm.=200
                                                            Gsal=emp(i)+comm.
                                                            Emp(i)=gsal
                                                End if
                                    Next
                                    For i=0 to noe-1
                                                Print(emp(i))
                                    Next

Syntax's for Print( ),Do-While, Do-Untile loop


Print( ):-
                      We can use this QTP function to display output in a printlog window.

                                    Syntax:-           print(“msg”)
                                                            Print(variables”)
                                                            Print(“msg” &variable)

A)    Do-While:-
The execution of a block of code as long as given condition was true, but in “do-while” loop back of code atleast one time when condition was failed.

                        Syntax:-          do
                                                ----------
                                                ----------
                                                Loop while condition

B)    Do-untile loop:- 
To execution of a block of code as long as given condition was false.

                        Syntax:-          do
                                                ----------
                                                ----------
                                                Loop untile condition
                                                ----------
                                                ----------

Loop Statements with Examples


LOOP STATEMENTS:
         
          The execution of a block of code more then one time is called as loop. Like all programming language vbscript is also allows you to create different types of loops.

A)    While loop:-
To run a block of code as long as condition was true.
           
           
EX:-1
            Option explicit
            Dim i, j
            i=1
            j=inputbox(“enter the number”)
            while(i<=j)
                        msgbox(i)
            wend

            EX:-2
                        To display all even numbers , in between 1 to limit.
                                    Option explicit
                                    Dim i, x
                                    X=inputbox(“enter the number limit”)
                                    i=2
                                    while(i<=cint(x))
                                                print i
                                                i=i+2
                                    wend

Select Case with Examples


C) Select case:-
           
             To alternative code dependences on data instead of conditions , we use this concept.

            EX:-1
                        Option explicit
                        Dim cctype
                        Cctype=inputbox(“enter the card type”)
                        Select case cctype
                                    Case “visa”
                                    Msgbox(“valid card”)
                                    Case “master”
                                    Msgbox(“valid card”)
                                    Case else
                                    Msgbox(“invalid card”)
                        End select

EX:-2
                        Option explicit
                        Dim daynum
                        Daynum=inputbox(“enter the day number”)
                        Select case dnum
                                    Case 1
                                                Msgbox(“Sunday”)
                                    Case 2
                                                Msgbox(“Monday”)
                                    Case 3
                                                Msgbox(“Tuesday”)
                                    Case 4
                                                Msgbox(“Wednesday”)
                                    Case 5
                                                Msgbox(“Thursday”)
                                    Case 6
                                                Msgbox(“Friday”)
                                    Case 7
                                                Msgbox(“Saturday”)
                                    Case else
                                                Msgbox(“invalid number”)
                        End select

Conditional Statements with Examples


Conditional Statements:

A) Condition:- To execute a code dependens on condition, we can use this statement.

                                    Syntax:-           IF condition Then
                                                                        ----------
                                                                        ----------
                                                                        ----------
                                                            End IF


B) else-IF condition:-

                                    Syntax:-           IF condition Then
                                                                        ----------
                                                                        ----------
                                                            Else
                                                                        ----------
                                                                        ----------
                                                            End IF
            EX:- Write vbscript in QTP to check given number is even or not.
                                   
                                    Option explicit
                                    Dim a
                                    a=Inputbox(“enter the number”)
                                    IF a Mod 2=0 Then
                                                Msgbox(“Even”)
                                    Else
                                                Msgbox(“odd”)
                                    End if

To check multiple conditions , we can follow syntax,
           
                        IF condition 1 Then
                                    ---------
                                    ---------
                        Elseif  condition 2 Then
                                    ---------
                                    ---------
                        Elseif  condition 3 Then
                                    ---------
                                    ---------
                        Else
                                    ---------
                                    ---------
                        End if

In above syntax, “else” block will be execute all conditions was false.

            EX:-1 
                        To display grade of students dependence on total marks. Here grade A is when total marks >=800, B grade is <=800 & >=700, C grade is <700 & >=600, and D grade is <600. 
                                    Option explicit
                                    Dim m
                                    M=inputbox(“enter the total marks”)
                                    If  m>=800 then
                                                Msgbox(“grade is A”)
                                    Elseif m<800 and >=700
                                                Msgbox(“grade is B”)
                                    Elseif m<700 and >=600
                                                Msgbox(“grade is C”)
                                    Else
                                                Msgbox(“grade iis D”)
                                    End if
            EX:-2
                        To display gross sal of emp dependence on basic sal. Here commission is 10% on basic  salary, when basic sal >=30000, commission is 5% when basic sal <30000 & >=15000, commission is 200 Rs when basic sal <15000.
  Gross sal=basics sal+ commission.
                       
Option explicit
                        Dim bsal ,comm., gsal
                        bsal=inputbox(“enter the basic sal”)
                        if bsal>=30000 then
                                    comm=bsal*10/100
                        elseif bsal<30000 and bsal>=15000
                                    comm.=bsal*5/100
                        else
                                    comm.=200
                        end if
                        gsal=bsal+comm.
                        Msgbox(gsal)

EX:-3

            Write vbscript in qtp, To display maturity amount dependence on commission and fixed deposit amount. Here commission is 10% when Age of depositor >=60 years.In general commission is 8.5% for others.

                        Option explicit
Dim amt,comm,age,tamt
amt=InputBox("Enter the amount")
age=InputBox("Enter the Age")
If age>60 Then
                                    Comm=amt*10/100
                        Else
                                    Comm=amt*8.5/100
                        End if
                        tmt=amt+comm.
                        Msgbox(tamt)