Forum Discussion

Oferv's avatar
Oferv
Super Contributor
2 years ago

ClickItem for a WPF comboBox won't select an item by its caption, only by index

Hi,

we are using a WPF comboBox object in our app (System.Windows.Controls.ComboBox), I am trying unsuccessfully to select one of the comboBox items using its caption, it does work using index but, in this case, I can't use the index as once an item is selected the sorting of the items is mixed up. this comboBox has languages items in it so for the first time the app is loading and by default, the selected language will be English, and the sorting of the languages will be x and will be done by alphabetical order but, if I will select French (using an index for example) the sorting of the languages will change as all the languages are now written in French and they will get re-sorted by alphabetical order so, using an index is out of the question in this case. the only option here is to use the language caption. any idea how can I do that?

 

Thanks,

 

Ofer

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    What properties are exposed in Object Browser for your combo box?

    • Oferv's avatar
      Oferv
      Super Contributor

      Hi rraghvani,

      please find attached

      • tristaanogre's avatar
        tristaanogre
        Esteemed Contributor

        Right here is your problem, Oferv 

        This the propery of the combo box that gives the captions separated by the "pipe" character.  Notice that there are no values in between the pipes.  So, technically speaking, each item in your combo box, while it DISPLAYS a caption, doesn't actually have one.  The caption is coming from some other code, probably owner-drawn or some such thing.  They are probably images, not text.  This is why ClickItem will not work.

         

        And this probably backs up my assumption.  Notice the "wText" field is not actually displaying the text as it appears on screen but appears to be calling some sort of object method that looks up and renders the text.

         

        So, unfortunately, ClickItem is probably not going to work for you using the caption.  You might need to use some sort of "ClickText" execution where you click the combo box to get the drop down and then find and click on the appropriate text string.

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor

    I have seen in some situations that, if the caption of the combobox item has something other than text as part of it (some sort of icon, etc), then caption selection doesn't work.  Can you verify if the items are text only or do they have some sort of graphic component?

    • Oferv's avatar
      Oferv
      Super Contributor

      Hi tristaanogre,

       

      There are no icons but there's more than one caption. see attached

       

       

    • Oferv's avatar
      Oferv
      Super Contributor

      Yes I did but, in there it says that the items can be selected using clickitem using caption and this does not work for me https://support.smartbear.com/testcomplete/docs/app-testing/desktop/wpf/control-support.html

      When trying to record the item clicking, TC is using index and not caption

       

      def Test1():
      App = Aliases.DesignerProcess
      comboBox = App .DesignerMainWindow.BackstageViewMenu.BackStageWindowPanels.OptionsTabBackStageWindow.LanguageOptionComboBox
      comboBox.ToggleButton.ClickButton(cbChecked)
      App .HwndSource_PopupRoot.PopupRoot.DropDownScrollViewer.VScroll.Pos = 182.32000000000002
      comboBox.ClickItem(8)
      App .HwndSource_DominoMessageBox3.DominoMessageBox.ButtonOk.ClickButton()

       

      While I want to use comboBox.ClickItem("English") or any other optional language

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    I think it might be possible to select the item based on the name, but you will have to try and see how the name is constructed e.g. "Finnish\rsuomi". 

    Does the Methods tab give some indication as to what method to use to select an item?

     

    If you combo box is a custom control, then it may be tricky to select the item based on name. See WPF ComboBox Support

    • Oferv's avatar
      Oferv
      Super Contributor

      Sorry, there are so many methods, not sure how to copy-paste them all. taking screenshots of each will take ages. do you want to search for specific methods? i.e search for items/select/click etc.?

       

      How would you use the properties you've marked in your screenshot to select an item? I've tried and couldn't find a way to do that. can you please give an example?

       

      Thanks,

       

      Ofer

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    Click on the three little dots ... to drill down into the object, there may be some clues as to how to select the item.

    • Oferv's avatar
      Oferv
      Super Contributor

      Yes, that's what I'm doing but couldn't find anything that will help me select the item 😕

       

      If I drill down for example to the DataContext and then use the methods available, would it work on the object selection? or I can only use the methods available on the comboBox itself?

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    When using Aliases, you can shorten the name e.g. Aliases.LanguageOptionComboBox

     

    Good you found a workaround