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 "#" & 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 > 0 Then varEnd = InStr(varLocation, sAllItems, ";") If varEnd = 0 Then varEnd = Len(sAllItems) + 1 varBeginning = InStrRev(sAllItems, ";", varLocation) Object.Select "" & 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:
| 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! :)
{ 31 comments… read them below or add one }
Hi Anshoo,
I tried out the 2nd method ( using RegExp ) to retrieve a list of all tag in a weblist.
My problem is that even though in the page I see all the options for the weblist, the only one that is retrieved is the one selected in the web app.
QTP also recognises the weblist to have only one item and ignores the rest of the options.
Sample code : (from DOM)
Drilldown
Drilldown Details
DrilldownBook
DrilldownBookDly
DrilldownBookMtd
Selection in the App is DrilldownBookDly and that is all I am able to retrieve. Looks like QTP totally overlooks the other option tags .
Please advice.
Grace, yours seems like an app which is using a 3rd party toolkit such as Dojo. It can be a little hard to provide a solution without looking at the list. Can you perform a click event and if that opens the list, retrieving all items from the opened WebElement?
Thanks Anshoo.
Yes that’s what I suspected though I guessed it to be AJAX or GWT.
I am able to click on the WebList and make the list items visible.But when I try selecting an item, I get the “item not found” error.
From the way the WebList opens up,it looks like it is getting populated after it is clicked (the list first opens downwards displaying the already chosen option , then springs up displaying the other options in the list)
At my wit’s end now.
Do suggest anything that strikes you and can be tried out.
Thanks!
Grace, I have worked with several custom toolkits that can create this behavior. What I do is shown below, but I can’t say 100% if this concept will translate for your app.
'Clicking the WebList will open and make the items visible: Browser("").WebList("").Click 'Instead of "selecting", click the item that you want to select 'From my experience, this WebElement is generally inside a table Browser("").WebTable("").WebElement("").ClickThanks Anshoo, that looked like a great idea but it did not work here,maybe because the Webtable does not show the other options as its members/items except for the current selected item.
I used Descriptive programming to retrieve all the Web Element child objects of the table too,with the same result.
Interestingly though,the outerhtml of the table lists all the options in the table and also gives the contentIndex of the option correctly.If there are 8 options,it is able to say that the current one has index 4. So this is not totally blind to the options.
Any great ideas are highly appreciated.
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.
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 FunctionAnshoo,
You’re right, the QTP method works like a charm! Thanks very much.
John
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
@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.
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
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.
Ichan,
You can use one of the following:
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
Hi Chetan,
Can you please post the entire DP statement that you’ve used?
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
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.
how to capture the dropdown menu in QTP?
Hi Neelam,
It depends.. What is the technology of your application (Web, .Net, Java etc)?
ok
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
^\$\d{1,}\/Annually$
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
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 FunctionI 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! :)
Anshoo,
can you write an article on understanding DOM ? do let us know if you have any other site to reference for
thx!
Deepak: I’ll see what I can do :)
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.
Hi Steven,
Approach #2 uses DOM. We can certainly use
getElementByIdorgetElementsByTagNamebut, 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.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.
Thanks for all your efforts.