Click here to Skip to main content
13,554,130 members
Click here to Skip to main content
Add your own
alternative version


8 bookmarked
Posted 28 Jun 2010
Licenced CPOL

UI Automation Interesting Challenge - Testing Automation Framework

, 28 Jun 2010
Rate this:
Please Sign up or sign in to vote.
UI Automation challenge using Menuitem, and MenuStrip control


This article will help you understand some of the interesting challenges with MenuItem, and MenuStrip control in System.Windows.Forms of Microsoft .NET Framework. Specifically, if you are creating test automation framework, this will guide you to use the right control patterns.


In 2005, Microsoft released UIA (UI Automation) as a successor to the older Microsoft Active Accessibility (MSAA) framework. This framework helps in UI accessibility. MenuStrip is the top-level container that supercedes MainMenu.

Using the Code

The below code is being used for generating Menu Items in earlier versions of .NET Framework. It is still available in the new version of Microsoft .NET Framework, but MenuStrip is the recommended way of rendering menu items.

Rendering MenuItem using MainMenu

private void GenerateMenu()
    MainMenu mainMenu = new MainMenu();
    this.Menu = mainMenu;
    //Top Level menu items
    MenuItem menuFile = new MenuItem("File");
    MenuItem menuProject = new MenuItem("Project");
    MenuItem menuHelp = new MenuItem("Help");
    mainMenu.MenuItems.AddRange(new MenuItem[] 
			{ menuFile, menuProject, menuHelp });
    //Menu item under "File" menu
    MenuItem menuNew = new MenuItem("New");
    MenuItem menuLoad = new MenuItem("Load");
    MenuItem menuExit = new MenuItem("Exit");
    menuFile.MenuItems.AddRange(new MenuItem[] { menuNew, menuLoad, menuExit });

There are three top level menu items “File”, “Project” and “Help”.

“File” menu item has three sub menu items “New”, “Load”, and “Exit”. “Project” and “Help” do not have any sub menu items.


The above screen shot is taken using UI Spy, but you can use any tool to see the control rendering.

The highlighted section shows two perspectives:

  1. MenuItem “File”: As you see in the code, there are 3 menu items that exist under “File” menu, but you don’t see because MainMenu does not render it unless you click it.
  2. ExpandCollapse Pattern: While rendering it uses expand collapse pattern instead of invoke pattern. If you are new to control patterns, please read details of UI Automation (UIAutomation) on MSDN.

You will see the comparison between this and MenuStrip below.

Rendering MenuItem using MenuStrip

The below code is being used for generating Menu Items in the newer version of .NET Framework using MenuStrip control.

private void GenerateMenu()
    MenuStrip menuStrip = new MenuStrip();
    //Top Level menu items
    ToolStripMenuItem menuFile = new ToolStripMenuItem("File");
    ToolStripMenuItem menuProject = new ToolStripMenuItem("Project");
    ToolStripMenuItem menuHelp = new ToolStripMenuItem("Help");
    menuStrip.Items.AddRange(new ToolStripMenuItem[] 
		{ menuFile, menuProject, menuHelp });
    //Menu item under "File" menu
    ToolStripMenuItem menuNew = new ToolStripMenuItem("New");
    ToolStripMenuItem menuLoad = new ToolStripMenuItem("Load");
    ToolStripMenuItem menuExit = new ToolStripMenuItem("Exit");
    menuFile.DropDownItems.AddRange(new ToolStripMenuItem[] 
		{ menuNew, menuLoad, menuExit });

This has the same structure, three menu items at the top and the first menu item has three sub menu items.


The above screen shot shows rendering of menu item using MenuStrip control. You will see that both the behaviors are different from the above rendered using the MainMenu control.

  1. MenuItem “File”: Now you see in this screenshot that “File” has three menu items under it without clicking the “File” menu item. This is one of the interesting changes or enhancement as part of the new Microsoft .NET Framework.
  2. Invoke Pattern: While rendering, it uses invoke pattern instead of ExpandCollapse Pattern.


If you are creating a test automation framework using UI Automation (UIAutomation), I would recommend using TryGetControlPattern. In this case, check for Invoke or Expand Collapse patterns.


  • 28th June, 2010: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Varun Jain 786
Software Developer (Senior)
United States United States
Sr Developer (Passionate Developer)

You may also be interested in...

Comments and Discussions

GeneralMy vote of 1 Pin
Marc Clifton29-Jun-10 18:25
protectorMarc Clifton29-Jun-10 18:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.180515.1 | Last Updated 28 Jun 2010
Article Copyright 2010 by Varun Jain 786
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid