Thursday, September 20, 2012

Factorial Function

'Factorial of number


Function factorial(no)

Dim fact
fact=1

For i=1 to no

fact=fact*I

Next

factorial=fact

End Function

num=inputbox("Enter number")

msgbox "Factorial is" &factorial(num)

Friday, September 14, 2012

String is palindrome or not

'string is palindrome or not
str=Inputbox ("enter string")
revstr=strreverse(str)
If str=revstr Then
    msgbox "string is palindrome"
else
    msgbox "string is not palindrome"
End If

Find Square of Number

' Program to square of number
num=inputbox ("Enter number","Square of number")
square=num*num
msgbox "Square of number is " &square

Example of LCASE function- “AuTOMatIon123_!@#” and 'convert all the upper case letter to lower case like “automation123_!@#”.


'Write a program that mimics the functionality of LCase function.
' For example, your program should take any variable like “AuTOMatIon123_!@#” and
'convert all the upper case letter to lower case like “automation123_!@#”.

'Code
Str = InputBox("String to be lowered")
diff = Asc("a") - Asc("A")

For i = 1 to Len(Str)
If Asc(Mid(Str,i,1))<=Asc("Z") And Asc(Mid(Str,i,1))>=Asc("A")Then
    If Asc(Mid(Str,i,1)) = Asc(" ") Then
    StrLower= StrLower + Mid(Str,i,1)
    End If
    temp= chr(Asc(Mid(Str,i,1))+diff)
    StrLower= StrLower+temp
Else
    StrLower= StrLower + Mid(Str,i,1)
End If
Next
MsgBox StrLower
'End Code

Reverses the order of words (not characters) in a sentence -“Good Morning Everybody” to “Everybody Morning Good”


' Write a program that reverses the order of words (not characters) in a sentence.
'For example, the program should change the sentence “Good Morning Everybody” to “Everybody Morning Good”

StrPR2 = InputBox("String to be Reversed")
SplitStr= Split(StrPR2)
For i = UBound(SplitStr) to 0 Step -1
StrRev=StrRev+" "+SplitStr(i)
Next
MsgBox StrRev

Program to display output in format V, B, S, c,r,i,p,t


'Given a string, write a program that finds out all the characters that constitute the string.
' Example – If the string is VBScript, the output (using a msgbox) should be in this format
' The string VBScript contains variables – V, B, S, c, r, i, p, t

Dim str
str = InputBox("Enterr string to be reversed","Trupti's trial prog")
iLen = Len(str)
For i =1 to iLen
temp=Mid(str,i,1)
RevStr= RevStr+temp+","
Next
msgbox "Reverse string example" &RevStr

VBScript - Working with Array

'Working with Array
' Dim keyword used to define array
' Redim keyword used to size or resize the arry
' Preserve optional- used to preserve the data in exisiting array, while you resize it
' Array Function
'Array(arglist) - use to define the array arguments
Dim arr1
arr1=Array("Sunday","Monday","Tuesday","Wedseday","Thursday","Friday","Saturday") ' first way of declare array with elements

'UBOUND() upperbound of array
msgbox "Upper bound of Array is " &UBOUND(arr1)
For i=0 to UBOUND(arr1)
        msgbox arr1(i)
Next

'LBOUND() lowerbound of array
msgbox "Lowerbound of Array is " &LBOUND(arr1)

'ISARRAY  function indicates whether a specific variable is array or not
Dim Var
Dim isarr(3) ' second way of declare array with given upper value and assign value to array elements
isarr(0)="one"
isarr(1)="Two"
isarr(2)="Three"
var=isarray(isarr)
msgbox "Isarr is variable is array or not" &var

Dim withoutupperlimitarr() ' declare array with no upper limit
var=IsArray(withoutupperlimitarr)
msgbox "withoutupperlimitarr is variable is array or not" &var

'Erase statement is used to empty array
Erase isarr
For i=0 to UBound(isarr)
    msgbox isarr(i)
Next

'Join() function joins substrings of an array into one long string with each substring separated by delimiter
'Join (list,[delimiter])
Dim a
Dim Joinex(3)
joinex(0)="This"
joinex(1)="is"
joinex(2)="join"
joinex(3)="example"
a=Join(joinex)
msgbox "Join string without delimiter" &a
a=join(joinex,",")
msgbox "Join string with delimiter" & a

'Split function returns a zero based , one dimenensional array containing a specified number of substring
'Split (expression, [,delimiter],count[,compare]) 0 means binary compare and 1 means text compare
Dim str,myarr, i
str="split * function* example"
myarr=split(str,"*",-1,1)
For i=0 to ubound(myarr)
    msgbox myarr(i)
Next

'Filter function returns zero based array containing a subset of a string array based on criteria specified
'Filter (inputstring, value,[,include[,compare])
'input string and value are required fields
' if include set to True return the subset of array contains the value as substring
' if include set to False then filter return the subset of the array that does not contains value as substring
a=Array("Apple","Orange","Banana","Olive","Apricoat")
b=filter(a,"A",True)
For each x in b
    msgbox "it shows all items containing A" &x
Next
c=filter(a,"A",False)
For each x in c
    msgbox "it shows all items NOT containing A" &x
Next
'Program to find the array elements start with A

Wednesday, September 12, 2012

String Functions in QTP

'string functions examples
' LTRIM() Left space remove function
str1="           Remove left space"
msgbox Ltrim(str1)

'RTRIM() Right space Remve function
str2="Remove right space              "
msgbox Rtrim(str2)

'Trim() Remove left and right space
str3= "       Remove left and right space           "
msgbox Trim(str3)

'LEN() to find the length of string function
str4= "find the length of string"
msgbox "Length of string is " &Len(str4)

'LCASE() UCASE() upper and lower case letter function
str5="AutoMATion word in Lower and UPPER case"
msgbox "in lowercase later"  &LCASE(str5)
msgbox "In uppercase later" & UCASE(str5)

'InStr function helps you determine if a smaller string is present in a bigger string or not.
'If a match is found, this functions returns a number that represents the position at which the match is found
sBigString = "Automation Repository"
sSmallString = "on Rep"
'Start match from 11th character
iReturnVal = InStr(11, sBigString, sSmallString, 1)
'From the above statement iReturnVal will have value 0 because we are matching from 11th character.
'So effectively, we are trying to find "on Rep" in "Repository"
msgbox "result of instr function" &iReturnVal

'InstrRev() function
iReturnValRev=InStrRev(sBigString, sSmallString)
msgbox "result of InStrRev function" &iReturnValRev

'Left function to find out a specified number of characters from the left of a string. Syntax: Left(String, Length)
Dim sMainString, sLeftString
sMainString = "Orange"
sLeftString = Left(sMainString, 2) 'Get the first two characters from the left of the string
msgbox  "Displays the value Or" &sLeftString

'Right function retrieves the specified characters from the right side of the string. Syntax: Right(String, Length)
sRightString = Right(sMainString, 2)
msgbox  "Display the value ge" &sRightString

'splits a string using a delimiter and returns a number of sub-strings stored in an array
'Syntax: Split(String, Delimiter(optional), Count(optional), Compare(optional))
sText = "Yes,No,Maybe"
sDelimiter = "," 'Delimiter is comma (,)
'Split the text using the delimiter. 'arrArray' is the array in which the fucntion Split will store the sub-strings
arrArray = Split(sText, sDelimiter)
'arrArray(0) contains the value Yes. arrArray(1) contains the value No and  arrArray(2 contains the value Maybe
'Display the sub-strings in a msgbox
iLoop = UBound(arrArray) 'Find trhe size of the array
For i = 0 to iLoop
   msgbox  "Split function result" &arrArray(i)
Next

'Mid function can be used to retrieve a sub-string from a main string Mid(String, Start, Length(optional))
Dim sMainString1, sSubString1, sSubString2
sMainString1 = "Orange"
sSubString1= Mid(sMainString1, 3, 2) ' variable 'sSubString' will have the value 'an'
msgbox "Substring upto length 2 by mid function" &sSubString1
sSubString2 = Mid(sMainString1, 3) ' variable 'sSubString1' will have the value 'ange'
msgbox "Sub string using mid function without length parameter" &sSubString2

'Replace function can be used to replace some sub-string within a main string with some other string.
replacestr="abaacaadea"
msgbox "Replace abaacaadea a character with b" &Replace(replacestr, a,b)

' Space function Returns a string consisting of the specified number of spaces
Dim MyString
MyString = Space(10)   ' Returns a string with 10 spaces.
MyString = "Hello" & Space(10) & "World" ' Insert 10 spaces between two strings.
msgbox "Display string with 10 space insert between two string" &MyString
'String function to create a repeating character string of a specified length. str = String(5, “a”) would return the string – “aaaaa”.
Dim MyString1
MyString1 = String(5, "*")   ' Returns "*****".
msgbox "string func value" &MyString1
MyString1 = String(5, 42)   ' Returns "*****".
msgbox "string func value" &MyString1
MyString1 = String(10, "ABC")   ' Returns "AAAAAAAAAA".
msgbox "string func value" &MyString1

'StrReverse function in QTP can be used to create a reverse of a string.
strrev=StrReverse("Trupti")
msgbox "String Reverse function" &strrev

'Join()- If you have a number of sub-strings in an array, you can use the Join function to concatenate all the sub-strings from the array into a single string.
Dim MyStringJoin
Dim MyArray3(3)
MyArray3(0) = "Mr."
MyArray3(1) = "John "
MyArray3(2) = "Doe "
MyArray3(3) = "III"
MyStringJoin = Join(MyArray3) ' MyString contains "Mr. John Doe III".
msgbox "Join String example" &MyStringJoin

'StrComp function can be used to compare two strings in QTP o means binary comparision and 1 means text comparision
Dim MyStr1, MyStr2, MyComp
MyStr1 = "ABCD": MyStr2 = "abcd"   ' Define variables.
MyComp = StrComp(MyStr1, MyStr2, 1)   ' Returns 0.
msgbox " text comparision ABCD abcd" &MyComp
MyComp = StrComp(MyStr1, MyStr2, 0)   ' Returns -1.
msgbox "binary compariion ABCD abcd" &myComp
MyComp = StrComp(MyStr2, MyStr1)   ' Returns 1.
msgbox "string comparision ABCD abcd" &MyComp

'Filter function returns a zero-based array containing a subset of a string array based on a specified filter criteria.
'If no matches of Value are found within InputStrings, Filter returns an empty array. An error occurs if InputStrings is Null or is not a one-dimensional array.
'The array returned by the Filter function contains only enough elements to contain the number of matched items
Dim MyIndex
Dim MyArray (3)
MyArray(0) = "Sunday"
MyArray(1) = "Monday"
MyArray(2) = "Tuesday"
MyIndex = Filter(MyArray, "Mon") ' MyIndex(0) contains "Monday".
msgbox "filter function output" &MyIndex(0)

Tuesday, September 4, 2012

To capture application screenshot and display it in test results

Hey folks,

A common feature that QTP testers are often asked to implement is to capture the application screenshot every time an error occurs during test run. This is definitely a good to have feature in your automation framework because -
a) When a script fails, you would have a proof that it failed due to an application issue and not due to your script, and
b) with the error screenshot, the developers usually find it easier to identify/fix the issue.

QTP provides a method called CaptureBitmap that can capture the image of either an object within the application or the entire screen/desktop and save it in any desired location. The image is saved as .png or .bmp file depending upon the file extension provided during the function call. The object is captured as it appears in the application when the method is performed. If only a part of the object is visible, then only the visible part is captured

Function fnDisplayBitmapInTestResults
URL="www.gmail.com"

 SystemUtil.Run "iexplore.exe", URL
browsernm=".*gmail.*"
Pagenm=".*gmail.*"
Set CurrObj=Browser("name:="&browsernm).Page("title:="&Pagenm)
wait(5)
currobj.CaptureBitmap "d:\Test.bmp"
Reporter.ReportEvent micDone ,"Image", "Image", "D:\Test.bmp"
End Function

call  fnDisplayBitmapInTestResults

Notes:
1. You cannot load images from Quality Center.
2. If you include large images in the run results, it may impact performance.
3. If you specify an image as a relative path, QTP will first search the Results folder for the image and then the search paths specified in the Folders pane of the Options dialog box

4 Different Ways to Associate Function Libraries to your QTP Scripts

4 Different Ways to Associate Function Libraries to your QTP Scripts
Most of the times, when you are creating test scripts or are designing a new QTP Framework, you would be trying to come up with reusable functions which you would have to store in the function library. Now, in order to use this function library with multiple test cases, you need to associate this function library with your scripts. This article explains the 4 methods that will help you in associating the function libraries in QTP Test Cases.

Based on the type of framework you are using, you can use any of the following methods to associate function libraries to your QTP Script -

  • By using ‘File > Settings > Resources > Associate Function Library’ option in QTP.
  • By using Automation Object Model (AOM).
  • By using ExecuteFile method.
  • using LoadFunctionLibrary method.