Choose specific item from dropdown ComboBox

Class library usage, coding and language questions.
WajdaW
Posts: 75
Joined: Wed Jan 05, 2011 6:45 pm

Choose specific item from dropdown ComboBox

Post by WajdaW » Sun Feb 13, 2011 7: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

User avatar
artur_gadomski
Posts: 207
Joined: Mon Jul 19, 2010 6:55 am
Location: Copenhagen, Denmark
Contact:

Re: Choose specific item from dropdown ComboBox

Post by artur_gadomski » Mon Feb 14, 2011 8: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();

WajdaW
Posts: 75
Joined: Wed Jan 05, 2011 6:45 pm

Re: Choose specific item from dropdown ComboBox

Post by WajdaW » Mon Feb 14, 2011 11:11 am

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

User avatar
Support Team
Site Admin
Site Admin
Posts: 11709
Joined: Fri Jul 07, 2006 4:30 pm
Location: Graz, Austria

Re: Choose specific item from dropdown ComboBox

Post by Support Team » Mon Feb 14, 2011 11:36 am

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
.
Image

WajdaW
Posts: 75
Joined: Wed Jan 05, 2011 6:45 pm

Re: Choose specific item from dropdown ComboBox

Post by WajdaW » Tue Feb 15, 2011 1: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

User avatar
Support Team
Site Admin
Site Admin
Posts: 11709
Joined: Fri Jul 07, 2006 4:30 pm
Location: Graz, Austria

Re: Choose specific item from dropdown ComboBox

Post by Support Team » Tue Feb 15, 2011 2: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
.
Image

sandeep
Posts: 2
Joined: Wed Mar 02, 2011 2:57 pm

Re: Choose specific item from dropdown ComboBox

Post by sandeep » Thu Mar 03, 2011 7: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.

User avatar
Support Team
Site Admin
Site Admin
Posts: 11709
Joined: Fri Jul 07, 2006 4:30 pm
Location: Graz, Austria

Re: Choose specific item from dropdown ComboBox

Post by Support Team » Thu Mar 03, 2011 10: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
.
Image