Powered by Blogger.

Saturday, March 15, 2014

QTP scripts examples



In this post we will write some QTP scripts. Also you can check my previous posts on:

- Basic Unix commands useful for Testing

- Cost Of Defect Repair in Testing

- Capgemini Manual Testing Interview Questions & Answers

1- Leap year or not?

Dim Year
Year=inputbox("Enter year")
If year mod 4=0 Then
msgbox "Leap year"
else
msgbox"Not leap year"
End If

2- Even or odd number

Dim number
number=input box("Enter number")
If number mod 2=0 Then
msgbox "Even number"
else
msgbox"odd number"
End If

3- To add 2 number

Dim num1,num2,sum
num1=inputbox("Enter number")
num2=inputbox("Enter number")
sum=cint(num1)+cint(num2)//Cint is used here for string conversion
msgbox ("Sum is"& Sum)

4- To calculate Simple Intrest

Dim p,t,r,si
p=inputbox("Enter number")
t=inputbox("Enter number")
r=inputbox("Enter number")
si=(p*t*r)/100
msgbox ("Simple intrest  is"& si)

5- Reverse a string

StrtoReverse = inputbox("Enter the String:")
StrLenCount = Len(StrtoReverse)

For i = 1 to StrLenCount

StrReversed = Mid(StrtoReverse,i,1) & StrReversed

Next

Msgbox StrReversed

6- Find how many  "G" are available in the word “Google”.

Const ForReading = 1
Set fso = CreateObject( "Scripting.FileSystemObject" )
Set textFile = fso.OpenTextFile( "D:\sample.txt",1)
contents = textFile.ReadAll
textFile.Close
Set rgxp = New Regexp
rgxp.Pattern = "m"
rgxp.IgnoreCase = True' To ignore upper case and lower case
rgxp.Global = True
Set matches = rgxp.Execute( contents )
Print "Number of G: " & matches.Count