LayoutTitle

Hoverable Dropdown

Monday, February 25, 2019

Regular Expressions or Wild Cards

Regular expressions were used when there will be a dynamic text appears to be validated. To perform validations on dynamic texts you have to use the wild cards. Namely, you have few in UFT they will be donated by some special charters, those are *, /, |, ? etc.,

List of Wild Cards

Symbol Description
\Marks the next character as a special character, a literal, a backreference, or an octal escape. For example, 'n' matches the character "n". '\n' matches a newline character. The sequence '\\' matches "\" and "\(" matches "(".
^Matches the position at the beginning of the input string. If the RegExp object's Multiline property is set, ^ also matches the position following '\n' or '\r'.
$Matches the position at the end of the input string. If the RegExp object's Multiline property is set, $ also matches the position preceding '\n' or '\r'.
*Matches the preceding character or subexpression zero or more times. For example, zo* matches "z" and "zoo". * is equivalent to {0,}.
+Matches the preceding character or subexpression one or more times. For example, 'zo+' matches "zo" and "zoo", but not "z". + is equivalent to {1,}.
?Matches the preceding character or subexpression zero or one time. For example, "do(es)?" matches the "do" in "do" or "does". ? is equivalent to {0,1}
{n}n is a nonnegative integer. Matches exactly n times. For example, 'o{2}' does not match the 'o' in "Bob," but matches the two o's in "food".
{n,}n is a nonnegative integer. Matches at least n times. For example, 'o{2,}' does not match the "o" in "Bob" and matches all the o's in "foooood". 'o{1,}' is equivalent to 'o+'. 'o{0,}' is equivalent to 'o*'.
{n,m}m and n are nonnegative integers, where n <= m. Matches at least n and at most m times. For example, "o{1,3}" matches the first three o's in "fooooood". 'o{0,1}' is equivalent to 'o?'. Note that you cannot put a space between the comma and the numbers.
?When this character immediately follows any of the other quantifiers (*, +, ?, {n}, {n,}, {n,m}), the matching pattern is non-greedy. A non-greedy pattern matches as little of the searched string as possible, whereas the default greedy pattern matches as much of the searched string as possible. For example, in the string "oooo", 'o+?' matches a single "o", while 'o+' matches all 'o's.
.Matches any single character except "\n". To match any character including the '\n', use a pattern such as '[\s\S].
(pattern)Matches pattern and captures the match. The captured match can be retrieved from the resulting Matches collection, using the SubMatches collection in VBScript or the $0$9 properties in JScript. To match parentheses characters ( ), use '\(' or '\)'.
(?:pattern)Matches pattern but does not capture the match, that is, it is a non-capturing match that is not stored for possible later use. This is useful for combining parts of a pattern with the "or" character (|). For example, 'industr(?:y|ies) is a more economical expression than 'industry|industries'.
(?=pattern)Positive lookahead matches the search string at any point where a string matching pattern begins. This is a non-capturing match, that is, the match is not captured for possible later use. For example 'Windows (?=95|98|NT|2000)' matches "Windows" in "Windows 2000" but not "Windows" in "Windows 3.1". Lookaheads do not consume characters, that is, after a match occurs, the search for the next match begins immediately following the last match, not after the characters that comprised the lookahead.
(?!pattern)Negative lookahead matches the search string at any point where a string not matching pattern begins. This is a non-capturing match, that is, the match is not captured for possible later use. For example 'Windows (?!95|98|NT|2000)' matches "Windows" in "Windows 3.1" but does not match "Windows" in "Windows 2000". Lookaheads do not consume characters, that is, after a match occurs, the search for the next match begins immediately following the last match, not after the characters that comprised the lookahead.
x|yMatches either x or y. For example, 'z|food' matches "z" or "food". '(z|f)ood' matches "zood" or "food".
[xyz]A character set. Matches any one of the enclosed characters. For example, '[abc]' matches the 'a' in "plain".
[^xyz]A negative character set. Matches any character not enclosed. For example, '[^abc]' matches the 'p' in "plain".
[a-z]A range of characters. Matches any character in the specified range. For example, '[a-z]' matches any lowercase alphabetic character in the range 'a' through 'z'.
[^a-z]A negative range characters. Matches any character not in the specified range. For example, '[^a-z]' matches any character not in the range 'a' through 'z'.
\bMatches a word boundary, that is, the position between a word and a space. For example, 'er\b' matches the 'er' in "never" but not the 'er' in "verb".
\BMatches a nonword boundary. 'er\B' matches the 'er' in "verb" but not the 'er' in "never".
\cxMatches the control character indicated by x. For example, \cM matches a Control-M or carriage return character. The value of x must be in the range of A-Z or a-z. If not, c is assumed to be a literal 'c' character.
\dMatches a digit character. Equivalent to [0-9].
\DMatches a nondigit character. Equivalent to [^0-9].
\fMatches a form-feed character. Equivalent to \x0c and \cL.
\nMatches a newline character. Equivalent to \x0a and \cJ.
\rMatches a carriage return character. Equivalent to \x0d and \cM.
\sMatches any white space character including space, tab, form-feed, and so on. Equivalent to [ \f\n\r\t\v].
\SMatches any non-white space character. Equivalent to [^ \f\n\r\t\v].
\tMatches a tab character. Equivalent to \x09 and \cI.
\vMatches a vertical tab character. Equivalent to \x0b and \cK.
\wMatches any word character including underscore. Equivalent to '[A-Za-z0-9_]'.
\WMatches any nonword character. Equivalent to '[^A-Za-z0-9_]'.
\xnMatches n, where n is a hexadecimal escape value. Hexadecimal escape values must be exactly two digits long. For example, '\x41' matches "A". '\x041' is equivalent to '\x04' & "1". Allows ASCII codes to be used in regular expressions.
\numMatches num, where num is a positive integer. A reference back to captured matches. For example, '(.)\1' matches two consecutive identical characters.
\nIdentifies either an octal escape value or a backreference. If \n is preceded by at least n captured subexpressions, n is a backreference. Otherwise, n is an octal escape value if n is an octal digit (0-7).
\nmIdentifies either an octal escape value or a backreference. If \nm is preceded by at least nm captured subexpressions, nm is a backreference. If \nm is preceded by at least n captures, n is a backreference followed by literal m. If neither of the preceding conditions exist, \nm matches octal escape value nm when and m are octal digits (0-7).
\nmlMatches octal escape value nml when n is an octal digit (0-3) and m and l are octal digits (0-7).
\unMatches n, where n is a Unicode character expressed as four hexadecimal digits. For example, \u00A9 matches the copyright symbol (©).

These Wild Cards helps you to write the automation code or helps you in solving the object  property issues or majority in order to identify the objects that has impacted due to the dynamic text changes, will use these wild cards either in Object Repository (OR) or in the automation code. These approaches are given below.

Approach 1 - Using Regular Expressions at Automation code level

As said, earlier one can automate using only Regular Expressions without OR. Here is the example how the code will be seen without OR.

Example using Object Repository (OR) will look like as shown below

Browser("Google Home").Page("Search Page").WebEdit("Search").Set "Vasu Chiluveru"


Browser Object and properties

Page Object and properties

Edit Object and properties


Example using Wild Cards or Regular Expressions will look like as shown below and shown varieties of its usage as well

1. With Single parameter 
Browser("title:=Google - Internet Explorer").Page("title:=Google").WebEdit("name:=q").Set "Vasu Chiluveru"

2. With multiple parameters along without Wild Card usage
Browser("title:=Google - Internet Explorer").Page("title:=Google").WebEdit("name:=q","html tag=INPUT").Set "Vasu Chiluveru"

3. With multiple parameters along with Wild Card usage
Browser("title:=Google *").Page("title:=Google").WebEdit("name:=q","html tag=INPUT").Set "Vasu Chiluveru"

Approach 2 - Using regExp keyword in the automation code

Another approach of using Regular expressions using regEx as shown below

Set regEx = New RegExp

Example: Validating the actual pattern with the given String

Function Code
Function RegExpTest(patrn, strng)
   Dim regEx, Match, Matches   ' Create variable.
   Set regEx = New RegExp   ' Create a regular expression.
   regEx.Pattern = patrn   ' Set pattern.
   regEx.IgnoreCase = True   ' Set case insensitivity.
   regEx.Global = True   ' Set global applicability.
   Set Matches = regEx.Execute(strng)   ' Execute search.
   For Each Match in Matches   ' Iterate Matches collection.
      RetStr = RetStr & "Match found at position "
      RetStr = RetStr & Match.FirstIndex & ". Match Value is '"
      RetStr = RetStr & Match.Value & "'." & vbCRLF
   Next
   RegExpTest = RetStr
End Function

Main Code
MsgBox(RegExpTest("is.", "IS1 is2 IS3 is4"))


Approach 3 - Using the wild cards in Object Repository

Below used asterisk (star * symbol) if you wanted this object has to recognize even if you run on Internet Explore (IE) /Chrome/Firefox. If the Object Value is as "Google - Internet Explorer" then the Browser object is recognized on IE browser only.






1 comment: