javaswinggithub-actionsxvfb

Swing UI tests on Github Actions


I have a few UI tests for my Java application made with AssertJ-Swing and JUnit 5. I encounter no problem running these tests locally but some of the tests fail once I push them to my continuous integration on Github Actions.

The .yml looks like this:

...
jobs:
  build:
    runs-on: ubuntu-latest
    env:
      workdir: idTest

    steps:
    - uses: actions/checkout@v2
      with:
        fetch-depth: 0
    - name: Set up JDK 11
      uses: actions/setup-java@v1
      with:
        java-version: 11

    - name: Build with Maven
      run: > 
        xvfb-run mvn verify
      working-directory: ${{ env.workdir }}  

As you can see I'm using xvfb-run to run the tests headlessly and for my simple tests that should be enough.

Dumb example test that (sometimes) fails because the item is not selected:

@Test
@GUITest
void test() {

    Item item = new Item("name");
    GuiActionRunner.execute(() -> view.getListModel().addElement(item));

    window.list("list").selectItem(0);
    window.list("list").requireSelectedItems(0);
}

The "strange" thing is that sometimes the test passes and sometimes it doesn't. It happens also with click() on buttons. I've also tried running the tests under Windows (that needs no xvfb to run) on Github Actions with no problems, so I guess that xvfb is the issue here. Anyone knows what the problem might be? I'm also up for trying different solutions other than xvfb if anyone has any suggestions. Thanks.


Solution

  • Turns out that in my view class I had something like this:

    public class MyView extends JFrame{
        private CardLayout layout;
        private JPanel myPane;
            
        public MyView() {
            setResizable(false);
            setTitle("View");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setBounds(100, 100, 450, 300);
    
            layout = new CardLayout();
            getContentPane().setLayout(layout);
            myPane = new JPanel();
            getContentPane().add(myPane, "pane");
    
            layout.show(getContentPane(), "layout");
            pack();
            setLocationRelativeTo(null);    
        }
    }
    

    Removing the last three lines fixed it for me. I suppose that they were interfering with xvfb. Also it seems that they add no value to the code itself and it is safe to remove them.