Find a Combobox Item

9 posts, 1 answers
  1. Natasha
    Natasha avatar
    21 posts
    Member since:
    Jun 2011

    Posted 20 Dec 2011 Link to this post

    My RadComboboxItem is composed for two textbock. I need to find an element only by the content of the second textbock.
    Name of the first text box is Ident.
    Name of the second text box is content.

    Change: I have attached the code
  2. Stoich
    Admin
    Stoich avatar
    1 posts

    Posted 23 Dec 2011 Link to this post

    Hello Natasha,
    this requires a coded solution. I've attached the test that gets the job done. Here's the actual code I use with comments explaining each line of code:

    String textBoxToSearchFor = "new 4";
                 
                Pages.SilverlightApplication2.SilverlightApp.ComboRadcombobox.ToggleDropDown(); //Open drop down.
                //We access a pre-generated variable to access the element because we added it to the Elements Explorer
              
                 
                //Go through each item in the combobox and find the one that hold the required textbox
                foreach (Telerik.WebAii.Controls.Xaml.RadComboBoxItem cbi in  Pages.SilverlightApplication2.SilverlightApp.ComboRadcombobox.ItemElements) {
                  
                 try //We use try/catch because Find.ByTextContent throws an exception when it doesn't find what it's looking for.
                    cbi.Find.ByTextContent(textBoxToSearchFor); }
                catch (Exception e) {
                 continue;   //Move on to the next item if the TextBox was not found inside of this one
                }
              
                cbi.Select(); //Select this item if the Exception was not thrown (i.e. Find found what it was looking for)
                }

    I hope this helps, let me know if you have any additional questions.

    Greetings,
    Stoich
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. Natasha
    Natasha avatar
    21 posts
    Member since:
    Jun 2011

    Posted 23 Dec 2011 Link to this post

    Hi,
    In your solution the tool the search for element in both textblock (indent and value).
    I just want to search in combobox "value".
    Thanks for your help.
  4. Stoich
    Admin
    Stoich avatar
    1 posts

    Posted 27 Dec 2011 Link to this post

    Hi Natasha,
    this requires a very minor change. The value TextBlock has the following tagname:
    <TextBlock Name='content' AutomationId='content' Uid='34476226'>

    So we need to verify that the element we want to select has that AutomationId. Here's the code that does it:
    if (!cbi.AutomationId.Equals("content")) { continue; } else //If AutmationId does not match value TextBlock, move on
                {
                cbi.Select(); //Select this item if the Exception was not thrown (i.e. Find found what it was looking for)
                }

    Here's how this integrates in the complete coded step:
    String textBoxToSearchFor = "new 4";
                  
                Pages.SilverlightApplication2.SilverlightApp.ComboRadcombobox.ToggleDropDown(); //Open drop down.
                //We access a pre-generated variable to access the element because we added it to the Elements Explorer
               
                  
                //Go through each item in the combobox and find the one that hold the required textbox
                foreach (Telerik.WebAii.Controls.Xaml.RadComboBoxItem cbi in  Pages.SilverlightApplication2.SilverlightApp.ComboRadcombobox.ItemElements) {
                   
                 try //We use try/catch because Find.ByTextContent throws an exception when it doesn't find what it's looking for.
                    cbi.Find.ByTextContent(textBoxToSearchFor); }
                catch (Exception e) {
                 continue;   //Move on to the next item if the TextBox was not found inside of this one
                }
               
                if (!cbi.AutomationId.Equals("content")) { continue; } else //If AutmationId does not match value TextBlock, move on
                {
                cbi.Select(); //Select this item if the Exception was not thrown (i.e. Find found what it was looking for)
                }
                     
                }

    Kind regards,
    Stoich
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  5. Natasha
    Natasha avatar
    21 posts
    Member since:
    Jun 2011

    Posted 27 Dec 2011 Link to this post

    Hi Stoich,
    I noticed this solution is very slow. For a combobox with 50 comboboxItem the solution delay is 4 minutes.
    You have any idea how minimize the delay???
  6. Stoich
    Admin
    Stoich avatar
    1 posts

    Posted 28 Dec 2011 Link to this post

    Hi Natasha,
    the slowness is there because we're waiting for this:
    cbi.Find.ByTextContent(textBoxToSearchFor);
    to timeout which takes about 10 seconds.

    We can change the so-called 'Find Strategy" in order to change this and speed things up. Check out the following blog post on setting Find Strategy:
    http://blogs.telerik.com/blogs/posts/10-06-24/silverlight-test-automation-changing-the-element-find-strategy-to-avoid-timeoutexecution-2578840260.aspx

    The new code should look like this:
    String textBoxToSearchFor = "new 4";
                   
                Pages.SilverlightApplication2.SilverlightApp.ComboRadcombobox.ToggleDropDown(); //Open drop down.
                //We access a pre-generated variable to access the element because we added it to the Elements Explorer
                
     
          // save the original strategy to set it back at test end
                FindStrategy originalStrategy = app.Find.Strategy;
     
      // reset the find strategy
                    app.Find.Strategy = FindStrategy.WhenNotVisibleReturnNull;
     
     
                   
     
                //Go through each item in the combobox and find the one that hold the required textbox
                foreach (Telerik.WebAii.Controls.Xaml.RadComboBoxItem cbi in  Pages.SilverlightApplication2.SilverlightApp.ComboRadcombobox.ItemElements) {
                    
                
                  if (  cbi.Find.ByTextContent(textBoxToSearchFor) != null) {
              
                
                if (!cbi.AutomationId.Equals("content")) { continue; } else //If AutmationId does not match value TextBlock, move on
                {
                cbi.Select();
                app.Find.Strategy = originalStrategy;
                break;
                }
                      
                }
            }

    Greetings,
    Stoich
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  7. Natasha
    Natasha avatar
    21 posts
    Member since:
    Jun 2011

    Posted 28 Dec 2011 Link to this post

    Hi Stoich,
    This solution didn't find the element.
    I send you my code and the test.
    Thank you.
  8. Answer
    Stoich
    Admin
    Stoich avatar
    1 posts

    Posted 01 Jan 2012 Link to this post

    Hi Natasha,

    I reworked the code so that you don't need to change the Find Strategy. Currently setting Find Strategy doesn't seem to work 100% as expected which causes the code I sent you not to work. Here's the revised code:

    // Navigate to : 'http://localhost:11960/SilverlightApplication2TestPage.aspx'
                ActiveBrowser.NavigateTo("http://localhost:11960/SilverlightApplication2TestPage.aspx");
                app = this.ActiveBrowser.SilverlightApps()[0]; 
              
                // radcombobox: drop down 'Open' action.
                String textBoxToSearchFor = "val6";
                                
                Pages.SilverlightApplication2.SilverlightApp.ComboRadcombobox.ToggleDropDown(); //Open drop down.
                //We access a pre-generated variable to access the element because we added it to the Elements Explorer
     
                //Go through each item in the combobox and find the one that hold the required textbox
                foreach (Telerik.WebAii.Controls.Xaml.RadComboBoxItem cbi in  Pages.SilverlightApplication2.SilverlightApp.ComboRadcombobox.ItemElements) {
                     
              //      FrameworkElement fwe = cbi.Find.ByText(textBoxToSearchFor);
                       FrameworkElement fwe = cbi.Find.ByExpression(new XamlFindExpression("textcontent="+textBoxToSearchFor));  
                 
                if (fwe != null)
                    {
                        Log.WriteLine("Found "+fwe.ToString());
                         
                        if (!fwe.AutomationId.Equals("content")) {
                            Log.WriteLine("Found desired text but not in 'content' textbox");
                            continue; } else
                             
                            {
                            cbi.Select();                     
                            break;
                            }       
                    }
     
            }

    Test is attached.  

    All the best,
    Stoich
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  9. Natasha
    Natasha avatar
    21 posts
    Member since:
    Jun 2011

    Posted 01 Jan 2012 Link to this post

    Hi Stoich.
    Thank you.
    Happy new year
Back to Top