Java 我能';t从测试类的对话框警报中获取文本

Java 我能';t从测试类的对话框警报中获取文本,java,testing,javafx,junit,hamcrest,Java,Testing,Javafx,Junit,Hamcrest,早上好, 我正在为一个javafx类做一个测试类,我现在正试图控制错误。当发生错误时,向我显示一个对话框警报,其中包含解释错误的消息。我想将此消息与测试类中的其他消息进行比较。 这是我最后一次尝试。 登录类 catch (ServerConnectionErrorException ex) { LOGGER.warning("LoginWindowController: Server connection error"); Alert alert

早上好, 我正在为一个javafx类做一个测试类,我现在正试图控制错误。当发生错误时,向我显示一个对话框警报,其中包含解释错误的消息。我想将此消息与测试类中的其他消息进行比较。
这是我最后一次尝试。

登录类

catch (ServerConnectionErrorException ex) {
            LOGGER.warning("LoginWindowController: Server connection error");
            Alert alert = new Alert(Alert.AlertType.ERROR);
            alert.setTitle("Server Error");
            alert.setContentText("Unable to connect with server");
            Label serverErrorContext = new Label();
            serverErrorContext.setText(alert.getContentText());
            serverErrorContext.setId("serverErrorContext");
            alert.showAndWait();
        }
@Test
    public void test4_ConnectionError(){
        clickOn("#txtLogin");
        write("user");
        clickOn("#txtPass");
        write("BBccd1234");
        clickOn("#btLogin");
        FxAssert.verifyThat("#serverErrorContext",LabeledMatchers.hasText("Unable to connect with server"));
        clickOn(".button");

    }
Alert alert = new Alert(Alert.AlertType.ERROR);
            alert.setTitle("Server Error");
            alert.setContentText("Unable to connect with server");
            Button errorButton = (Button) alert.getDialogPane().lookupButton(ButtonType.OK);
            errorButton.setId("serverConnectionError");
            alert.showAndWait();
clickOn("#txtLogin");
        write("user");
        clickOn("#txtPass");
        write("BBccd1234");
        clickOn("#btLogin");
        FxAssert.verifyThat("#serverConnectionError",isEnabled());
        clickOn("#serverConnectionError");
测试类

catch (ServerConnectionErrorException ex) {
            LOGGER.warning("LoginWindowController: Server connection error");
            Alert alert = new Alert(Alert.AlertType.ERROR);
            alert.setTitle("Server Error");
            alert.setContentText("Unable to connect with server");
            Label serverErrorContext = new Label();
            serverErrorContext.setText(alert.getContentText());
            serverErrorContext.setId("serverErrorContext");
            alert.showAndWait();
        }
@Test
    public void test4_ConnectionError(){
        clickOn("#txtLogin");
        write("user");
        clickOn("#txtPass");
        write("BBccd1234");
        clickOn("#btLogin");
        FxAssert.verifyThat("#serverErrorContext",LabeledMatchers.hasText("Unable to connect with server"));
        clickOn(".button");

    }
Alert alert = new Alert(Alert.AlertType.ERROR);
            alert.setTitle("Server Error");
            alert.setContentText("Unable to connect with server");
            Button errorButton = (Button) alert.getDialogPane().lookupButton(ButtonType.OK);
            errorButton.setId("serverConnectionError");
            alert.showAndWait();
clickOn("#txtLogin");
        write("user");
        clickOn("#txtPass");
        write("BBccd1234");
        clickOn("#btLogin");
        FxAssert.verifyThat("#serverConnectionError",isEnabled());
        clickOn("#serverConnectionError");

最后一个检查contentText是否与应该显示的消息相同的实例,我决定为每个警报声明一个按钮并为其分配一个id。每个id都是唯一的,因此机器人不可能单击其他按钮,如果allert没有正确显示,则测试可能是错误的。
主类

catch (ServerConnectionErrorException ex) {
            LOGGER.warning("LoginWindowController: Server connection error");
            Alert alert = new Alert(Alert.AlertType.ERROR);
            alert.setTitle("Server Error");
            alert.setContentText("Unable to connect with server");
            Label serverErrorContext = new Label();
            serverErrorContext.setText(alert.getContentText());
            serverErrorContext.setId("serverErrorContext");
            alert.showAndWait();
        }
@Test
    public void test4_ConnectionError(){
        clickOn("#txtLogin");
        write("user");
        clickOn("#txtPass");
        write("BBccd1234");
        clickOn("#btLogin");
        FxAssert.verifyThat("#serverErrorContext",LabeledMatchers.hasText("Unable to connect with server"));
        clickOn(".button");

    }
Alert alert = new Alert(Alert.AlertType.ERROR);
            alert.setTitle("Server Error");
            alert.setContentText("Unable to connect with server");
            Button errorButton = (Button) alert.getDialogPane().lookupButton(ButtonType.OK);
            errorButton.setId("serverConnectionError");
            alert.showAndWait();
clickOn("#txtLogin");
        write("user");
        clickOn("#txtPass");
        write("BBccd1234");
        clickOn("#btLogin");
        FxAssert.verifyThat("#serverConnectionError",isEnabled());
        clickOn("#serverConnectionError");
测试类

catch (ServerConnectionErrorException ex) {
            LOGGER.warning("LoginWindowController: Server connection error");
            Alert alert = new Alert(Alert.AlertType.ERROR);
            alert.setTitle("Server Error");
            alert.setContentText("Unable to connect with server");
            Label serverErrorContext = new Label();
            serverErrorContext.setText(alert.getContentText());
            serverErrorContext.setId("serverErrorContext");
            alert.showAndWait();
        }
@Test
    public void test4_ConnectionError(){
        clickOn("#txtLogin");
        write("user");
        clickOn("#txtPass");
        write("BBccd1234");
        clickOn("#btLogin");
        FxAssert.verifyThat("#serverErrorContext",LabeledMatchers.hasText("Unable to connect with server"));
        clickOn(".button");

    }
Alert alert = new Alert(Alert.AlertType.ERROR);
            alert.setTitle("Server Error");
            alert.setContentText("Unable to connect with server");
            Button errorButton = (Button) alert.getDialogPane().lookupButton(ButtonType.OK);
            errorButton.setId("serverConnectionError");
            alert.showAndWait();
clickOn("#txtLogin");
        write("user");
        clickOn("#txtPass");
        write("BBccd1234");
        clickOn("#btLogin");
        FxAssert.verifyThat("#serverConnectionError",isEnabled());
        clickOn("#serverConnectionError");

不清楚你在问什么。请提供一个。@hotzst验证显示的警报是否收到了正确的错误消息,例如,如果我尝试连接到服务器但无法建立连接,则应显示服务器错误。例如,请检查这是服务器错误,而不是用户名错误