javafxmenuitemtestfx

TestFx - How to test javaFx MenuItems


Since MenuItem is not a Node, I'm not able to look it up. How do I test, if some MenuItem is disabled? I've tried to look it up as it was a node and it returned me something, which looks like this..

(toString representation of returned object):

(ContextMenuContent$MenuItemContainer[id=mnEditHrom, styleClass=menu-item])

But i can't cast MenuItem on that, it says "Node cannot be converted to MenuItem" and when I call isDisabled() function on what was returned, i get incorrect information.

Lets say I have MenuItem with "mnEdit" id, which is disabled. When i call

find("#mnEdit").isDisabled();

it returns false. Find method looks like this:

public <T extends Node> T find(String query) 
{
  return (T) lookup(query).queryAll().iterator().next();
} 

So again, how do I test whether some MenuItem is disabled or not in testFx?


Solution

  • You almost done in the original post. When you get the MenuItemContainer get the MenuItem firstly and finally call isDisable():

    ContextMenuContent.MenuItemContainer actualMenuItemContainer = find("#mnEdit");
    
    boolean actualResult = actualMenuItemContainer.getItem().isDisable();