forked from nus-cs2103-AY2324S2/ip
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package gui; | ||
|
||
// this code is taken from https://github.com/TestFX/TestFX | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.testfx.api.FxRobot; | ||
import org.testfx.assertions.api.Assertions; | ||
import org.testfx.framework.junit5.ApplicationExtension; | ||
import org.testfx.framework.junit5.Start; | ||
|
||
import javafx.scene.Scene; | ||
import javafx.scene.control.Button; | ||
import javafx.scene.layout.StackPane; | ||
import javafx.stage.Stage; | ||
|
||
@ExtendWith(ApplicationExtension.class) | ||
class JavaFxTest { | ||
|
||
private Button button; | ||
|
||
/** | ||
* Will be called with {@code @Before} semantics, i. e. before each test method. | ||
* | ||
* @param stage - Will be injected by the test runner. | ||
*/ | ||
@Start | ||
private void start(Stage stage) { | ||
button = new Button("click me!"); | ||
button.setId("myButton"); | ||
button.setOnAction(actionEvent -> button.setText("clicked!")); | ||
stage.setScene(new Scene(new StackPane(button), 100, 100)); | ||
stage.show(); | ||
} | ||
|
||
/** | ||
* @param robot - Will be injected by the test runner. | ||
*/ | ||
@Test | ||
void buttonShouldHaveText(FxRobot robot) { | ||
Assertions.assertThat(button).hasText("click me!"); | ||
// or (lookup by css id): | ||
Assertions.assertThat(robot.lookup("#myButton").queryAs(Button.class)).hasText("click me!"); | ||
// or (lookup by css class): | ||
Assertions.assertThat(robot.lookup(".button").queryAs(Button.class)).hasText("click me!"); | ||
// or (query specific type): | ||
Assertions.assertThat(robot.lookup(".button").queryButton()).hasText("click me!"); | ||
} | ||
|
||
/** | ||
* @param robot - Will be injected by the test runner. | ||
*/ | ||
@Test | ||
void textChangesIfButtonClicked(FxRobot robot) { | ||
// when: | ||
robot.clickOn(".button"); | ||
|
||
// then: | ||
Assertions.assertThat(button).hasText("clicked!"); | ||
// or (lookup by css id): | ||
Assertions.assertThat(robot.lookup("#myButton").queryAs(Button.class)).hasText("clicked!"); | ||
// or (lookup by css class): | ||
Assertions.assertThat(robot.lookup(".button").queryAs(Button.class)).hasText("clicked!"); | ||
// or (query specific type) | ||
Assertions.assertThat(robot.lookup(".button").queryButton()).hasText("clicked!"); | ||
} | ||
} |