Relevant Codes (by Anshoo Arora)

A Test Development Resource for HP QuickTest Professional.

Regular Expressions and Select Method (ListBox)

by Anshoo Arora on April 19, 2010

This post describes a few ways to use Regular Expressions to select the target item from List objects. All examples of this post demonstrate techniques to be used in Web Applications, but they can be easily extended to technologies that store all list items with delimiters.

Regex Select Using QTP Methods

The below method uses QTP’s technique of retrieving the all items property from the WebList. It also uses RegExp object, which means, regular expressions will be supported. If the pattern does not match the pattern or the exact string from the List, the first item will be selected by default. If the default selection does not fit your requirement, please feel free to modify the code as per your needs.

Function RegexSelectQTP(Object, sPattern)
	Dim oRegExp, arrAllItems, ix
 
	'Create RegExp Object
	Set oRegExp = New RegExp
	oRegExp.IgnoreCase = False
	oRegExp.Pattern = sPattern
 
	'Split Object's all_items property
	arrAllItems = Split(Object.GetROProperty("all items"), ";")
	For ix = LBound(arrAllItems) To UBound(arrAllItems)
		'If RegExp pattern matches list item, we're done!
		If oRegExp.Test(arrAllItems(ix)) Then
			Object.Select "#" & ix
			Set oRegExp = Nothing
			Exit Function
		End If
	Next
 
	'Select Item #1 by default
	Object.Select "#0"
End Function
RegisterUserFunc "WebList", "RegexSelectQTP", "RegexSelectQTP"
Usage:
Browser("").Page("").WebList("").RegexSelectQTP "London - Heathrow" 'Select London-Heathrow
Browser("").Page("").WebList("").RegexSelectQTP "London - Heath.*"  'Select London-Heathrow
Browser("").Page("").WebList("").RegexSelectQTP "London - \D+"      'Select London-Heathrow

Regex Select Using DOM Methods

This is quite similar to above, but instead of using the all items property, it uses the HTML DOM options. In general, the HTML of a WebList is like this:

<select>
	<option>Acapulco</option>
	<option>London</option>
	<option>New York</option>
	<option>San Francisco</option>
</select>

The code below retrieves all the items within the option /option tag and loops through each to determine if there is a regexp match. Also, like the above method, if a match is not found, the first item in the list will be selected by default. Note: This method will not work for technologies other than Web.

Function RegexSelectDOM(Object, sPattern)
	Dim oRegExp, oOptions, ix
 
	'Create RegExp Object
	Set oRegExp = New RegExp
	oRegExp.IgnoreCase = False
	oRegExp.Pattern = sPattern
 
	'DOM options
	Set oOptions = Object.Object.Options
	For ix = 0 to oOptions.Length - 1
		'If RegExp pattern matches list item, we're done!
		If oRegExp.Test(oOptions(ix).Text) Then
			Object.Select "#" &amp; ix
			Set oRegExp = Nothing
			Exit Function
		End If
	Next
 
	'Select Item #1 by default
	Object.Select "#0"
End Function
RegisterUserFunc "WebList", "RegexSelectDOM", "RegexSelectDOM"
Usage:
Browser("").Page("").WebList("").RegexSelectDOM "London - Heathrow" 'Select London-Heathrow
Browser("").Page("").WebList("").RegexSelectDOM "London - Heath.*"  'Select London-Heathrow
Browser("").Page("").WebList("").RegexSelectDOM "London - \D+"      'Select London-Heathrow

Select Using VBScript (InStr/Mid) Methods

This method does not support Regular Expressions as it is based on VBScript’s InStr and Mid methods. Instead, it parses the correct value depending upon the supplied complete or partial values without the regexp meta characters.

Public Function VBSSelect(Object, sString)
	Dim sAllItems, varLocation, varEnd, varBeginning
 
	'Retrieve Object's all_items property
	sAllItems = Object.GetROProperty("all items")
 
	'Verify if the supplied string is found in list's all_items property
	varLocation = InStr(1, sAllItems, sString)
	'If found:
	If varLocation &gt; 0 Then
        varEnd = InStr(varLocation, sAllItems, ";")
		If varEnd = 0 Then varEnd = Len(sAllItems) + 1
		varBeginning = InStrRev(sAllItems, ";", varLocation)
		Object.Select "" &amp; Mid(sAllItems, varBeginning + 1, varEnd - varBeginning - 1)
		Exit Function
	End If
 
	'Select Item #1 by default
	Object.Select "#0"
End Function
RegisterUserFunc "WebList", "VBSSelect", "VBSSelect"
Usage:
Browser("").Page("").WebList("").VBSSelect "London - Heathrow" 'Select London-Heathrow
Browser("").Page("").WebList("").VBSSelect "London - Heath"    'Select London-Heathrow
Browser("").Page("").WebList("").VBSSelect "London - "         'Select London-Heathrow

Performance Comparison

The above 3 methods were tested on QTP 10 against a Web Application with 500 iterations each on a Dell XPS M1210 Core2Duo 2.0GHz 3GB Laptop. The table below presents the average time taken by each method to successfully select a value from the WebList in QTP’s Normal and Fast run modes:

Performance Comparison between Select Methods
Run Mode Normal Fast
RegexSelectQTP 0.44 seconds 0.38 seconds
RegexSelectDOM 0.45 seconds 0.40 seconds
VBSSelect 0.39 seconds 0.35 seconds

If you have written a custom method that you would like to share, please feel free to do so and help the community! :)

If you have any questions, please ask them in the comments section. If your query is confidential, please use the Contact Form to send me an e-mail instead.

{ 26 comments… read them below or add one }

1 steven April 22, 2010 at 3:59 am

Can we also use the DOM operation? I mean using getElementByTag or Id then loop all the child nood, such as nextSibiling.Text to see whether the option we want to select match the text we get.

Reply

2 Anshoo Arora April 25, 2010 at 3:14 pm

Hi Steven,

Approach #2 uses DOM. We can certainly use getElementById or getElementsByTagName but, that will only retrieve the List object – not the list items. The list items are retrieved as shown in the second approach through list options.

3 deepak May 3, 2010 at 12:06 am

Anshoo,

can you write an article on understanding DOM ? do let us know if you have any other site to reference for

thx!

Reply

4 Anshoo Arora May 5, 2010 at 2:26 am

Deepak: I’ll see what I can do :)

5 Harish May 5, 2010 at 9:16 am

Here is one more custom method,

Function RegexSelect(TestObject, sPattern)
	For i = 0 to (TestObject.GetROProperty("items count")-1)
		TestObject.Select "#" & i
		If TestObject.CheckProperty("value", micRegExpMatch(sPattern),0) Then
			Exit For
		End If
	Next
End Function

Reply

6 Anshoo Arora May 6, 2010 at 2:07 pm

I will run the same performance tests on this method, and include it in this post over the weekend giving full credit to you.

Thanks for sharing, Harish! :)

7 Anu May 18, 2010 at 7:39 am

Hi Anshoo,

I want to use regular expresion for the link because on click of every link,it navigates to new web page.
The code is written below:
Browser(“Browser”).Page(“Page”).Link(“Next”).Click

Here ‘Next’ is present on every web page and instead of using Data Table, i want to use regular expression in the Object Repository.
Please tell me which syntax of the regular expression i used in this.

Thanks in advance…….

Regards,
Anu

Reply

8 Vishal Jhaveri May 26, 2010 at 1:41 am

Hi Anshoo,
What will be the regular expression for $762…/Annually where ….represents n numbers
Actually these values are picked up from a database and get populated in a weblist. So everytime this value keeps on changing depending on the option that I select. Hence i want to use regular expression for this.
Please reply as soon as possible.

Thanks,
Vishal

Reply

9 steven May 26, 2010 at 5:16 am

^\$\d{1,}\/Annually$

10 durai June 10, 2010 at 8:49 am

ok

Reply

11 Neelam June 22, 2010 at 11:02 pm

how to capture the dropdown menu in QTP?

Reply

12 Anshoo Arora June 28, 2010 at 10:13 am

Hi Neelam,

It depends.. What is the technology of your application (Web, .Net, Java etc)?

13 Gayathri June 23, 2010 at 12:45 am

Hi Anshoo,

how to write a custom method to retrieve a substring from the mainstring.

not using the built in functions in QTP.

Thanks in Advance for your help.

Gayathri

Reply

14 Anshoo Arora June 28, 2010 at 10:29 am

Not sure if I understand your question. Is there a set sub-string or a pattern that you’re trying to retrieve each time? If its a pattern, then we will have to use RegEx. If its a sub-string then there are several ways we can do it using Left, Right, InStr, InStrRev and RegEx as well.

15 chetan July 9, 2010 at 4:17 am

Hi,

Can you tell me regular expression for title with different prefix n suffix
e.g abc mytitle xyz , pqr mytitle abc in this mytitle is constant
i am using .*mytitle.* but it is not working

Reply

16 Anshoo Arora July 14, 2010 at 2:33 pm

Hi Chetan,

Can you please post the entire DP statement that you’ve used?

17 Ichan July 16, 2010 at 10:29 am

How you do reg exp on Total: 2 this number is dynamic after Total:

And another is 10 digits number, and this number also dynamic. atleast 1.

Reply

18 Anshoo Arora July 30, 2010 at 8:41 am

Ichan,

You can use one of the following:

Total: [0-9][0-9]
Total: \d+

19 Prakash March 14, 2011 at 2:17 am

Hi Anshoo,

I want to use regular expresion for the link because on click of every link,it navigates to new web page.
The code is written below:
Browser(“Browser”).Page(“Page”).Link(“Next”).Click

Here ‘Next’ is present on every web page and instead of using Data Table, i want to use regular expression in the Object Repository.
Please tell me which syntax of the regular expression i used in this.

Regards,
Prakash

Reply

20 John Dwyer May 12, 2011 at 5:53 am

Hi Anshoo,
Thanks so much for posting the RegexSelectDOM function. It was a total life-saver! I’m using QTP to automate multiple languages and so being able to select drop-down values by their value and no text helped me greatly.
Is it possible to modify the function to also examine radio buttons, text boxes, etc.?
Great site.
John

Reply

21 Anshoo Arora May 19, 2011 at 7:30 pm

@John: Thank you! Its certainly possible for radio buttons, but not for TextBox as we can only set a value; no select there. I currently do not have QTP on this machine, but I suspect one of these functions in this article may work with radioButtons as-is. I may be wrong, but I can verify this for you.

22 John Dwyer May 20, 2011 at 3:35 am

Anshoo,
You’re right, the QTP method works like a charm! Thanks very much.
John

Reply

23 Neha July 29, 2011 at 12:06 am

I have tried with first method . and it gives error : error in function library for line :
Object.Select “#” & ix
Then I tried with :
Object.Select “#” &ix
and it gives run error:
Cannot identify the specified item of the Mechanics object. Confirm that the specified item is included in the object’s item collection.
My application is a windows based application and the direct select on WinList doesn’t work . I need to select an item from winlist on left side and then using > button need to move it to right side winlist. pls suggest.

Reply

24 Anshoo Arora August 1, 2011 at 4:23 pm

Neha,

For Windows, you will have to do 2 minor changes.

1. Change “#” & ix to simply ix
2. Change the delimiter from ; to vbLf

Function RegexSelectQTP(Object, sPattern)
	Dim oRegExp, arrAllItems, ix

	'Create RegExp Object
	Set oRegExp = New RegExp
	oRegExp.IgnoreCase = False
	oRegExp.Pattern = sPattern

	'Split Object's all_items property
	arrAllItems = Split(Object.GetROProperty("all items"), vbLf)
	For ix = LBound(arrAllItems) To UBound(arrAllItems)
		Print arrAllItems(ix)
		'If RegExp pattern matches list item, we're done!
		If oRegExp.Test(arrAllItems(ix)) Then
			Object.Select ix
			Set oRegExp = Nothing
			Exit Function
		End If
	Next

	'Select Item #1 by default
	Object.Select 0
End Function

25 steven April 25, 2010 at 8:34 pm

Yes, i know what exactly you means. But after we get the object then we can loop all the child node of the object using method perviousSibling or nextSibling in javascript, thenwe get the option object of the list then we can get the text of the option object.

Any way it may a more complex approach, but I am just trying to think of the way we can use. Probably not suitable.

Reply

26 Harish May 6, 2010 at 11:09 pm

Thanks for all your efforts.

Reply

Leave a Comment

Previous post:

Next post: