Selecting element in a collection of buttons by text

135 Views Asked by At

I am trying to loop through a collection of button objects in a SilverLight web page (there are 175 button objects) to find a button that has the text value = 'Airport". But I do not know how to get to the text property using WebAii (C#). Please could somebody help. Below is the code I have put together. The '????' parts are were I am stuck. Also, I am unsure of how to actually double click the element when I have found the one I am looking for, so if you could here here to it would be most appreciated.

var buttons2 =_silverlightApp.Find.AllByType<TextBlock>();         
for (int i = 0; i < buttons2.Count+ 1; i++)          
{

     if(buttons2.??????.text = "Airport")
     {
                 int elementNum = i;
                 ??????.LeftDoubleClick;
     }      

}
2

There are 2 best solutions below

1
On

I don't know if the Buttons you are displaying are created dynamically but in case it is an ordinary Button, declared somewhere in your xaml I recommend using AutomationIds.

<Button
    Content="Woohoo Airport! OMG!"
    AutomationProperties.AutomationId="AirportButton"/>

the WebAii class VisualFind offers

public FrameworkElement ByAutomationId(string id)

and you can call it on the (WebAii-)SilverlightApp or on a (WebAii-)FrameworkElement

SilverlightApp.Find.ByAutomationId("AirportButton");
FrameworkElement.Find.ByAutomationId("AirportButton");
2
On

I work for Telerik in the Test Studio and Webaii support team. I will endeavor to help you with this problem.

Let Webaii do the iterating and finding for you like this:

Button btn = _silverlightApp.Find.ByExpression(new XamlFindExpression("XamlTag=textblock","TextContent=Airport")).Parent().As();

I hope this helps.

Cody