public class CalculatorApp extends Application { private static Optional<Callback<Parent>> callback = Optional.empty(); public static void main(String[] args) { launch(args); } @Override public void start(Stage primaryStage) throws Exception { BorderPane root = new BorderPane(); root.setCenter(new Calculator()); Scene scene = new Scene(root); primaryStage.setScene(scene); primaryStage.show(); callback.ifPresent(o -> o.call(root)); } public static void onLoad(Callback<Parent> r) { CalculatorApp.callback = Optional.of(r); } }
public interface Callback<T> { void call(T arg); }
public class FirstTest { private static GuiTest controller; @BeforeClass public static void setUpClass() { CalculatorApp.onLoad(r -> { controller = new GuiTest() { @Override protected Parent getRootNode() { return r; } }; }); FXTestUtils.launchApp(CalculatorApp.class); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } ...
FXTestUtils.launchApp(CalculatorApp.class);
private void clear() { controller.click("."); }
Event.fireEvent(your_node, new MouseEvent(MouseEvent.MOUSE_CLICKED, 0, 0, 0, 0, MouseButton.PRIMARY, 1, true, true, true, true, true, true, true, true, true, true, null));
@Before public void beforeTest() { clear(); }
public void click(int digit) { String numStr = Integer.toString(digit); for (int i = 0; i < numStr.length(); i++) { controller.click(String.valueOf(numStr.charAt(i))); } }
<Label fx:id="eq"... <Label fx:id="divide"... <Label fx:id="multiply"... <Label fx:id="subtract"... <Label fx:id="add"...
<?xml version="1.0" encoding="UTF-8"?> <?import java.net.*?> <?import javafx.scene.control.*?> <?import java.lang.*?> <?import javafx.scene.layout.*?> <fx:root maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" styleClass="root" type="GridPane" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"> <columnConstraints> <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" percentWidth="27.0" prefWidth="100.0" /> <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" percentWidth="27.0" prefWidth="100.0" /> <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" percentWidth="27.0" prefWidth="100.0" /> <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" percentWidth="19.0" prefWidth="100.0" /> </columnConstraints> <rowConstraints> <RowConstraints minHeight="10.0" percentHeight="25.0" prefHeight="30.0" vgrow="SOMETIMES" /> <RowConstraints minHeight="10.0" percentHeight="25.0" prefHeight="30.0" vgrow="SOMETIMES" /> <RowConstraints minHeight="10.0" percentHeight="25.0" prefHeight="30.0" vgrow="SOMETIMES" /> <RowConstraints minHeight="10.0" percentHeight="25.0" prefHeight="30.0" vgrow="SOMETIMES" /> <RowConstraints minHeight="10.0" percentHeight="25.0" prefHeight="30.0" vgrow="SOMETIMES" /> </rowConstraints> <children> <StackPane maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" GridPane.columnSpan="4"> <children> <TextField fx:id="input" alignment="CENTER_RIGHT" focusTraversable="false" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" styleClass="input" text="0" GridPane.columnSpan="4" /> <Label fx:id="description" styleClass="operation" StackPane.alignment="BOTTOM_LEFT" /> </children> </StackPane> <Label alignment="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" onMouseClicked="#handleClick" text="3" GridPane.columnIndex="2" GridPane.rowIndex="3" /> <Label alignment="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" onMouseClicked="#handleClick" text="9" GridPane.columnIndex="2" GridPane.rowIndex="1" /> <Label alignment="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" onMouseClicked="#handleClick" text="2" GridPane.columnIndex="1" GridPane.rowIndex="3" /> <Label alignment="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" onMouseClicked="#handleClick" text="1" GridPane.rowIndex="3" /> <Label alignment="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" onMouseClicked="#handleClick" text="5" GridPane.columnIndex="1" GridPane.rowIndex="2" /> <Label alignment="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" onMouseClicked="#handleClick" text="8" GridPane.columnIndex="1" GridPane.rowIndex="1" /> <Label alignment="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" onMouseClicked="#handleClick" text="4" GridPane.rowIndex="2" /> <Label alignment="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" onMouseClicked="#handleClick" text="7" GridPane.rowIndex="1" /> <Label alignment="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" onMouseClicked="#handleClick" text="," GridPane.rowIndex="4" /> <Label fx:id="eq" alignment="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" onMouseClicked="#handleEq" text="=" GridPane.columnIndex="2" GridPane.rowIndex="4" /> <Label alignment="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" onMouseClicked="#handleClick" text="0" GridPane.columnIndex="1" GridPane.rowIndex="4" /> <Label alignment="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" onMouseClicked="#handleClick" text="6" GridPane.columnIndex="2" GridPane.rowIndex="2" /> <GridPane styleClass="operations" GridPane.columnIndex="3" GridPane.rowIndex="1" GridPane.rowSpan="4"> <columnConstraints> <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" /> </columnConstraints> <rowConstraints> <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" /> <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" /> <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" /> <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" /> <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" /> </rowConstraints> <children> <Label alignment="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" onMouseClicked="#clear" text="." /> <Label fx:id="divide" alignment="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" onMouseClicked="#handleOperationSelect" text="÷" GridPane.rowIndex="1" /> <Label fx:id="multiply" alignment="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" onMouseClicked="#handleOperationSelect" text="×" GridPane.rowIndex="2" /> <Label fx:id="subtract" alignment="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" onMouseClicked="#handleOperationSelect" text="−" GridPane.rowIndex="3" /> <Label fx:id="add" alignment="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" onMouseClicked="#handleOperationSelect" text="+" GridPane.rowIndex="4" /> </children> </GridPane> </children> <stylesheets> <URL value="@../../../style/base.css" /> <URL value="@../../../style/skin.css" /> <URL value="@Calculator.css" /> </stylesheets> </fx:root>
public enum Operation { ADD, SUBTRACT, MULTIPLY, DIVIDE, EQ; }
public class OperationMatcher implements Matcher<Node> { private Operation operation; public OperationMatcher(Operation operation) { this.operation = operation; } @Override public boolean matches(Object item) { if (item instanceof Labeled) { String expected = operation.toString().toLowerCase(); String id = ((Labeled)item).getId(); if (id != null) { if (expected.equals(id.toLowerCase())) { return true; } } } return false; } ...
private void perform(Operation operation) { Matcher<Node> matcher = new OperationMatcher(operation); controller.click(matcher, MouseButton.PRIMARY); }
public void checkDescriptionField(String expectedText) throws InterruptedException { Thread.sleep(200); Node result = controller.find(".operation"); String actualText = ((Labeled) result).getText(); Assert.assertEquals(expectedText.trim(), actualText.trim()); } public void checkInputField(String expectedText) throws InterruptedException { Thread.sleep(200); Node result = controller.find(".input"); String actualText = ((TextField) result).getText(); Assert.assertEquals(expectedText.trim(), actualText.trim()); }
@Test public void testADD() throws InterruptedException { int digit1 = random.nextInt(1000); int digit2 = random.nextInt(1000); click(digit1); checkDescriptionField(String.valueOf(digit1)); checkInputField(String.valueOf(digit1)); perform(Operation.ADD); click(digit2); checkDescriptionField(digit1 + " + " + digit2); checkInputField(String.valueOf(digit2)); perform(Operation.EQ); checkInputField(String.valueOf(digit1 + digit2) + ",00"); } @Test public void testSubstract() throws InterruptedException { int digit1 = random.nextInt(1000); int digit2 = random.nextInt(1000); click(digit1); checkDescriptionField(String.valueOf(digit1)); checkInputField(String.valueOf(digit1)); perform(Operation.SUBTRACT); click(digit2); checkDescriptionField(digit1 + " − " + digit2); checkInputField(String.valueOf(digit2)); perform(Operation.EQ); checkInputField(String.valueOf(digit1 - digit2) + ",00"); }
Source: https://habr.com/ru/post/257981/
All Articles