Sunday, June 30, 2013

Use Of Regular Expression Object

What is Regular Expression ?

Regular expression is a sequence of characters. It means using regular expression object's
methods and properties a pattern can be found and manipulated.

Above description tells us that when to use regular expressions, to list a few major ones
that can be used in Automated Testing with QTP/VBScript:

1. To find a pattern in the content/text etc.

2. To create generic objects in Object Repository

3. To get collection of objects with a particular property pattern in descriptive programming
using regular expressions


Regular Expression (RegExp) Object in VBSript:

Now we are going to explore how to use regular expression object "RegExp". "RegExp" object in VBScript provides regular expression support.

Methods of RegExp Object:

1. Execute
2. Replace
3. Test

Properties of RegExp Object:


1. Global
2. IgnoreCase
3. Pattern

Using Methods of RegExp Object:

1. Execute Method:

The Execute method returns a Matches collection containing a Match object for each match found in string. Execute returns an empty Matches collection if no match is found.

Syntax:
RegularExpressionObject.Execute(string) 


Here is an instance explaining Execute Method of RegExp Object:

'########################
'@ Execute Method
'########################

Function funRegularExp (patrn, str)
 
 Dim varRgExp, Match, varFound   
    
  Set varRgExp = New RegExp   
  varRgExp.Pattern = patrn
  varRgExp.IgnoreCase = True        
  varRgExp.Global = True     
      
  Set varFound = varRgExp.Execute(str)

  For Each Match in varFound
    varResult = varResult & "Match found at position "
    varResult = varResult & Match.FirstIndex & ". Match Value is '"
    varResult = varResult & Match.Value & "'." & vbCRLF
  Next

  funRegularExp = varResult

End Function

print (funRegularExp("boy", "Boy1 sitting besides the boy2 has reporting card of BoY3"))


The result of above function call with the print statement comes like this:


2. Replace Method:

Replace method of Regular Expression object is used to find a specific pattern in the given string and getting it replaced with provided value


Syntax:
RegularExpressionObject.Replace(string1, string2) 


Here is an instance to illustrate use of Replace method:


'########################
'@ Replace Method
'########################

Function funReplace (patrn, varRepl)

  Dim regEx, sourceStr              
  sourceStr = "The quick brown fox jumped over the lazy dog."

  Set regEx = New RegExp            
  regEx.Pattern = patrn       
  regEx.IgnoreCase = True

  funReplace = regEx.Replace(sourceStr, varRepl)   

End Function

'@ Replace 'fox' with 'rat'.
print (funReplace("fox", "rat"))


The result of above function call with the print statement comes like this:



3. Test Method:

Test method executes a regular expression search in a given string. It returns a Boolean value to specify that pattern is was found in the given string


Syntax:
RegularExpressionObject.Test(string) 

See below example to understand use of regular expression Test method:


'########################
'@ Test Method
'########################

Function funRegularExp (patrn, str)
 
  Dim varRgExp, isFound   
  
  '@ Create New Regular Expression Object
  Set varRgExp = New RegExp

  '@ Set pattern   
  varRgExp.Pattern = patrn
  varRgExp.IgnoreCase = True        
  varRgExp.Global = True     
  
  isFound = varRgExp.Test(str)
  
  If isFound Then
    print "Pattern: "&patrn&" is found in the given string: "&str
  else
    print "Pattern: "&patrn&" is not found in the given string: "&str
  End If
  
End Function

funRegularExp "boy", "Boy1 sitting besides the boy2 has reporting card of BoY3"


Here is the result of above function call:



Use Of Properties of Regular Expression Object:

1. Global Property:

Global property is used to set or return a Boolean value that indicates if a pattern should match all occurrences in an entire search string or just the first one. 


Syntax:
RegularExpressionObject.Global [= True | False ]

Global Property Values and Description

Default Value: False

2. IgnoreCase Property:

IgnoreCase property is used to set or return a Boolean value that indicates if a pattern search is case-sensitive or not. 


Syntax:
RegularExpressionObject.IgnoreCase [= True | False ]

IgnoreCase Property Values and Description

Default Value: False


3. Pattern Property:

Pattern property is used to set or return the regular expression pattern being searched for.

Syntax:
object.Pattern [= "searchstring"]


Friday, May 27, 2011

How To Use Descriptive Programming In QTP Continued..


When And Where To Use Descriptive Programming:

In this article we will continue to learn "when and where to use Descriptive Programming in QTP?". What are the scenarios where one should use descriptive programming (Programmatic descriptions):
Just to list major scenarios below:


1. When object is not saved in Object Repository:

Descriptive Programming can be very useful if one want to perform an operation on an object that is not stored in the OR (Object Repository).

2. When many objects with same properties available in AUT (Application Under Test):

Descriptive Programming is also used in case where one has to perform the same operation on several objects with certain identical properties.

3. To handle dynamic objects:

It can also be used to perform an operation on an object whose properties match a description that one determines dynamically during the run session.
  
Here is an example to illustrate:
If there are multiple links in a particular section with different names, so it is not recommended to store all links in OR, better is to create properties collection and pass the name as parameter for “innertext”

 


Function clickLink(varLinkName)
 Set objDesc = description.Create()
 objDesc ("micclass").value = "Link"
 objDesc ("html tag").value="A"
 objDesc ("innertext").value = varLinkName
 Set objChild = Browser("Browser").Page("Page").ChildObjects(objDesc)
 objChild(0).Click
End Function

clickLink "Link1"
clickLink "Link2"


How To Use Descriptive Programming (Programmatic Description) In QTP Basics


Types Of Descriptive Programming:

Before starting to play with Descriptive Programming,one needs to understand the types of Descriptive Programming (programmatic descriptions).
Descriptive Programming can be of two types, Static and Dynamic:

1. Static Descriptive Programming:

    Example:
     
    
    
    Browser("Browser").Page("Page").WebEdit("Name:=Employee", "Index:=3").Set "John" 
    
    
    

    2. Dynamic Descriptive Programming: 

     One adds a collection of properties and values to a Description object, and then enters the Description object name in the statement.
    Example:
     
    
    
    Set objDesc = description.Create()
    objDesc ("micclass").value = "Link"
    objDesc ("html tag").value="A"
    objDesc ("innertext").value = "ClickHere"
    Set objChild =Browser("Browser").Page("Page").ChildObjects(objDesc) 
    
    
    

    Thursday, July 29, 2010

    How To Compare Two PDF Files

    Comparing two PDF files using Quick Test Professional is bit tricky. Lets do it. As we know to compare two files first thing one would require is to get the contents of file thus require file handling and then the logic to compare content. It can be done in two steps:

    (1) Opening the pdf files and getting their text content from clipboard

    (2) Comparing the two strings containing text contents.

    Here is an example:

     
    
    
    Dim varPath1, varPath2, str1, str2
    varPath1 = "C:\first.pdf"
    varPath2 = "C:\second.pdf"
    
    str1= getPDFText (varPath1)
    str2= getPDFText (varPath2)
    
    if StrComp(str1,str2,0) <> 0 Then
     msgbox "PDF files do not have same text content"
    else
     msgbox "PDF files have same text content"
    end If
    
    Public Function getPDFText (varPath)
    
     Dim MyClipboard
    
     SystemUtil.Run varPath
     
     Window("regexpwndtitle:=Adobe Reader","regexpwndclass:=AdobeAcrobat").Type micAltDwn + "t" + micAltUp
    
     Window("regexpwndtitle:=Adobe Reader","regexpwndclass:=AdobeAcrobat").Type "s"
    
     Window("regexpwndtitle:=Adobe Reader","regexpwndclass:=AdobeAcrobat").Type micDwn
    
     Window("regexpwndtitle:=Adobe Reader","regexpwndclass:=AdobeAcrobat").Type "Enter"
    
     Window("regexpwndtitle:=Adobe Reader","regexpwndclass:=AdobeAcrobat").Type micCtrlDwn + "a" + micCtrlUp
    
     Window("regexpwndtitle:=Adobe Reader","regexpwndclass:=AdobeAcrobat").Type micCtrlDwn + "c" + micCtrlUp
    
     Set MyClipboard = CreateObject("Mercury.Clipboard")
    
     getPDFText = MyClipboard.GetText
    
     MyClipboard.Clear
    
     Set MyClipboard=Nothing
    
     Window("regexpwndtitle:=Adobe Reader","regexpwndclass:=AdobeAcrobat").Activate
    
     Window("regexpwndtitle:=Adobe Reader","regexpwndclass:=AdobeAcrobat").Close
    
    End Function