Navigating menus in automation tests I am trying to write a method that can find the child item in a menu. The below code works but is not exactly elegant or scaleable. Is there another way to find the path to child menu item that could be used?

public class Item
{
    public string Name { get; set; }
    public string Parent { get; set; } 
}


public void OpenMenuItem(string name)
{
    var items = new List&lt;Item&gt;
    {
        new Item  { Name = "Test", Parent = null },
        new Item  { Name = "TestChild1", Parent = "Test" },
        new Item  { Name = "TestChild2", Parent = "Test" },
        new Item  { Name = "TestGranChild1", Parent = "TestChild1" },
        new Item  { Name = "TestGranChild2", Parent = "TestChild1" },
        new Item  { Name = "TestGranChild3", Parent = "TestChild2" },
        new Item  { Name = "TestGranChild4", Parent = "TestChild2" }
    };
    var menuItem = items.Where(x =&gt; x.Name == name);
    if (menuItem.FirstOrDefault().Parent != null)
    {
        var parent1 = items.Where(x =&gt; x.Name == menuItem.FirstOrDefault().Parent);
        if (parent1.FirstOrDefault().Parent != null)
        {
            var parent2 = items.Where(x =&gt; x.Name == parent1.FirstOrDefault().Parent);
            app.Tap(x =&gt; x.Marked(parent2.FirstOrDefault().Name));
        }
        app.Tap(x =&gt; x.Marked(parent1.FirstOrDefault().Name));
    }
    app.Tap(x =&gt; x.Marked(menuItem.FirstOrDefault().Name));
}
Make parent a list of ancestors as a path to the item: 

new Item  { Name = "TestGranChild4", Path = {"Test", "TestChild2"} },


var menuItem = items.Where(x =&gt; x.Name == name);
menuItem.FirstOrDefault().Path.ForEach(a =&gt; app.tap(x=&gt;x.Marked(a)));
app.Tap(x =&gt; x.Marked(menuItem.FirstOrDefault().Name));