Right Click Disabled

Sunday, February 26, 2012

VBScript code in QTP to create a dictionary object to access a dictionary, to store five pairs of data into that dictionary and then to find total marks stored in that dictionary.

Option explicit
Dim dico, i, sn, m, lm, tot
'Here     i       for loop
'            sn     for subject name
'            m     for marks
'            lm    for list of marks
'            tot   for total
'Create dictionary object and add pairs of data
Set dico=CreateObject("Scripting.Dictionary")
For i=1 to 5 step 1
      sn=inputbox("Enter Subject Name : ")
      m=inputbox("Enter Marks : ")
      dico.Add sn,m
Next
'Find total marks in that dictionary
tot=0
lm=dico.Items
For each m in lm
      tot=tot+m
Next
Print("Total Marks is :"&tot)
Set dico=nothing

Thursday, February 23, 2012

Dictionary Object


Dictionary is used to store pairs of data and every pair first value is called as Key and second value is called as Item. To access this dictionary pairs we can use an object called as dictionary object.



The corresponding dictionary object can create an interface between QTP tool and a dictionary in memory (RAM). To create dictionary object and access dictionary content using that object, we can use below code in VBScript in QTP tool:

Option explicit
Dim dico
Set dico=CreateObject("Scripting.Dictionary")

In the above code createobject() function is used to create an object for specified data structure. Here “dico” is an object for a dictionary.

To store data into dictionary as pairs, dictionary object is providing a function or method like shown below:

Option explicit
Dim dico
Set dico=CreateObject("Scripting.Dictionary")
dico.Add "English", 78
dico.Add "Hindi", 65
dico.Add "Maths", 98
dico.Add "Computers", 90
dico.Add "Physics", 80

While adding pairs of data to dictionary using dictionary object, corresponding test automater can maintain unique values for Keys.

VBScript code in QTP for to display addition of two arrays


Option explicit
Dim array1(4), array2(4), array3(4), i
For i=0 to 4 step 1
      array1(i)=inputbox("Enter value for Array 1 :")
Next
For i=0 to 4 step 1
      array2(i)=inputbox("Enter value for Array 2 :")
Next
For i=0 to 4 step 1
      array3(i)=cint(array1(i))+cint(array2(i))
      print(array3(i))
Next

VBScript code in QTP to display given value is Palindrome


Option explicit
Dim val, res
val=inputbox("Enter any value : ")
res=strreverse(val)
If val=res Then
   msgbox(val&" is a Palindrome.")
   else
      msgbox(val&" is not a Palindrome.")
End If

OR

Option explicit
Dim val, res
val=inputbox("Enter any value : ")
res=strreverse(val)
If strcomp(val,res)=0 Then
   msgbox(val&" is a Palindrome.")
   else
      msgbox(val&" is not a Palindrome.")
End If

VBScript code in QTP to display given value in reverse


Option explicit
Dim val, res
val=inputbox("Enter any value : ")
res=strreverse(val)
msgbox("Reverse of  "&val&" is : "&res)

VBScript code in QTP to display last four characters in given value


Option explicit
Dim val, lastfour
val=inputbox("Enter any value : ")
lastfour=right(val,4)
msgbox("Last four characters in "&val&" is : "&lastfour)

Wednesday, February 22, 2012

VBScript code in QTP to display first four characters in given value


Option explicit
Dim val, firstfour
val=inputbox("Enter any value : ")
firstfour=left(val,4)
msgbox("First four characters in "&val&" is : "&firstfour)

VBScript code in QTP to display number of words in given sentence


Option explicit
Dim sentence, nw
sentence=inputbox("Enter any sentence : ")
nw=split(sentence," ")
msgbox("Number of words in given sentence is : "&ubound(nw)+1)

VBScript code in QTP to create five elements array, to store five values in to that array in any type, display alphabets and alphanumerics in that array.


Option explicit
Dim val(4), i
For i=0 to 4 step 1
     val(i)=inputbox("Enter any values :")
Next
For i=0 to 4 step 1
     If not isnumeric(val(i)) Then
        print(val(i))
     End If
Next

VBScript code in QTP to create five elements array , to store five values in any type and display numbers only.


Option explicit
Dim val(4), i
For i=0 to 4 step 1
     val(i)=inputbox("Enter any values :")
Next
For i=0 to 4 step 1
     If isnumeric(val(i)) Then
         print(val(i))
     End If
Next

VBScript code in QTP to create an array with five elements to store a student five subject marks and display minimum marks


Option explicit
Dim subject(4), i, min
For i=0 to 4 step 1
     subject(i)=inputbox("Enter marks :")
Next
min=subject(0)
For i=0 to 4 step 1
     If min>subject(i) Then
        min=subject(i)
     End If
Next
msgbox("Maximum marks : "&min)

VBScript code in QTP to create an array with five elements to store a student five subject marks and display maximum marks


Option explicit
Dim subject(4), i, max
For i=0 to 4 step 1
     subject(i)=inputbox("Enter marks :")
Next
max=subject(0)
For i=0 to 4 step 1
     If max<subject(i) Then
         max=subject(i)
     End If
Next
msgbox("Maximum marks : "&max)

VBScript code in QTP to create an array with five elements to store a student five subject marks and display total marks


Option explicit
Dim marks(4), i, total
total=0
For i=0 to 4 step 1
    marks(i)=inputbox("Enter marks :")
    total=total+marks(i)
Next
msgbox("Total marks of student : "&total)

VBScript code to display gross salary of an employee


Here, Gross Salary is equal to basic salary plus commission. If basic salary >=15000 then commission is 10% of basic salary. If basic salary <15000 and >=8000 then commission is 5% of basic salary. If basic salary <8000 then commission is 100/-

Option explicit
Dim gross, basic, comm
basic=inputbox("Enter Basic salary of an Employee :")
If basic>=15000 Then
   comm=basic*10/100
   elseif basic<15000 and basic>=8000 Then
       comm=basic*5/100
       else
          comm=100
End If
gross=basic+comm
msgbox("Gross salary of Employee is :"&gross)

VBScript code in QTP to display grade of a student depends on total marks.


Here, Grade is “A” when total marks >=800, Grade is “B” when total marks<800 and >=700, Grade is “C” when total marks <700 and >=600, Grade is “D” when total marks<600.

Option explicit
Dim totalmarks
totalmarks=inputbox("Enter Total Marks of student :")
If totalmarks >=800 Then
   msgbox("Grade is A")
   elseif totalmarks<800 and totalmarks>=700 Then
       msgbox("Grade is B")
       elseif totalmarks<700 and totalmarks>=600 Then
            msgbox("Grade is C")
            else
              msgbox("Grade is D")
End If

VBScript code in QTP to find given number is ever or odd


Option explicit
Dim x
x=inputbox("Enter Number :")
If x mod 2 = 0 Then
    msgbox(x&" is Even number")
    else
       msgbox(x& " is Odd number")
End If

Displaying output in QTP


To display output while running VBScript code on desktop, test automaters are using below syntax.

msgbox(“Welcome to divineQTP”)

Type Casting

Simply type casting is the procedure to convert the data from one type to another in any programming language.

Type casting Functions:

Cint() à Convert to integer
Cdbl() à Convert to double(float)
Clng() à Convert to long integer
Cdate() à Convert to date in format
Ccur() à Convert to currency
Cstr() à Convert to string

Here, default value type is string.

Addition of two numbers get from keyboard in QTP

Option explicit
Dim x, y, z
x=inputbox("Enter First Number :")
y=inputbox("Enter Second Number :")
z=cint(x)+cint(y)
msgbox(z)

Note: In VBScript, Plus(+) operator is overriding operator. It perform addition and concardination. To get addition operation for plus(+) operator, test automaters are using type casting concept.

Multiplication of two numbers get from keyboard in QTP

Option explicit
Dim x, y, z
x=inputbox("Enter First Number :")
y=inputbox("Enter Second Number :")
z=x*y
msgbox(z)
Twitter Bird Gadget