Search code examples
swtbot

how to call to context menu in swtbot


I want to call to context menu in my application.

The issue that I don't have any items in the tree.

I active my view and then I want to open context menu.

 SWTBotView view = bot.viewByTitle("Project Explorer");

 view.bot.tree().contextMenu("New").click();

then I got error message

Could you please advise me how I can open contextMenu without any item in the tree ?


Solution

  • As there is no direct way to do this. I assume you have a shortcut for opening your context menu.

    bot.activeShell().pressShortcut(<your shortcut>);
    
    bot.waitUntil(new ContextMenuAppears(tree,
                    "New"));
    tree.contextMenu("New").click();
    

    Where ContextMenuAppears is an ICondition which waits for the desired context menu to appear.

    public class ContextMenuAppears implements ICondition {
    
    private SWTBotTree swtBotTree;
    private String mMenuText;
    public TekstContextMenuAppears(SWTBotTree pSwtBotTree, String menuText) {
        swtBotTree = pSwtBotTree;
        mMenuText = menuText;
    }
    
    @Override
    public boolean test() throws Exception {
        try {           
            return swtBotTree.contextMenu(mMenuText).isVisible();   
        } catch (WidgetNotFoundException e) {
            return false;
        }
    
    }
    
    @Override
    public void init(SWTBot bot) {
        // TODO Auto-generated method stub
    
    }
    
    @Override
    public String getFailureMessage() {
        // TODO Auto-generated method stub
        return null;
    }
    
    }