QTP Scripts Examples

Here are few QTP scripts examples like Leap year or not, Even or odd number, To add 2 number, To calculate Simple Intrest, Reverse a string etc.

You may like following testing tutorials:

1- Leap year or not?

Dim Year
Year=inputbox("Enter year")
If year mod 4=0 Then
msgbox "Leap year"
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"
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 Interest

Dim p,t,r,si
p=inputbox("Enter number")
t=inputbox("Enter number")
r=inputbox("Enter number")
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
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
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

Basic Unix commands useful for Testing

Now, we will see some basic Unix commands which are useful for Testing.

  1. $logname: Displays the current username.
  2. $pwd: Displays current working directory.
  3. $clear: Clear the screen
  4. $exit: log out from the current user.
  5. $date: Displays system date and time.
  6. $Who am i: Displays current username, terminal number, date and time at which you logged into the system.
  7. $who: To display the information about all the users who have logged into the system currently.
    i.e each user login name, terminal number, date and time that person logged in.
  8. $finger: Displays complete information about all the users who are logged in.
  9. $cal: Displays previous month, current month and next month calendar.
  10. $cal year: Displays the given year calendar.
  11. $cal month year: Displays the given month calendar only.
  12. init: To change system run levels. i. #init 0: To shut down the system
    ii. #init 1: To bring the system to single user mode.
    iii. #init 2: To bring the system to a multiuser mode with no resource shared.
    iv. #init 3: To bring the system multi-user mode with source shared.
    v. #init 6: Halt and reboot the system to the default run level.
  13. $banner “demo”: It prints a message in large letters.

Create files:
There are two commands to create files: touch and cat

  1. $Touch filename: it creates zero byte file size.
    Note: Touch does not allow us to store anything in a file. It is used to create several empty files quickly.
  2. $cat> filename
    i.eg. $cat> demo:To create a file.
    ii.eg. $cat>>demo: To append data to the file.
    iii.eg. $cat file1 file2 file3 > file4
    The above command would create file4 which contains contents of file1 followed by file2 and followed by that of file3. i.e it concatenates file1,file2 and file3 contents and redirects to file4. If file4 already contains something it would be overwritten.
  3. $cat< filename (or)$cat filename:To open a file.

Removing files:
rm command is used to remove a file.

  • $rm filename: It removes the file.
  • $rm -i filename:It asks confirmation before deleting the file.
  • $rm file1 file2 file3: It removes three files.
  • $rm*: It removes all files in the current directory.

Here, we check few QTP script examples with some basic Unix commands useful for Testing.

Donwload Hub site pdf

Download SharePoint Online Tutorial PDF FREE!

Get update on Webinars, video tutorials, training courses etc.