Search code examples
wpfpopupcontextmenuui-automationwhite-framework

TestStack White: Cannot find MenuItem object within a ContextMenu using AutomationID


I am using TestStack White for UI automation of a Windows WPF chat application. I can get the Add button searching by AutomationID which pops a ContextMenu with two MenuItems to select to add contact or to create a group.

When I try to get the Create group MenuItem using its automationID, it throws an exception:

TestStack.White.AutomationException: Failed to get AutomationId = mitem_createGroup

These are the methods I've tried to get the MenuItem but failed:

mainWindow.Get(SearchCriteria.ByAutomationId("mitem_createGroup")).Click();

mainWindow.MenuBar.MenuItemBy(SearchCriteria.ByAutomationId("mitem_createGroup")).Click();

mainWindow.Get<MenuBar>(SearchCriteria.ByClassName("ContextMenu"));

var createGrpBtn = mainWindow.Get(SearchCriteria.ByText("Create group"));

mainWindow.PopupMenu("Create group");

I am out of ideas and need help figuring this out.


Edit: Here is the XAML for the contextmenu and menuitems. The ContextMenu is actually that of an ImageButton

<ContextMenu StaysOpen="False" FontSize="14" Style="{DynamicResource ContextMenuStyle}">
    <MenuItem Height="32" Header="{lex:Loc STRING_MAIN_WINDOW_ADD_CONTACT}" Name="mitem_addContact" Click="mitem_addContact_Click">
        <MenuItem.Icon>
            <Image Source="{svg:SvgImage /ChatApp;component/Images/add_contact.svg}"  />
        </MenuItem.Icon>
    </MenuItem>
    <MenuItem Height="32" Header="{lex:Loc STRING_MAIN_WINDOW_CREATE_GROUP}" Name="mitem_createGroup" Click="mitem_createGroup_Click">
        <MenuItem.Icon>
            <Image Source="{svg:SvgImage /ChatApp;component/Images/add_group.svg}" />
        </MenuItem.Icon>
    </MenuItem>
</ContextMenu>

Solution

  • I came across this post on SO: Find option to right click c# White/TestStack UI Automation

    You cannot get ContextMenu simply by using Window.Get<T> and I was in the right direction when I tried to get the ContextMenu popup, but i was doing it wrong. (See in question the last method i tried).

    The SO link helped me get the solution:

    PopUpMenu popup = mainWindow.Popup;
    Menu createGroup_item = popup.ItemBy(
        SearchCriteria.ByAutomationId("mitem_createGroup")
    );
    createGroup_item.Click();