When to use Regular Expressions in QTP - QTP

If the expected value of a property is dynamic, but follows a pattern then regular expressions are most often the best solution. For example, values NameID_233, NameID_256, NameID_290 etc., follows a pattern where the value always starts with the string literal, "NamelD ", and is always followed by a number. This pattern can be represented using a regular expression "NameID_.*". where "." and "*" are meta characters with special meanings as shown in Table . The "." characters specified here mean any character and the "*" character after the "." means zero or more occurrences of pattern matching ".".

Regular expressions can also be used to identify QTP Test Objects whose properties are dynamic or when there are many objects with similar identifying patterns. Let say a webpage contains text boxes where all their names start with the string literal "Name" and the actual name is a pattern like "Namel", "Name2", "Name3", etc.. To access these text box objects we can use Descriptive Programming (DP) and one of the following methods.

Method 1

'Create a description object for text box Set oTextBox = Description.Create
'The regular expression to match a value starting Name oTextBox("naBe").value = "Name.•"
'This property by default is True, so we could skip this line of code oTextBox("rta«e") .RegularExpression = True
'To access the first text box whose name start with Name oTextBox("index").value = 0
'Set the value of the text box
Browser (" ").Page (" ").WebEdit (oTextBox). Set" "Value"

Method 2

'Set the value of first text box whose name start with

"Name" Browser("").Page("").WebEdit("name: =Name.&", "indexs =0").Set "Value""

In this method we can also avoid using the regular expression by generating the name at runtime as given here.

'The text box to be set i = 1
'Set the text box value Browser("").Page("").WebEdit("name!-Name" & i).Set "Value"

If our object is in the repository then we need to change the value using the Object Repository .Manager. Using that tool, open the object repository and browse to the object as shown in the F igure. Click on the "Constant value options" button. Now check the checkbox for "Regular E xpression" and enter the appropriate text in its input field as shown in the Figure

When to use Regular Expressions in QTP

Object Repository.

Object Repository.

Regular Expression checkbox.

Regular Expression Escape Charecterstics.

Regular Expression Escape Charecterstics.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

QTP Topics