测试在 TestFX 上通过,但不在无头模式下

Test passing on TestFX, but not in headless mode

我正在使用 TestFX 测试使用 Ctrl 键在 ListView 中选择多个项目,然后单击一个按钮,这将生成一个警告对话框,其内容基于所选项目。

press(KeyCode.CONTROL);
clickOn((Node) lookup(hasText("Item 0")).query());
verifyThat((Node) lookup(hasText("Item 0")).query(), Node::isFocused);
clickOn((Node) lookup(hasText("Item 1")).query());
verifyThat((Node) lookup(hasText("Item 1")).query(), Node::isFocused);
clickOn((Node) lookup(hasText("Item 2")).query());
verifyThat((Node) lookup(hasText("Item 2")).query(), Node::isFocused);
release(KeyCode.CONTROL);

clickOn("actionButton");
alertDialogHasHeaderAndContent("Items selected: 3"); // my own function

在头模式

测试全部通过。

无头模式

三个 verifyThat 测试都通过了,但是当它单击按钮并验证生成的警报对话框是否正确时,它却不正确;生成的警报就好像只从列表中选择了一项。

结果是:

org.junit.ComparisonFailure: 
Expected :Items selected: 3
Actual   :Items selected: 1

这里 alertDialogHasHeaderAndContent 为了完整起见:

/**
 * Checks the current alert dialog displayed (on the top of the window stack) has the expected contents.
 *
 * From 
 * Licenced under cc by-sa 3.0 with attribution required https://creativecommons.org/licenses/by-sa/3.0/
 * @param expectedHeader Expected header of the dialog
 * @param expectedContent Expected content of the dialog
 */
private void alertDialogHasHeaderAndContent(final String expectedHeader, final String expectedContent) {
    final Stage actualAlertDialog = getTopModalStage();
    assertNotNull(actualAlertDialog);

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

/**
 * Get the top modal window.
 *
 * Adapted from 
 * Licenced under cc by-sa 3.0 with attribution required https://creativecommons.org/licenses/by-sa/3.0/
 * @return the top modal window
 */
private 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<>(new FxRobot().robotContext().getWindowFinder().listWindows());
    Collections.reverse(allWindows);

    return (Stage) allWindows
            .stream()
            .filter(window -> window instanceof Stage)
            .findFirst()
            .orElse(null);
}

这是 Monocole 的问题。有关详细信息,请参阅 https://github.com/TestFX/TestFX/issues/566