Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. QA Tools
  3. Squish
  4. Squish for Java, select an item in a list box
Forum Updated to NodeBB v4.3 + New Features

Squish for Java, select an item in a list box

Scheduled Pinned Locked Moved Solved Squish
8 Posts 2 Posters 451 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Pieter Knelissen
    wrote last edited by
    #1

    I use Squish for Java (on a JavaFX app) and need to select an item in a list/combobox.
    I don't know upfront (design-time) which item I need to select as it is an input to my test.
    When the item is visible in the list when the list opens, my code works.
    But when the item is not visible (yet) and you would have to scroll down in the list, my code does not work.
    Any suggestions on how to solve this in a generic way (so with items visible by default and not visible by default) ?

    1 Reply Last reply
    1
    • H Offline
      H Offline
      henry.ishiyama
      wrote last edited by
      #2

      To interact with UI component (ex: list item), it has to exist, is visible and enabled.

      1 Reply Last reply
      0
      • P Offline
        P Offline
        Pieter Knelissen
        wrote last edited by
        #3

        @henry.ishiyama : Indeed. The list (from the list/combo box) from which I have to select is pretty long and by default only part of the list is visible. It requires scrolling in the list to make other items visible.
        But design-time, I don't know how much I have to scroll to make the desired item (the item to select) visible.
        The silly way that I found is to squish.NativeType("<Down>") and inspect the value of the 'current' item until I have found my desired item.
        There must be some smarter way....

        H 1 Reply Last reply
        1
        • P Pieter Knelissen

          @henry.ishiyama : Indeed. The list (from the list/combo box) from which I have to select is pretty long and by default only part of the list is visible. It requires scrolling in the list to make other items visible.
          But design-time, I don't know how much I have to scroll to make the desired item (the item to select) visible.
          The silly way that I found is to squish.NativeType("<Down>") and inspect the value of the 'current' item until I have found my desired item.
          There must be some smarter way....

          H Offline
          H Offline
          henry.ishiyama
          wrote last edited by
          #4

          @Pieter-Knelissen
          It is possible to use the following functions to scroll to the desired item.
          "setValue ", "mouseWheel"

          Also you can dump the complete List into a variable and access it.

          For example as follows:

          **sample test case : **
          # -*- coding: utf-8 -*-
          
          import names
          
          
          def main():
              startApplication("run.bat")
              
              # Use "setValue" and "mouseWheel" functions to scroll
              mouseClick(waitForObjectItem(names.defaultListModel_Example_JList, "Banana"), 75, 14, 0, Button.Button1)
              mouseClick(waitForObjectItem(names.defaultListModel_Example_JList, "Pineapple"), 77, 3, 0, Button.Button1)
              setValue(waitForObject(names.defaultListModel_Example_JScrollBar), 504) # setValue -> https://doc.qt.io/squish/java-setvalue-function.html
              setValue(waitForObject(names.defaultListModel_Example_JScrollBar), 378) # setValue -> https://doc.qt.io/squish/java-setvalue-function.html
              setValue(waitForObject(names.defaultListModel_Example_JScrollBar), 630) # setValue -> https://doc.qt.io/squish/java-setvalue-function.html
              setValue(waitForObject(names.defaultListModel_Example_JScrollBar), 74) # setValue -> https://doc.qt.io/squish/java-setvalue-function.html
              mouseClick(waitForObjectItem(names.defaultListModel_Example_JList, "Raspberry"), 241, 5, 0, Button.Button1)
              setValue(waitForObject(names.defaultListModel_Example_JScrollBar), 189) # setValue -> https://doc.qt.io/squish/java-setvalue-function.html
              mouseClick(waitForObjectItem(names.defaultListModel_Example_JList, "Nectarine"), 190, 7, 0, Button.Button1)
              
              j = 1
              for i in range(0, 25):
                  j = (j * -1)
                  mouseWheel((i * j)) # mouseWheel -> https://doc.qt.io/squish/java-mousewheel-function.html
                  snooze(1)
          
              # Dump the complete List into a variable and access it.
              tmpObj = waitForObject(names.defaultListModel_Example_JList)
              tmpList = tmpObj.parent.view.getModel()
              tmpSize = tmpList.size
              
              for i in range(0, tmpSize):
                  tmpItem = tmpList.get(i)
                  test.log(f"{i} {tmpItem}")
          
              test.breakpoint()
          
          **sample application : **
          // Example: Basic Usage
          import javax.swing.*;
          import java.awt.*;
          
          public class ListExample {
              public static void main(String[] args) {
                  JFrame frame = new JFrame("DefaultListModel Example");
                  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                  frame.setSize(300, 200);
          
                  // 1. Create the model
                  DefaultListModel<String> listModel = new DefaultListModel<>();
          
                  // 2. Add data to the model
                  listModel.addElement("Apple");
                  listModel.addElement("Banana");
                  listModel.add(1, "Orange"); // Insert Orange at index 1
                  listModel.addElement("Mango");
                  listModel.addElement("Pineapple");
                  listModel.addElement("Grapes");
                  listModel.addElement("Strawberry");
                  listModel.addElement("Blueberry");
                  listModel.addElement("Raspberry");
                  listModel.addElement("Blackberry");
                  listModel.addElement("Cherry");
                  listModel.addElement("Peach");
                  listModel.addElement("Plum");
                  listModel.addElement("Apricot");
                  listModel.addElement("Nectarine");
                  listModel.addElement("Kiwi");
                  listModel.addElement("Papaya");
                  listModel.addElement("Guava");
                  listModel.addElement("Lychee");
                  listModel.addElement("Dragon fruit");
                  listModel.addElement("Passion fruit");
                  listModel.addElement("Pomegranate");
                  listModel.addElement("Watermelon");
                  listModel.addElement("Cantaloupe");
                  listModel.addElement("Honeydew melon");
                  listModel.addElement("Coconut");
                  listModel.addElement("Fig");
                  listModel.addElement("Date");
                  listModel.addElement("Persimmon");
                  listModel.addElement("Cranberry");
                  listModel.addElement("Mulberry");
                  listModel.addElement("Gooseberry");
                  listModel.addElement("Starfruit (Carambola)");
                  listModel.addElement("Jackfruit");
                  listModel.addElement("Durian");
                  listModel.addElement("Tamarind");
                  listModel.addElement("Olive");
                  listModel.addElement("Avocado");
                  listModel.addElement("Lemon");
                  listModel.addElement("Lime");
                  listModel.addElement("Grapefruit");
                  listModel.addElement("Tangerine");
                  listModel.addElement("Clementine");
                  listModel.addElement("Soursop");
                  listModel.addElement("Sapodilla");
                  listModel.addElement("Rambutan");
                  listModel.addElement("Longan");
                  listModel.addElement("Mangosteen");
                  listModel.addElement("Breadfruit");
                  listModel.addElement("Custard apple");
          
                  // 3. Create JList with the model
                  JList<String> fruitList = new JList<>(listModel);
                  fruitList.setFont(new Font("Arial", Font.PLAIN, 16));
          
                  // Add list to a scroll pane for better viewing
                  JScrollPane scrollPane = new JScrollPane(fruitList);
                  frame.add(scrollPane, BorderLayout.CENTER);
          
                  frame.setVisible(true);
              }
          }
          
          1 Reply Last reply
          0
          • P Offline
            P Offline
            Pieter Knelissen
            wrote last edited by
            #5

            Henry, thank you for the pointers!

            1 Reply Last reply
            0
            • P Offline
              P Offline
              Pieter Knelissen
              wrote last edited by
              #6

              Henry, an additional question if I may:
              where can I find the attributes/methods supported by the various 'objects' that you mention in the python code?
              E.g. tmpObj.parent.view.getModel() : how do I know that 'parent', 'view' and 'getModel()' exist for the tmpObj?

              1 Reply Last reply
              0
              • P Offline
                P Offline
                Pieter Knelissen
                wrote last edited by
                #7

                I don't have the Java-code of the app that I need to run/test. What worked for me:
                obj = waitForObject (<name of the combobox>)
                obj.items.length gives the number of items in the combobox
                with obj.items.get(i) I retrieve the value of an item
                with obj.value=<value to assign> I assign the value as if I had clicked it.

                1 Reply Last reply
                1
                • P Pieter Knelissen has marked this topic as solved
                • H Offline
                  H Offline
                  henry.ishiyama
                  wrote last edited by
                  #8

                  @Pieter-Knelissen sorry for the delay.
                  regarding the attributes/methos available, it depends on the UI component used and the UI structure.
                  For example in our above sample test case "tmpObj" for the given sample application,
                  After the test case hits the breakpoint, and see the "Variables View", you will see "type" of each local variable.
                  And the "type" for "tmpObj" is javax_swing_JList. (Attached screenshot)
                  Therefore, from there on, you have to look into the JList and related UI component documentation to access the desired attributes and methods.
                  image.png

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved