assertj-swing icon indicating copy to clipboard operation
assertj-swing copied to clipboard

JComboBoxFixture.selectItem don't always work as expected

Open smazzoleni opened this issue 7 years ago • 5 comments

Hi Joel. First of all, thank you for your very interesting work. I've been using assertJ-core and assertJ-swing for several weeks now (and I like it a lot), but I'm experiencing random failures in my tests when playing with comboboxes.

Consider the following loop :

for (int i = 0; i < 10; i++) {
	GuiActionRunner.execute(() -> {
		JComboBox<String> combo = new JComboBox<>();
		combo.addItem("item_1");
		combo.addItem("item_2");
		jFrame = Containers.frameFor(combo);
		jFrame.setName(this.getClass().getName());
		jFrame.setVisible(true);
	});
	
	FrameFixture frame = new FrameFixture(jFrame);
	frame.comboBox().selectItem(1);
	frame.comboBox().requireSelection(1);    // **** randomly fails *****
	frame.cleanUp();
}

A ComparisonFailure is thrown most of the time, after a random number of iterations.

org.junit.ComparisonFailure: [javax.swing.JComboBox[name=null, selectedItem='item_1', contents=["item_1", "item_2"], editable=false, enabled=true, visible=true, showing=true] - property:'selectedIndex'] expected:<[1]> but was:<[0]>

I found a workaround by explicitly clicking on combo before selecting item.

	frame.comboBox().click().selectItem(1);

I noticed the problem whenever selecting items using index or text. What do you think ? Am I missing something or is it actually an issue ? Regards

smazzoleni avatar Jul 15 '17 15:07 smazzoleni

Hi Sergio,

thx for the feedback. Which version of AssertJ Swing are you using? Because I made some improvements for test stability between 2.x.x and 3.x.x. I ran your code and had no problems - even 1000 times.

  @Test
  public void test() {
    for (int i = 0; i < 1000; i++) {
      JFrame jFrame = GuiActionRunner.execute(() -> {
        JComboBox<String> combo = new JComboBox<>();
        combo.addItem("item_1");
        combo.addItem("item_2");
        JFrame frame = Containers.frameFor(combo);
        frame.setName(this.getClass().getName());
        frame.pack();
        frame.setVisible(true);
        return frame;
      });

      FrameFixture frame = new FrameFixture(robot, jFrame);
      frame.comboBox().selectItem(1);
      frame.comboBox().requireSelection(1);
      frame.cleanUp();
    }
  }

croesch avatar Aug 09 '17 13:08 croesch

Hello Joel,

thank you for your response. I was using AssertJ Swing 3.6.0 when I wrote. I upgraded to 3.8.0 and problem is still present with the sample you give me back (I add Robot robot = BasicRobot.robotWithNewAwtHierarchy(); before the loop).

Here are some info on my current configuration :

  • windows 10
  • eclipse oxygen
  • maven project
  • java 1.8.131
  • junit 4.12

Let me know if I can give you more helpful information.

smazzoleni avatar Aug 12 '17 08:08 smazzoleni

Hi Guys! We made the same observation as smazzoleni. Selecting by index or text fails in like 1 of 50 cases.

AssertJSwing 3.9.2 Java 8u191 Ubuntu 16.04 in a Docker Environment

The "workaround" of smazzoleni looks like to work.

KaiWissel avatar Aug 07 '20 07:08 KaiWissel

After some testing in the last couple of weeks, I have to state that the proposed workaround doesn't work in our setup. I've surrounded that sneaky b**** with a try-catch and let it rerun - up to three times -, but even then a failure will occur sometimes.

So far i couldn't find any pattern. The behaviour is totally random.

  • Robot clicks the ComboBox
  • PopUp opens
  • scrolls to the required item
  • ???
  • PopUp will close
  • The required item is not selected

KaiWissel avatar Aug 27 '20 12:08 KaiWissel

Any news on this? We seem to run into the same issue from time to time.

robvandyck avatar Jul 15 '21 09:07 robvandyck