我正在使用 TestFX 测试ListView
使用该Ctrl
键选择多个项目,然后单击一个按钮,该按钮会生成一个警报对话框,其内容基于所选项目。
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 https://stackoverflow.com/a/48654878/8355496
* 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 https://stackoverflow.com/a/48654878/8355496
* 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);
}