Saturday, February 16

Difference between Action, Procedure, Function and Sub?

Hello Friends!

I believe that many QTP newbie’s are confused about Action, Procedures, Function and Subs. Am I right?

No worries! Here we are going to discuss all of them in detail to eliminate all the confusions!

In QTP, there are two ways (on broad level); we can break up the code into logical units. Logical unit is nothing but a 'Piece of code' or ‘a series of VBScript statements’, to perform specific activity in QTP. These two ways are -

1. Actions - specific to QTP
2. Procedures - vbscript provided


Okie.. but what about Functions and Subs? Wait guys! we'll come there.

We know that we use vbscript as scripting language in QTP. (To read about vbscript, Click Here>> )

VBScript has two kinds of procedures: Sub procedure and Function procedure

So, in brief, its goes like this..


1. Actions
2. Procedures
   2.1. Subs/Subroutine
   2.2. Function

Now we know that Action and Procedures are the two things. Procedure is further devided in two types - Function & Sub.

Feeling batter? Great!!

So, Lets start with actions..

1. QTP Actions:-

Action is specific to QTP and not the part of vbscript. Every QTP test has at least one Action(default name is Action1). 
Action can have an object repository associated with it. Action can return multiple values in form of 'output parameters'.

2. Procedures:
2.1. VBScript Sub Procedures:-

A Sub procedure:

  • is a series of statements, enclosed by the Sub and End Sub statements
  • can perform actions, but does not return a value
  • can take arguments
  • without arguments, it must include an empty set of parentheses ()

Sub mysub()
  Print "my Sub Procedude"
End Sub

or

Sub mysub(argument1,argument2)
  Print "my Sub Procedure"
End Sub

How to call Sub Procedures:

To call a Sub, you will use Call statement by enclosing arguments (if any) in parentheses.


The Call statement is not necessary to call a Sub, but if you want to use Call statement (Recommended), you must enclose arguments (if any) in parentheses.

Call mysub(argument1,argument2)

You can call a Sub without using Call statement as well, but not recommended.

mysub argument1,argument2


2.2. VBScript Function Procedures:-

A Function procedure:

  • is a series of statements, enclosed by the Function and End Function statements
  • can perform operations and can return a value
  • can take arguments that are passed to it by a calling procedure
  • without arguments, must include an empty set of parentheses ()
  • returns a value by assigning a value to function name itself

Function myfunction1()
  Print "my fuction1"
End Function

or

Function myfunction2(a,b)
myfunction2=a+b  'assign value to function name
End Function


How to call Function Procedures:

Call myfunction1() 'calling 1st function, without any return value

abc=myfunction2(argument1,argument2)  'calling 2nd function, with a return value

Here you call a Function called "myfunction2", the Function returns a value that will be stored in the variable "abc".

Hope all your confusions are gone! But if you still have any doubts, please post your comments!
Continue Reading >>

Tuesday, December 18

How to get latest file in a folder

Hello friends!!

Here is a brief code for getting latest/newest file in a particular folder/directory...

Dim LatestFile
Set oFolder=CreateObject("scripting.FileSystemObject").GetFolder("E:\Dwnld")
For Each eachFile In oFolder.Files
    If LatestFile = "" Then
        Set LatestFile = eachFile
    Else If LatestFile.DateCreated < eachFile.DateCreated Then
            Set LatestFile = eachFile
        End If
    End If
Next

Msgbox LatestFile.Name
Continue Reading >>

Tuesday, July 3

Maintain a Log file in QTP

Hello Friends!

Don't you think its useful to maintain a log file during execution of your QTP script?

I think yes. It's specifically useful when you have long scripts or if your script is calling lots of functions. Log file gives a good grip on your execution. It is good for debugging purpose as well.

In this post we will see how to maintain a log file by using a easy vb function..


Function f_WriteToLogFile(LogMsg)
 Const ForAppending = 8
 Set oFSO = CreateObject("Scripting.FileSystemObject")

 'Check if file already exists. If not, create it.
 If oFSO.FileExists("c:\file_path\filename.txt") = False Then
   Set oFile = oFSO.CreateTextFile("c:\file_path\filename.txt",True)

   Set oFile = Nothing
 End If

  Set oTxtFile = oFSO.OpenTextFile("c:\file_path\filename.txt", ForAppending, True)
  oTxtFile.WriteLine LogMsg
End Function

How to call above function:


For example you have a script of 100 lines. You can call this function after every task/funtion call or in loops to see which condition was true or anything you want to send in your log file.


line 1
line 2
.
.
.
.
line 15
f_WriteToDriverLogFile("line 15 executed...")

line 16
line 17
 

once your execution is done, you can open your log file to see all log messages.
Continue Reading >>