Regular Expressions in UFT

5 11 2018

In 2013 I wrote a technical blog post for work. Since then the company was purchased by Micro Focus and the post has disappeared (probably due to branding issues). I’m re-posting it here (with minor changes) for future reference. Opinions expressed here are my own and do not represent those of Micro Focus or its entities.

Regular expressions (regex) are a common feature in many programming languages and utilities, UFT also uses allows the use of regex for object identification but there are a couple of small twists that one should keep in mind.

Motivation

Why use regexs in the first place?

Regular expressions are useful an object’s property has a consistent pattern but the details may change over time. For example if you wanted to identify the price part of a product on your shopping site you would be ill-advised to use the actual price in the description.

repository-with-literals

One would prefer to have a description that won’t break the next time this item goes on sale

repository-with-regex

Tools

Some people, when confronted with a problem, think “I know, I’ll use regular expressions.” Now they have two problems.
– Jamie Zawinski

The problem with regexs is that they quickly descend into un-maintainability, while it can be pretty straightforward to write a regex it’s often very hard to read and understand an existing regex.

In order to help author and understand regexs UFT supplies the Regular Expression Evaluator (Tools -> Regular Expression Evaluator…)

evaluator

Which is also available from within the object repository’s value configuration editor (along with a regex cheat sheet)

cheat

There are many other tools for dealing with regexs, one of my favorites recently has been regex101.com which also breaks down and explains the regex it’s evaluating.

How UFT is different

When using a regex in a description, UFT does have a few differences from other tools

Case insensitive

UFT tests are based on VBScript which is a case insensitive language.

Dim Index
For index = 1 To 10
    Print INDEX
Next

People using a case insensitive language to write tests naturally expect all aspects of the test to be case insensitive. This is the reason that (in most) places UFT adds the case insensitive flag to regexs that are part of an object’s description.

Anchoring

As we saw in the Regular Expression Evaluator screenshot, a regex can match a substring in the middle of a longer string. This can cause problems in object identification since the text of a containing object will naturally contain the text of a contained object. For this reason regexs in description are anchored to the beginning and end. This is the equivalent of adding ^ at the beginning of the regex and $ at the end.

This means that “\$99\.” will not match “$99.99, if you want to ignore the cents you will have to add a wildcard “\$99\.\d*”.

Descriptive Programming

When using descriptive programming all values are regular expressions by default. The reasoning behind this is that the fact that you’re using descriptive programming probably indicates that the regular description as seen in the object repository is not working well for you in this situation, therefore the descriptions of your objects are probably dynamic and regular expressions will serve you better than string literals.

There are two ways to write descriptive programming:

Inline descriptive programming

Inline descriptive programming is when the property values are written as part of the test line that identifies the object

Browser("title:=HP.*").Refresh

In this case the property values are always regular expressions and there is no way to tell UFT to try and find a literal match

Using the Description object

One case use the Description object in order to specify an object’s description.

Set desc = Description.Create()
desc("title").Value = "HP.*"
Browser(desc).Refresh

In this case one can turn the regular expression functionality off by setting the property’s RegularExpression property to false.

meme

desc("title").Value = "This is a *plain* [old] string (not some fancy regExp?)."
desc("title").RegularExpression = False

Note that this allows you to use some values as regular expressions and some as literals.

Wait a minute…

Now if you’re a Smart Aleck (like I am) you may be thinking “wait a minute, in a previous post you described an optimization UFT uses for html tags and advised adding them to descriptive programming, wouldn’t a regex html tag nullify the html tag optimization?”

Explanation for non-Smart Alecks, if UFT tried to get a collection of elements from the browser using a regular expression value, it would not work since the browser API expects to get a plain string.

document.getElementsByTagName("T.") // does not return all 
                                    // TR, TD and TH elements

Since descriptive programming uses regular expression by default one might assume that this optimization is not available for descriptive programming users, however we realized this was the case and we check if the value of the html tag contains any regular expression characters, if it does not we treat it as a normal value and not as a regular expression.

In Conclusion

We saw how regular expressions in UFT differ from how they are used in other environments, these differences are:

  • Regular expressions are case insensitive
  • UFT expects to match the regular expression against the whole string and not just a substring

We also saw when and how the regular expressiveness of a value can be turned off.

Advertisements

Actions

Information

One response

8 11 2018
Regexs in UFT | I will not buy this blog, it is scratched!

[…] Last month I talked about a blog post I wrote for work. I didn’t mention that there was some stuff that was dropped during editing. Some of the discarded material was salvaged and expanded into a new post regarding the use of regular expressions in UFT. […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s




%d bloggers like this: