Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple TestFX example fails

Working with TestFX 4.0.14 in Eclipse photon and fx9 or fx11 (doesn't matter), the simple example test from the TestFX wiki fails in should_click_on_button() with

Expected: Labeled has text "clicked!"
         but: was "click me!"

When looking at the screen, the pane and its contained button is shown, but the mouse moves to someplace else: so the button is never clicked and consequently its text never changed.

Any idea what's wrong/how to fix?

The test code (all copied from the wiki for convenience):

import org.junit.Test;
import org.testfx.framework.junit.ApplicationTest;

import static org.testfx.api.FxAssert.*;
import static org.testfx.matcher.control.LabeledMatchers.*;

import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

/**
 * Simple testfx example from testfx wiki:
 * https://github.com/TestFX/TestFX/wiki/Getting-Started
 * 
 */
public class ClickApplicationTest extends ApplicationTest {
    @Override
    public void start(Stage stage) {
        Parent sceneRoot = new ClickApplication.ClickPane();
        Scene scene = new Scene(sceneRoot, 100, 100);
        stage.setScene(scene);
        stage.show();
    }

    @Test
    public void should_contain_button() {
        // expect:
        verifyThat(".button", hasText("click me!"));
    }

    @Test
    public void should_click_on_button() {
        // when:
        clickOn(".button");

        // then:
        verifyThat(".button", hasText("clicked!"));
    }


}

The application code:

import javafx.application.Application;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

/**
 * Simple testfx example from testfx wiki:
 * https://github.com/TestFX/TestFX/wiki/Getting-Started
 * 
 */
public class ClickApplication extends Application {
    // application for acceptance tests.
    @Override public void start(Stage stage) {
        Parent sceneRoot = new ClickPane();
        Scene scene = new Scene(sceneRoot, 100, 100);
        stage.setScene(scene);
        stage.show();
    }

    // scene object for unit tests
    public static class ClickPane extends StackPane {
        public ClickPane() {
            super();
            Button button = new Button("click me!");
            button.setOnAction(actionEvent -> button.setText("clicked!"));
            getChildren().add(button);
        }
    }
}

Update:

Found an open issue in TestFX that might match. It mentions a core fx bug that might be the reason - but doesn't seem to be: it's fixed in fx11 (verified that the code in the core bug report passes), but the testfx issue prevails ..

like image 713
kleopatra Avatar asked Oct 02 '18 09:10

kleopatra


1 Answers

It works for me.

Anyway, you may be checking the UI change before it happens since it's done in the FX Application Thread and not in the thread that is executing the test.

Use this line

WaitForAsyncUtils.waitForFxEvents()

between the clickOn and verifyThat calls.

like image 156
transgressoft Avatar answered Sep 21 '22 16:09

transgressoft