This project is read-only.

Problems when "clicking" on menu

Jun 30, 2010 at 1:42 PM

I have a problem when trying to click on a main menu item within a test. Occasionally it works but often the menu will open very briefly and then close. I have placed asserts in the code to verify that the menu item is visible and enabled at the point when the call to Click() is made. I believe that white uses mouse automation to carry out the click. I have tried using the UIA InvokePattern to perform the click and this works consistently. Is there any reason why this might be happening? Also is there anyway to specify the method to use for clicking (i.e. invoke vs sending win32 messages). 


Menu menu = mainWindow.MainMenu.MenuItem("Project", "New");
menu.Click(); // doesn't work

var invokePattern = menu.AutomationElement.GetCurrentPattern(InvokePattern.Pattern) as InvokePattern;
invokePattern.Invoke(); // does work

Many thanks for your help. 



Jul 2, 2010 at 8:32 AM
Edited Jul 2, 2010 at 8:38 AM

I think you can have a try with the following solution: use basic UI Automation elements (shipped by .Net Framework 3.5) to access the menuItem you mentioned.

Assume that you want to check the menuItems under 'Help' in Windows Calculator, use the following code:

private void ShowMenuItemsUnderHelp()
    //Get the Calculator
    AutomationElement calc = AutomationElement.RootElement.FindFirst(TreeScope.Children,
        new PropertyCondition(AutomationElement.NameProperty, "Calculator"));

    //Get the 'Help' menu item in menubar
    AutomationElement help = calc.FindFirst(TreeScope.Descendants,
        new PropertyCondition(AutomationElement.NameProperty, "Help"));

    //Expand the 'Help' menu item

    //Find all menu items under 'Help'
    AutomationElement about = help.FindFirst(TreeScope.Descendants,
        new PropertyCondition(AutomationElement.NameProperty, "About Calculator"));

Before running this code, make sure that you opened a Windows Calculator and no window covers on it.

My experience is that using UI Automation is quite a stable solution. Is your problem solved?

Jul 2, 2010 at 10:40 PM

Thanks I appreciate the reply. Yes I can use UI Automation to "invoke" the menu item however I was wondering why the click was failing. I've seen other similar issues in White such as problems with list scroll position when selecting rows and the solution often seems to be to revert to UI Automation. I've created a number of extension methods within my own solution to deal with this and as you say I find UI Automation much more predictable and robust. 


Jul 18, 2010 at 9:12 AM

>>  Also is there anyway to specify the method to use for clicking (i.e. invoke vs sending win32 messages). 

Button class has one (RaiseClickEvent), I can add a generic one too. But I would also like to understand why you are facing this issue. Would it be possible to send some sample code for this. You said the menu closes immediately why does it close? There can be different things that come to my mind which are important here:

a) Before opening of menu there is some wait and you donot have hour glass for this.

b) The bounds of menu item is not right.

Mouse click is appropriate representation of user action hence white uses it.

Jul 18, 2010 at 9:12 AM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.