Selecting Option in a Select List (Drop Down List) : General Questions

Selecting Option in a Select List (Drop Down List)

Ask general questions here.

Selecting Option in a Select List (Drop Down List)

Postby Pixi6s » Thu Oct 06, 2011 4:35 pm

I am using Ranorex 3.1 and C#. I am using IE 9.

The web site I am testing has a Drop Down (Select List) and I need to change the value of this during my test runs. I really feel like I am missing something because this has not been easy. I don't care if I accomplish this via the record module or user code. When I do record it clicks the asset that is behind the list when it is "dropped down". I just want a "set" option available for this asset. It's very frustrating.

Any ideas?
Attachments
option.jpg
option.jpg (106.63 KiB) Viewed 278 times
select.jpg
select.jpg (116.88 KiB) Viewed 278 times
Pixi6s
 
Posts: 86
Joined: Tue Jun 28, 2011 9:57 pm

Re: Selecting Option in a Select List (Drop Down List)

Postby Support Team » Fri Oct 07, 2011 12:25 pm

Hi,

To select a specific option tag in your select tag you could use following code
SelectTag someSelectTag = "/dom[@domain='www.w3schools.com']/body/table[2]/tbody/tr[1]/td[2]/iframe/body/select";
OptionTag optTag = someSelectTag.FindSingle(".//option[@value='Volvo']");
optTag.Selected = true;

The sample page is http://www.w3schools.com/tags/tryit.asp ... tml_option
The code above works in both browsers (IE and Firefox).

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

Re: Selecting Option in a Select List (Drop Down List)

Postby Pixi6s » Fri Oct 07, 2011 10:42 pm

That did work, I see the select box change values, unfortunately it didn't fire off the code that is suppose to be called when the drop down is changed. If you have any other suggestions let me know, otherwise I will just have to use the first letters and Up and Down Arrow to navigate it.
Pixi6s
 
Posts: 86
Joined: Tue Jun 28, 2011 9:57 pm

Re: Selecting Option in a Select List (Drop Down List)

Postby Ciege » Sat Oct 08, 2011 12:10 am

Try this method:
Code: Select all
        public static int DOMComboSelect(Ranorex.WebDocument webDocumentName, string ComboName, string ComboItem)
        {
            /************************************************************************
             * Function         : DOMComboSelect(Ranorex.WebDocument webDocumentName, string ComboName, string ComboItem)
             *
             * Description      : This method will select a ComboBox item in a web document.
             *                         
             * Parameters       : webDocumentName   - The web DOM object the the ComboBox lives on
             *                  : ComboName         - Name of the ComboBox to click
             *                  : ComboItem         - Item in the ComboBox to select.
             *                 
             * Return Type      : Integer
             *
             * Return Value     : 0 for success, -1 for failure
             *
             * Revision History
             * Date       : Author                    : Reason/Change
             * ---------- : ------------------------- : ------------------------------
             * 03/10/2009 : Chris Gendreau            : Initial Creation
             ************************************************************************/

            Ranorex.SelectTag DOMCombo;

            //Search for the ComboBox on the page
            try
            {
                webDocumentName.EnsureVisible();
                Thread.Sleep(500);
                DOMCombo = webDocumentName.FindSingle(".//select[@Id='" + ComboName + "']");
            }

            catch (RanorexException e)
            {
                Report.Error("Unable to find ComboBox: " + ComboName);
                Console.WriteLine(e.ToString());
                Report.Screenshot();
                return -1;
            }

            //Verify Item exists in ComboBox
            int intResult = DOMComboVerifyItem(webDocumentName, ComboName, ComboItem);
            if (intResult == -1)
            {
                Report.Error("Item: " + ComboItem + " is not a valid item in the ComboBox: " + ComboName);
                return -1;
            }

            //Click a ComboBox & Item
            try
            {
                Report.Info("Selecting item: " + ComboItem + " in  ComboBox: " + ComboName);
                DOMCombo.EnsureVisible();
                DOMCombo.Focus();
                Thread.Sleep(1000);
                DOMCombo.Click(Location.CenterRight, 1000);
                Thread.Sleep(1000);
                ListItem item = "/container[@caption='selectbox']/listitem[@text='" + ComboItem + "']";
                item.Click(Location.Center, 1000);
                Report.Debug("  Selected item: " + ComboItem + " in  ComboBox: " + ComboName);
            }

            catch (RanorexException e)
            {
                Report.Error(e.ToString());
                Report.Screenshot();
                return -1;
            }

            return 0;
        } //End DOMComboSelect
If this or any response has helped you, please reply to the thread stating that it worked so other people with a similar issue will know how you fixed your issue!

Ciege...
User avatar
Ciege
 
Posts: 965
Joined: Thu Oct 16, 2008 7:46 pm
Location: Arizona, USA

Re: Selecting Option in a Select List (Drop Down List)

Postby natasha » Wed Nov 23, 2011 1:37 am

Hi - I am having the same problem as Pixi6s above... if I use the following code, the code that is supposed to fire on dropdown selection change does not execute:

Code: Select all
ListItem item = "/container[@caption='selectbox']/listitem[@text='" + name + "']";      
OptionTag option = dropDown.FindSingle(".//option[@innertext='"+ name +"']");

option.Selected = true;
item.Click();


If I remove the 'option.Selected' property as detailed below, the dropdown change action executes properly, but if the list item is out of view and requires scrolling, the wrong ListItem is selected.

Code: Select all
ListItem item = "/container[@caption='selectbox']/listitem[@text='" + name + "']";   

OptionTag option = dropDown.FindSingle(".//option[@innertext='"+ name +"']");

//option.Selected = true;
item.Click();


Hope you can help with this.

Thanks,
Natasha
natasha
 
Posts: 5
Joined: Mon Oct 17, 2011 8:18 pm

Re: Selecting Option in a Select List (Drop Down List)

Postby sham526 » Wed Nov 23, 2011 11:22 am

you can use the below function which we created for our purpose.

Find_str is the string which need to be found and office_path is the object for the selectTag.


Public Function fnDropDown_FindOption(ByVal Find_str As String, ByVal office_path As Ranorex.SelectTag) As Boolean

fnDropDown_FindOption = False
Try
Dim optag_office As System.Collections.Generic.IList(Of OptionTag) = office_path.Find(Of Ranorex.OptionTag)("./option")

For c As Integer = 1 To optag_office.Count

Dim opt_office_rxpath As New RxPath("./option[" + c.ToString + "]")
Dim office_opt_port As Ranorex.OptionTag = office_path.FindSingle(Of Ranorex.OptionTag)(opt_office_rxpath)
Dim offname As String = office_opt_port.InnerText.Trim

If offname.ToLower = Find_str.ToLower.Trim Then
office_path.EnsureVisible()
office_path.Click()
optag_office(c - 1).Selected = True
optag_office(c - 1).PerformClick()
office_path.Click()
fnDropDown_FindOption=True
Exit For
End If
Next

Catch ex As RanorexException
MsgBox(ex.ToString)
End Try
End Function


This Function will return True if the required was selected else it will return False
sham526
 
Posts: 17
Joined: Wed Jul 07, 2010 8:12 am
Location: Hyderabad(INDIA)


Return to General Questions

Who is online

Users browsing this forum: No registered users and 1 guest

cron