Forum Discussion

brian_kratsch's avatar
brian_kratsch
Contributor
10 years ago
Solved

Selecting unique value from drop down list with duplicates

Background: Keyword Test user, very little coded experience but I can usually follow examples



Issue:

I have run into a HTML Select object (ObjectType Select) that has duplicate values in the list. The Select contains 2 optgroups and in the first optgroup it contains the same value as the 2nd opt group. 

I would typically use object.ClickItem using keyword tests to select the item. However if I try this it will click the first option that matches in the list when I really want the option from the 2nd optgroup.

Can anyone provide an example on how to solve this issue as I can only assume it is a common issue.

I have included a screen shot.









  • Hi Brian,



    You can also use a custom ClickItem function to click the Nth matching item. For example (assuming your project uses JScript):



    /*

    Selects the Nth duplicate item from the combo box.



    Parameters:

    ComboBox - the combo box object.

    Label - the item text.

    Index - the index (from 1) of the duplicate item to select.

    */

    function ClickItem(ComboBox, Label, Index)

    {

      var ind = 1;

      for (var i = 0; i < ComboBox.wItemCount; i++)

      {

        if (ComboBox.wItem(i) == Label)

        {

          if (ind == Index)

          {

            ComboBox.ClickItem(i);

            return;

          }

          else

            ind++;

        }

      }

    }



    In your keyword test, use the Run Script Routine operation to call this ClickItem function with the following parameters:

    ComboBox: Aliases.browser.<page_name_and_name_of_the_select_element>

    Label: item text

    Index: 2

11 Replies

  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)
    Hi Brian,



    You can also use a custom ClickItem function to click the Nth matching item. For example (assuming your project uses JScript):



    /*

    Selects the Nth duplicate item from the combo box.



    Parameters:

    ComboBox - the combo box object.

    Label - the item text.

    Index - the index (from 1) of the duplicate item to select.

    */

    function ClickItem(ComboBox, Label, Index)

    {

      var ind = 1;

      for (var i = 0; i < ComboBox.wItemCount; i++)

      {

        if (ComboBox.wItem(i) == Label)

        {

          if (ind == Index)

          {

            ComboBox.ClickItem(i);

            return;

          }

          else

            ind++;

        }

      }

    }



    In your keyword test, use the Run Script Routine operation to call this ClickItem function with the following parameters:

    ComboBox: Aliases.browser.<page_name_and_name_of_the_select_element>

    Label: item text

    Index: 2
    • hhagay's avatar
      hhagay
      Contributor

      Hello

      I found your solution while searching for somewhat similar issue.

      I use TC12 with Javascript

      The AUT uses react to dynamically generate object ids

       

      How do I find the dropdown object? then how do i map its list items given that they are hidden and my attempts to click and keep the dropdown list fail.

       

      Than you

       

       

       

      • AlexKaras's avatar
        AlexKaras
        Champion Level 3

        Hi,

         

        > The AUT uses react to dynamically generate object ids. How do I find the dropdown object?

        To locate the dropdown object use Object Spy and drag its 'crosshair' control over dropdown control of your application. To reliably identify the found object during test runs you must examine it in the Object Browser and find the combination of parameters and their values that do not change from run to run. More details can be found in the https://support.smartbear.com/screencasts/testcomplete/reliable-tests-for-dynamic-objects/ recording and https://support.smartbear.com/testcomplete/docs/testing-with/object-identification/index.html help topic.

         

        > then how do i map its list items given that they are hidden and my attempts to click and keep the dropdown list fail.

        If the dropdown list is created only after the dropdown is expanded, you may use Object Spy in the Point-and-Click mode (use its control with an arrow to switch this mode on). Expand your combobox, point to it, fix by pressing Ctrl-A shortcut and examine obtained object in the Object Spy. (Note, the object may disappear in the Object Spy after some time if it is destroyed in the tested application. Repeat the above procedure if this is the case.)

        You may also to record selection of some item from the dropdown to get an idea of how things are recognized by TestComplete and use recorded script along with the info obtained from the Object Spy to work out test code that meets your needs.

        In real test code the sequence of actions should be like this: click the dropdown control to expand it; find the object with list items; iterate through the list to find the item that you need (or search for that item using XPath); click the found item.

  • TanyaYatskovska's avatar
    TanyaYatskovska
    SmartBear Alumni (Retired)
    Hi Brian,

     


    I suggest that you try using the EvaluateXPath method to get the needed item. In this case, you can get access to the object based on the HTML structure of the page. Here is the sample XPath expression you can use:


    //optgroup[@label="Random Lenght"]/option[text()="Prime"]


     


    Refer to the "Finding Web Objects Using XPath Expressions" article for more information.