Squish for Java, select an item in a list box
-
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) ? -
To interact with UI component (ex: list item), it has to exist, is visible and enabled.
-
@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.... -
@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....@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); } } -
Henry, thank you for the pointers!
-
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? -
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. -
P Pieter Knelissen has marked this topic as solved on
-
@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.

-
I’ve run into a similar issue with long JavaFX lists where the target item isn’t initially visible. Using the component’s available items/model instead of relying only on scrolling seems much more reliable, especially when the test input changes dynamically.
The approach with
obj.items.length,obj.items.get(i)and assigningobj.valuelooks particularly useful for making the test independent of the item’s position in the list. -
@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.

@henry.ishiyama said in Squish for Java, select an item in a list box:
@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.

Just to add, and visible in the screenshot as well: The object's (fully qualified) class name can be seen in the view "Application Objects" as well.
Alternatives to the use of the API documentation of the individual types/classes of GUI objects can be:
-
On object references: Use Python's dir() function. This will list all members, including methods.
-
When the object can be picked: You can see the member variables in Squish IDE's "Properties" view, and the methods in the "Variables" view.
There is one Squish specialty here:
Squish for Java maps getter/setter pairs to pseudo member variables. For example:
If the object's class has...
getThisOneThing() setThisOneThing()...then Squish makes the pseudo member variable...
thisonething...(yes, all lowercase) available on the object reference.
-
-
I’ve run into a similar issue with long JavaFX lists where the target item isn’t initially visible. Using the component’s available items/model instead of relying only on scrolling seems much more reliable, especially when the test input changes dynamically.
The approach with
obj.items.length,obj.items.get(i)and assigningobj.valuelooks particularly useful for making the test independent of the item’s position in the list.@Emma-2000 said in Squish for Java, select an item in a list box:
I’ve run into a similar issue with long JavaFX lists where the target item isn’t initially visible. Using the component’s available items/model instead of relying only on scrolling seems much more reliable, especially when the test input changes dynamically.
Agreed on using the model and toolkit API to access or even scroll a particular object into view.
(Squish for Java tries to automatically scroll an object into view upon mouseClick(). And one can pass
Button.NoButton(https://doc.qt.io/squish/java-convenience-api.html#java-convenience-function-parameters) tomouseClick()to trigger the scrolling attempt without performing a mouse click.)The approach with
obj.items.length,obj.items.get(i)and assigningobj.valuelooks particularly useful for making the test independent of the item’s position in the list.obj.items.get(i)requires specifying the index of the item, which I intuitively understand to be the "position in the list".Perhaps by "position" you meant the location on the screen? If so, yes, hardcoded positions are better to be avoided. (Squish is using screen positions for sending events, etc. - but it determines/calculates these screen locations at runtime.) Ah, after further consideration, you probably meant iterating over the items until the desired one is found. Alright.