Blog to understand automation concepts in QTP, Selenium Webdriver and Manual Testing concepts
How to use Regular Expressions in QTP
Regular expressions
are used to identify objects and text strings with varying values. Regular
Expression are strings that defines search phrase based on special character
provided in the expression.This is useful when expected value of any object
property is regularly changing but in a fix pattern.
Some useful points to remember about Regular Expressions are:
- Regular Expression is useful for following scenarios in QTP:
- Defining the property values of an object in dialog boxes or in programmatic descriptions
- Defining expected values for checkpoints
- Defining pop-up window conditions in a recovery scenario.
- Can create regular expression for strings only.
- Period (.), hyphen (-), asterisk (*), caret (^), brackets ([ ]),parentheses (()), dollar sign ($), vertical line (|), plus sign (+), question mark (?), and backslash (\) are special characters used to create regular expression.
- When one of above mentioned special characters is preceded by a backslash (\), QTP treats it as a literal character.
- By default, the value of all Property objects added to a Properties collection are treated as regular expressions.
Below are the various regular expressions used in QTP.
- Matching Any Single Character (.) eg abc. Will match abc followed by
any character.
- Matching Any Single Character in a List ( [xy] ) e.g [ab] will match
either a or b
- Matching Any Single Character Not in a List ( [^xy] ) e.g 1[^23] will
match all values between 11 to 19 except 12 and 13.
- Matching Any Single Character within a Range ( [x-y] ) e.g : 1[1-3] will
match 11,12, and 13.
- Matching Any AlphaNumeric Character Including the Underscore ( \w )
- Matching Any Non-AlphaNumeric Character (\W) will match any special
character other than underscore. Please note case of W in this case.
- Matching Zero or More Specific Characters ( * ) This matches zero or more occurrences of
the preceding character. e.g ca* will match caa,caaaa,c and so on. Similarly
c.* will match c, cs,caaa, and so on, since preceding character here is “.”.
- Matching One or More Specific Characters ( + ) Only difference from *
is it will match for minimum one character. e.g ta+r will match taar,tar but
not tr as in above case.
- Matching Zero or One Specific Character ( ? ) A question mark (?) instructs QTP to match zero or one occurrences
of the preceding character. For example: te?r matches ter and tr, but nothing
else
- Matching One of Several Regular Expressions ( | ) e.g new|day will
match either of new or day. If we write ne(w|d)ay, it will match neway or
neday.
- Matching the Beginning of a Line ( ^ ) This will match only if match is
found at beginning of line.
- Matching the End of a Line ( $ ) This will match only if match is found
at end of line.
- Matching a word at boundary(\b) e.g new\b will match testnew but
not in knewit.
- Matches a digit character(\d) Matches a digit value.
- Matching a non-digit character(\D) Matches a non digit value
For understanding of regexp object for regular expression, Click hereObject Repositories in QTP:Understanding Types and Collection Object.
Labels:
Advanced QTP,
Object Identification,
QTP,
QTP AOM
Object
Repository stores all the objects learned in the application. An Object
repository is automatically created when we record in the application. Object can
also be added in object repository through object spy and Navigate and Learn
objects.
Following are important points to remember about object repositories:
- Objects are stored in tree structure in OR with child objects linked to parent objects.
- Objects in Local OR and shared OR can be accessed/modified through Object Repository window and Object repository manager window respectively.
- Objects in OR can be added through:
o
Recording mode
o
Active screen
o
Using Object Spy
o
Using Navigate and Learn through
Object repository window.
- Objects can be added with expected properties defined manually in OR. This is useful in case application is not ready, but we know the expected property for objects.
- Copying or moving an object to be a child of a bottom-level object in the object hierarchy is not allowed.
- Property value and name of object in object repository can be modified.
- Modifying the name of a test object in the local object repository is automatically updated in both the Keyword View and the Expert View for all occurrences of the test object.
- We can highlight objects from OR in application.
- Object from object repositories can be dragged and drop in the expert view. Hierarchy Tree of the object is visible in expert view on dragging object from OR.
There are two types of Object Repositories:
1. Local Repository
Objects are automatically added in Local Object Repository
once they are learned from the application.
Following are important points to remember about Local Object Repository:
- If an object with the same name is located in both the local object repository and in a shared object repository associated with the same action, the action uses the local object definition.
- Local Object Repository is associated with the action in which it was created. You cannot associate Local object repository with multiple actions. Due to this, they are useful mainly in single action.
- If child objects are moved from shared OR to local OR, parent objects are also added to Local OR.
2. Shared Object Repository
A shared object repository stores objects in a file that can be accessed by multiple tests (in read-only mode).
Following are important points to remember about Local Object Repository:
- Same Shared Object repository can be added for multiple actions.
- Shared OR can be dynamically associated with tests and multiple actions.
- Shared OS is saved with extension .tsr
- Objects from local OR can be exported to shared OR.
- An Action can have multiple Shared object repository associated.
- A shared repository can be edited by one person at a time and is locked for other user
- Using OR comparison tool, we can compare the objects in 2 different shared OR
- Using Object repositories Merge tool, we can merge two shared OR and create a new OR with objects from both the OR’s.
Associating Repositories with Action
We can associate object repositories with action in following ways:
Through QTP Interface
Navigate to Resources>Associate Repositories. Click on + icon and select repositories. Note we can add shared object repository only. Next Select Actions from available Actions and move required actions to Associated Actions.
Using Object Repositories collection
We can add object repositories using ObjectRepositories Collection
Below code snippet shows how to add ObjectRepositories using QTP AOM
Public Function func_AddORToTest(TestName, ActionName, ORName)
''Create an object of qtp
Set qtApp = CreateObject("QuickTest.Application")
qtApp.Launch
qtApp.Visible = True
qtApp.Open TestName
Set qtRepositories = qtApp.Test.Actions(ActionName).ObjectRepositories
' Add MainApp.tsr if it's not already in the collection
If qtRepositories.Find(ORName) = -1 Then ' If the repository cannot be found in the collection
qtRepositories.Add ORName, 1 ' Add the repository to the collection
End If
'Save the test and close QuickTest
qtApp.Test.Save
qtApp.Quit
Set qtRepositories = Nothing
Set qtApp = Nothing
End Function
Subscribe to:
Posts (Atom)