Choose specific item from dropdown ComboBox : Automation API

Choose specific item from dropdown ComboBox

Class library usage, coding and language questions.

Choose specific item from dropdown ComboBox

Postby WajdaW » Sun Feb 13, 2011 8:55 pm

Hi, I have this code for choosing some specific language from my dropdown list.
Sometimes it works, and sometimes not.
Code: Select all
        public void SwitchLanguage(string language)
        {
             // click on dropdown to open list from which to chose items
            languageSwitchComboBox.Click();
           // search for item in list by text
            for (int i = 0; i < languageSwitchComboBox.Items.Count; i++)
                if (language.Equals(languageSwitchComboBox.Items[i].Text))
                      // when its found click on that item
                      languageSwitchComboBox.Items[i].Click();
        }

Is there another more reliable way to do this?
I have run it few times and sometimes it didn't work...
Regards,

Vajda Vladimir
WajdaW
 
Posts: 75
Joined: Wed Jan 05, 2011 7:45 pm

Re: Choose specific item from dropdown ComboBox

Postby artur_gadomski » Mon Feb 14, 2011 9:03 am

Do you know that it doesn't work because it doesn't click or maybe it's because it can't find the language?

Anyway I found this way most reliable:
ComboBox c = _path;
c.Click();
c.Items[_index].Select();
User avatar
artur_gadomski
 
Posts: 207
Joined: Mon Jul 19, 2010 7:55 am
Location: Copenhagen, Denmark

Re: Choose specific item from dropdown ComboBox

Postby WajdaW » Mon Feb 14, 2011 12:11 pm

It doesn't find language in combobox.
I can't use:
Code: Select all
c.Items[_index].Select();


Because I don't know where language will be in list (on which index), I just pass the language name, I need to somehow find language in list by its name.

Regards,

Vajda Vladimir
WajdaW
 
Posts: 75
Joined: Wed Jan 05, 2011 7:45 pm

Re: Choose specific item from dropdown ComboBox

Postby Support Team » Mon Feb 14, 2011 12:36 pm

WajdaW wrote:Sometimes it works, and sometimes not.

I think the problem with your code is that you retrieve the ComboBox.Items property multiple times. Each time Ranorex will anew search for the items of the combobox. That could be a problem in terms of performance, but also of reliability if the list of items changes while executing the loop.

Why don't you use a relative RanoreXPath, e.g.:
public void SwitchLanguage(string language)
{
    // click on dropdown to open list from which to chose items
    languageSwitchComboBox.Click();
    // search for item in list by text
    ListItem item = languageSwitchComboBox.FindSingle<ListItem>(
    	"//item[@text='" + language + "']");
    item.Click();
}

Regards,
Alex
Ranorex Team
User avatar
Support Team
Site Admin
 
Posts: 6839
Joined: Fri Jul 07, 2006 5:30 pm
Location: Graz, Austria

Re: Choose specific item from dropdown ComboBox

Postby WajdaW » Tue Feb 15, 2011 2:54 pm

@Alex
It won't find element that way.
I searched combobox path and item path with ranorex spy instant search and those two paths are very much different, item path is not subtree to combobox path...


I had an extra click after opening combo box which was closing combo box so I think that was problem with my code. Now I fixed that click and works fine...

Regards,

Vajda Vladimir
WajdaW
 
Posts: 75
Joined: Wed Jan 05, 2011 7:45 pm

Re: Choose specific item from dropdown ComboBox

Postby Support Team » Tue Feb 15, 2011 3:01 pm

WajdaW wrote:I searched combobox path and item path with ranorex spy instant search and those two paths are very much different, item path is not subtree to combobox path...

That can be the case if the drop-down control is a top-level element.

Glad you could fix it!

Regards,
Alex
Ranorex Team
User avatar
Support Team
Site Admin
 
Posts: 6839
Joined: Fri Jul 07, 2006 5:30 pm
Location: Graz, Austria

Re: Choose specific item from dropdown ComboBox

Postby sandeep » Thu Mar 03, 2011 8:15 am

Hi,

In my case it is not detecting the drop down menu as is it.. it is being recognised as a element, so how to click it and select from the list.
sandeep
 
Posts: 2
Joined: Wed Mar 02, 2011 3:57 pm

Re: Choose specific item from dropdown ComboBox

Postby Support Team » Thu Mar 03, 2011 11:33 am

Hi,

Please could you post us a Ranorex snapshot of your drop down control. One in close state and one in open state.
How to create a Ranorex Snapshot:
http://www.ranorex.com/support/user-gui ... files.html

Thanks in advance.

Regards,
Peter
Ranorex Team
User avatar
Support Team
Site Admin
 
Posts: 6839
Joined: Fri Jul 07, 2006 5:30 pm
Location: Graz, Austria


Return to Automation API

Who is online

Users browsing this forum: No registered users and 0 guests