Page 1 of 1

FindSingle using a partial @accessiblename

Posted: Tue Sep 04, 2012 7:27 pm
by carsonw
Hi there - I have a combo box with a drop down of currencies.

We select the currency we want like this:

Code: Select all

ListItem listItem = comboBox.FindSingle<ListItem>("list/listitem[@accessiblename='"+ testField.Value + "']");
listItem.Select();
listItem.Click();
What I want to do is just pass in "CAD" for the accessible name, but the actual name is "CAD - Canada Dollar".

Is there a way we can do this? Thanks :)

Carson.

Re: FindSingle using a partial @accessiblename

Posted: Tue Sep 04, 2012 7:34 pm
by Ciege
You can use regular expressions...
See here: http://www.ranorex.com/support/user-gui ... xpath.html

Basically you'll need to change your equal sign "=" to a tilde "~" after the "@accessiblename". Then use the appropriate RegEx expression for what you want to do.

Re: FindSingle using a partial @accessiblename

Posted: Wed Sep 05, 2012 1:10 am
by carsonw
...obviously! It's funny how that totally escaped me since we have regular expressions all over the place in our repository - we just rarely search for objects in the code itself so it didn't even occur to me.

Just shows how it's good to have people to bounce things off of from time to time. Thanks! :)

Re: FindSingle using a partial @accessiblename

Posted: Wed Sep 05, 2012 1:24 am
by carsonw
The only problem I run into now then, is that if I try to "FindChild" later I can only use the default label... not a regular expression in this case.

So this is what we're doing:

Code: Select all

public static Row GetDataRow(Dictionary<string, string> searchVaules, Table dataTable)
		{
			Row foundRow = null;
			foreach(Row iterationRow in dataTable.Rows)
			{
				bool valuesMatch = true;
				foreach(KeyValuePair<string, string> kvp in searchVaules)
				{
					if (kvp.Value != iterationRow.FindChild<Cell>(kvp.Key).Text)
					{
						valuesMatch = false;
						break;
					}
				}
				if (valuesMatch)
				{
					foundRow = iterationRow;
					break;
				}
			}
			return foundRow;
		}
But you can see I have to set the full, actual, text in this case. I tried using the regular expression earlier (so I could look for "CAD" instead of the whole thing) and that worked great. But now looking inside each cell for that value... I guess I could use "Find" instead of "FindChild", probably just as efficient and more flexible.

Re: FindSingle using a partial @accessiblename

Posted: Wed Sep 05, 2012 2:31 pm
by Support Team
Hi,

You can use each of the find methods mentioned on the following link: Row Class.

Regards,
Markus
Ranorex Support Team

Re: FindSingle using a partial @accessiblename

Posted: Wed Sep 05, 2012 6:58 pm
by carsonw
Ahh yes - saw FindSingle in there, but it turns out to be much simpler than I thought. The column names are absolute so that's fine, and I just needed to the contains in the value. Really, what I needed was to walk away from it for a little while :)

It all works now though, in case it's useful to others, this is what it looked like in the end:

Setting:

Code: Select all

default:
					// Open combobox using property  
					comboBox.DropDownVisible = true; 
					  
					// Select list item with from DropDown list  
					ListItem listItem = comboBox.FindSingle<ListItem>("list/listitem[@accessiblename~'^"+ testField.Value + ".*']");
					listItem.Select();
					listItem.Click();
					Delay.Seconds(delayAfterFieldSetInSeconds);
					break;
Getting a row from the grid based on a partial match of the grid VALUE:

Code: Select all

public static Row GetDataRow(Dictionary<string, string> searchVaules, Table dataTable)
		{
			Row foundRow = null;
			foreach(Row iterationRow in dataTable.Rows)
			{
				bool valuesMatch = true;
				foreach(KeyValuePair<string, string> kvp in searchVaules)
				{
					Cell cellToVerify = iterationRow.FindChild<Cell>(kvp.Key);
					if (!cellToVerify.Text.Contains(kvp.Value))
					{
						valuesMatch = false;
						break;
					}
				}
				if (valuesMatch)
				{
					foundRow = iterationRow;
					break;
				}
			}
			return foundRow;
		}
Thanks again for the insights :)

Re: FindSingle using a partial @accessiblename

Posted: Wed Sep 05, 2012 7:02 pm
by Ciege
Good stuff, thanks for sharing!