TestFx-如何测试没有ID的验证对话框 [英] TestFx - How to test validation dialogs with no ids

查看:81
本文介绍了TestFx-如何测试没有ID的验证对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有记录网格和按钮插入的应用程序。单击插入后,将有一个表单,您可以在其中填写数据,然后单击确定以将新记录添加到网格中。单击确定后,如果任何文本字段与验证规则都不匹配,则将进行验证,该验证将引发带有错误信息的对话框。如果对话框没有ID,是否可以使用textFx测试对话框上的文本?

I have an application with grid of records and button insert. After clicking insert, there is a form, where you fill in data and click Ok for adding new record to the grid. After clicking Ok, there is validation which fires dialog with error informations, if any of the text fields do not match validation rules. Is there any posible way to test text on the dialog with textFx, if the dialog has no id?

推荐答案

这是一个示例用于基于警报的对话框:

This is an example for Alert based dialog:

在测试中:

alert_dialog_has_header_and_content(
    "Removing 'Almaty' location", "Are you sure to remove this record?");

在您的助手测试课程中:

In you helper test class:

public void alert_dialog_has_header_and_content(final String expectedHeader, final String expectedContent) {
    final javafx.stage.Stage actualAlertDialog = getTopModalStage();
    assertNotNull(actualAlertDialog);

    final DialogPane dialogPane = (DialogPane) actualAlertDialog.getScene().getRoot();
    assertEquals(expectedHeader, dialogPane.getHeaderText());
    assertEquals(expectedContent, dialogPane.getContentText());
}

private javafx.stage.Stage getTopModalStage() {
    // Get a list of windows but ordered from top[0] to bottom[n] ones.
    // It is needed to get the first found modal window.
    final List<Window> allWindows = new ArrayList<>(robot.robotContext().getWindowFinder().listWindows());
    Collections.reverse(allWindows);

    return (javafx.stage.Stage) allWindows
            .stream()
            .filter(window -> window instanceof javafx.stage.Stage)
            .filter(window -> ((javafx.stage.Stage) window).getModality() == Modality.APPLICATION_MODAL)
            .findFirst()
            .orElse(null);
}

这篇关于TestFx-如何测试没有ID的验证对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆