Scott Hanselman

Accessing Fields with Regular Expressions and Watir

February 14, 2006 Comment on this post [1] Posted in ASP.NET | Ruby | Watir
Sponsored By

This is just a reminder to myself and anyone else using Watir for Web Application Testing. Nearly any method supports using Regular Expressions within. Brian Vallelunga reminded me of this.

For example:

ie.text_field(:id, /myTextBoxName$/).set("Hello World")

That finds any control that ends with "myTextBoxName" and sets its value

Here is an example using the link method to click a hyperlink using the url attribute:

ie.link(:url, /shtml/).click

This will click the link that matches shtml. RTM!

Now playing: Living On Earth - Living on Earth: February 10, 2006

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Hosting By
Hosted in an Azure App Service
February 14, 2006 20:41
Well thanks for the plug. The reason for mentioning this is that when using asp.net, controls ids tend to look like:

ctl00_ctl00_TabPageContent_MainContent_ApplicationPage_ApplicantDetails_nameTextBox

If using the actual id to reference a control a developer risks breaking the script if he changes any id of any controls in the hierarchy. Using regular expressions to just grab the end name is great solution to this problem.

Just be on the lookout when reusing multiple versions of the same composite control. In that situation you might need to use a two-part name. For example, I have a date control that consists of a dropdown for month and a textbox for year. With multiple of those on a form, the textboxes and dropdowns will have the same internal name and thus their id's will end in the same value.

I might have something like:

ct100_jobStartDatePicker_monthDropDown

and

ct100_jobEndDatePicker_monthDropDown

In this case the script to identify the select the first dropdown control would be:

ie.select_list(:id, /jobStartDatePicker_monthDropDown$/).select("April")

Comments are closed.

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.