No Regular-Expressions for the Desktop Object

Article Tools

Update: Thanks to Peretz, we now know that there is a hotfix that solves the issue (designated QTP_00563), and that the fix has already been incorporated into the upcoming QTP 10 (Atlantis). Thanks a lot for the information and the care!

I have recently spent over a day of frustrating debugging until I’ve finally stumbled upon a QTP bug which caused all my problems; so I thought I’d share it with the QTP community, hoping it would save someone else’s precious time and money.

Our story begins with the .ChildObjects command:

If you work extensively with QTP, you’ve probably came across the .ChildObjects command (if not – you can read a little about it here). This wonderful command allows us to ask a QTP object for all its child-objects that have certain properties. It allows us to write extremely elegant and dynamic scripts, without committing ourselves to working with specific GUI objects.

For example, this will input all the text fields within a given page, without knowing who they are or how many of them exist beforehand:

Dim oDesc
Set oDesc = Description.Create

oDesc("micclass").Value = "WebEdit"

Dim oChildren
Dim i

Set oChildren = Browser("index:=0").Page("index:=0").ChildObjects(oDesc)

For i = 0 to oChildren.Count - 1
    oChildren.Item(i).Set "Demo Text"
Next

This is just one simple example – in real life .ChildObjects is even more amazing and useful.

The .ChildObjects command can be combined with the Desktop object, to run through all the top-level windows and applications. For example, this will highlight every opened Notepad window.

Dim oDesc
Set oDesc = Description.Create

oDesc("nativeclass").Value = "Notepad"

Dim oChildren
Dim i

Set oChildren =Desktop.ChildObjects(oDesc)

For i = 0 to oChildren.Count - 1
    oChildren.HighLight
Next


Well, you get the trick.

Now to the second part of our story:

One of the amazing features of QTP, is the ability to describe objects with Regular Expressions. For example, you’d like to describe both Notepad and Wordpad windows, you could’ve used the following line in the above script:

oDesc("nativeclass").Value = "Notepad|WordPadClass"

The “|” sign is a regular expression switch that tell QTP that both “Notepad” and “WordPadClass” should be considered as successful “hits”. Regular Expressions allow for wonderful, flexible object identification, and have rescued me from the pit of despair on several occasions (you can read more on Regular Expressions in QTP in Dani’s book – Scripting QTP ).

The bug I’ve found is that when retrieving the Desktop’s child-objects, regular expressions do not work! This means that the above example would’ve yielded zero results – missing both Notepad and Wordpad windows. This is true for all regular expressions, even the simpler .* ones.

This drove me crazy, as regular expressions work well with the .ChildObjects command on every other object. As long as it’s not the Desktop object, everything’s OK. Once you use it on the Desktop – no regular expression works. Trying to manually set the .RegularExpression property of the description to true doesn’t work either, and in any case, the same description works fine when its used in a .ChildObjects command on any other object.

This bug forces me to do horrible things. If we use the Wordpad/Notepad example, it forces us to duplicate our code (once for each NativeClass); on other occasions it does even more damage. Perhaps the things which angers me the most is that it’s clearly a bug, some silly overlook or omission. It’s far too arbitrary to be anything else.

At least now that you know about it, you won’t have to waste hours of hard work figuring out where the problem is…

Previous postImplementing a Generic Iterator with Function Pointers Next postUsing Runtime Attributes to Describe QTP Web Objects

Related Posts

Post Your Comment

You must be logged in to post a comment.