Blog to understand automation concepts in QTP, Selenium Webdriver and Manual Testing concepts

How to send e-mail using outlook object in QTP

This post explain how to send e-mail using microsoft outlook object with or without any attachments. In the next article we will explain how to read information from an e-mail message.

''Function Description - This function sends a mail to end user
'' Arguments - SendtoUser - Mail will be send to user mentioned in send to
''Subject - Subject of the mail
''Body - Text of the mail
''Attachment - Path of attachment to send e-mail

SendMailUsingOutlook "SendToUser@test.com; testZ@test.com"," Subject", "Body", ""

Function SendMailUsingOutlook(SendToUser, Subject, Body, Attachment) 

'' create an object of outlook application and creating a new e-mail object
    Set objOutlook=CreateObject("Outlook.Application") 
    Set objMail=objOutlook.CreateItem(0) 

'' Provide details of the mail object
    objMail.to=SendToUser
    objMail.Subject=Subject 
    objMail.Body=Body 

'' attach a file with the e-mail, provide path of the attachment in the folder
    If (Attachment <> "") Then 
        objMail.Attachments.Add(Attachment) 
    End If 
'' Send the e-mail
    objMail.Send 
'' close the instance of Outlook application object
    objOutlook.Quit 

''release the object created    Set objMail = Nothing 
    Set objOutlook = Nothing 
End Function 

Tutorial about Array in VBScript for QTP

Array is a data type used to describe a collection of elements. An array can be one-dimensional or multidimensional array. In VBScript, array can be represented as follows:


Dim arrayOned(6) - represent a dimension array which can store 7 elements. Index starts from 0.
Dim arraytwod(6)(4) - represents a two dimensional array which can store 7*5 =35 element
Dim(6)(4)(5) - Represents a three dimensional array. We can describe  n-dimension array in similar ways.

The above arrays are example of fixed array. Once we specify an array with dim and providing size of array, it is created as fixed dimension array. We cannot change the size of the array.
We can also create dynamic array in VBScript as shown below:

dim arraydynam() - create a variable without providing the size of array.Redim arraydynam(10) - we can then assign a size to the array by using redim. We can resize the array size as when required. 


On reducing the size of the array, loss of information can take place, so care should be taken while reducing the size of array in dynamic array.


There are some useful inbuilt functions in VBScript used to work with arrays. Let us discuss on the same.


Split Function - Split can be used to split a string into array based on the delimiter provided. Syntax for Split function is:

Split (expression [, delimiter [, count [, compare]]]) - here expression denotes the string which will be splitted based on the delimiter provided. 
Count and compare are optional with default value as -1 (repeats all substring) and binary compare. Default delimiter used is space.

Ubound function - Ubound returns largest subscript for the provided dimension of an array. In Case of argument for dimension not provided, it gives the ubound for first dimension

UBound(array, dimension))

Lbound function - Lbound returns smallest subscript for the provided dimension of an array. In Case of argument for dimension not provided, it gives the lbound for first dimension

UBound(array, dimension))

Join Function - Join function does opposite of what Split function does. It joins all the elements in the array using the delimiter. Syntax of function is:Join(array, delimiter)

In case of join also, the default delimiter if not provided is space character.

Filter Function - Filter is used to create a sub array from array matching the particular condition.
Filter(array, value, include, compare) - In the function, include and compare are optional parameters, include can have value true or false, true for searching for element with particular value matching and false to return elements which do not have match the condition

Array function - This function creates an array based on array element provided in the argument.

e.g. : a = array ("testing ", "array", "by" ,"creating", "array", "using array functions)

IsArray function returns true/false that indicates whether a specified variable is an array or not


''A simple code explaining the various functions for array manipulation

dim arr, strjoin
arr = split("this,is, wonderfrul, test",",") ' create an array using split function
ilowbound = lbound(arr) 'get lowerbound and upperbound for array
iupperbnd = ubound(arr)
for i=ilowboud to iupperbnd Step 1 '' Loop the array element
msgbox arr(i)
Next
b= filter(arr, "t")
msgbox b(1)
strjoin = join(arr, ";")
msgbox strjoin
msgbox Isarray(arr)

Understanding operators and built-in String functions in VBScript

In the previous article we discuss on defining a variable, different types of variables in VBscript . In this article we will discuss on various operators and built in string functions in VBScript as are frequently used in QTP.

Let us start by discussing different types of operators. Operators are broadly classified into 
- Logical Operators
- Arithmetic operators
- Comparison Operators

Logical Operators - Logical Operator are used to perform logical operations and returns boolean value as true or false based on the inputs provided and operator used.

Logical Operator used are Not ( Not True = false)
And (Returns true if both A and B are true, false otherwise)
OR(Returns true if either of A or B is true)
xor ( returns false if either of A or B is true)

Arithmetic Operators - We have been performing arithmetic operation from the day we start our education. So this is easy.  The various arithmetic operators in VBScript  are +(addition), - (subtraction), *(multiply), /(division), 
\(integer division, return the quotient), 
mod (a mod b, gives the remainder on dividing a by b),
^(exponential function).
Of the Operators , defined above , + is used to concatenate two strings also.

Comparison Operators are used to compare two variable . Example of comparison Operators are =(equality), <>(Inequality), <(Less than ), <(greater than), <=,>= ( greater than or equal to). To Compare strings, we can use strcomp function.

We have discussed the various operators used in VBScript.Now let us discuss on various string functions that are inbuilt in vbscript and are useful to work with strings

Instr function - This function returns the location of first occurrence of one string within another string. 
str1 = "This is testing blog on VBScript testing"
str2 = "testing"
iLoc = Instr(1,str1,str2,0)
msgbox iLoc

Syntax is InStr([start,]string1,string2[,compare]). start and compare are optional parameters with default value as 1 and 0(binary compare)

Instrrev function - This function returns the location of first occurrence of one string within another string. While Instr starts search from start of string,In Instrrev The search begins from the end of string. But the location returned is from start of string.
str1 = "This is testing blog on VBSCRIPT testing test"
str2 = "testing"
iLoc = Instrrev(str1,str2,-1,0)
msgbox iLoc

Syntax is InStrRev(string1,string2,[start,][compare]). start and compare are optional parameters with default value as -1(end of string) and 0(binary compare)

LCase(string) converts string to lowercase
Ucase(string) converts to uppercase.

len(string) returns number of character in a string.

Left, right and mid are used to return specified characters from the string starting from left,right, and middle of string
str3 = left(str1,10)
str4= right(str1, 10)
str5 = mid(str1,5,10) 

LTrim,RTrim and trim are used to remove spaces on the left, right or middle of a string
str6 = trim(str1)

strComp compares two string and returns value as 0 if both string matches.
blncomp = strcomp(str1,str2[,compare])

space(number) returns the space character returned times as provided in the argument.

String(number, character) returns a string with character repeated number of times as provided in the arguments.