我通过查找 MenuBar 来解决它,通过它的 Id 识别我想要测试的项目,因为我现在手头有 MenuItem 对象,所以我可以在它上面调用 isDisable()。
MenuTest.class
CommonTests common = new CommmonTests();
@Test
public void disabledMenuItemTest()
{
common.disabledMenuItemTest("#mainMenu", "mnEditHrom", true);
}
CommonTests.class
TestUtils utils = new TestUtils();
public void disabledMenuItemTest(String menuBarSelector, String menuItemId, boolean expected)
{
Boolean actual = utils.isMenuItemDisabled(menuBarSelector, menuItemId);
if (actual != null)
assertEquals("MenuItem "+menuItemId+" je enabled/disabled (expected = "+expected+").", expected, actual.booleanValue());
else
fail("MenuBar/MenuItem not found.");
}
TestUtils.class
public Boolean isMenuItemDisabled(String menuBarSelector, String menuItemId)
{
ArrayList<MenuItem> list = getAllMenuItems(menuBarSelector);
Boolean disabled = null;
if(list != null)
{
for(MenuItem item : list)
{
if(item.getId() != null && item.getId().equals(menuItemId))
return item.isDisable();
}
}
return disabled;
}
private ArrayList<MenuItem> getAllMenuItems(String menuBarSelector)
{
ArrayList<MenuItem> itemsList = new ArrayList<MenuItem>();
MenuBar menuBar = (MenuBar) find(menuBarSelector);
if(menuBar != null)
{
menuBar.getMenus().forEach(menu -> {
menu.getItems().forEach(menuItem -> {
itemsList.add(menuItem);
});
});
return itemsList;
}
return null;
}