【发布时间】:2018-10-04 04:20:16
【问题描述】:
我正在使用 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 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);
}
【问题讨论】:
-
我在 TestFX 的 Github 页面上发布了这个问题并得到了回复:github.com/TestFX/TestFX/issues/566
-
您在哪个操作系统中测试?
-
我正在使用 Linux Mint
-
我问这个问题的原因是我遇到了类似的问题。我使用的是Mac OS X,而不是
press(KeyCode.COMMAND);,我使用的是press(KeyCode.CONTROL);,但我的测试失败了,因为通过控制按钮在Mac OS上按下命令按钮允许进行多项选择。
标签: java javafx java-8 javafx-8 testfx