How to test the checked state of menu items

Mar 31, 2011 at 2:52 PM

For menu items that can be check or unchecked, how do I read the current state?

Apr 1, 2011 at 6:49 AM

Hi

 

what you mean is the toggle state of the item.

there is no way for menu items in white (since 0.20 you have this for Button)

 

Use the TogglePattern of UIAutomation

 

/// <summary>
    /// Get the TogglePatern from automation element 
    /// </summary>
    /// <param name="element">The element</param>
    /// <returns></returns>
    public static TogglePattern GetToggleAutomationElement(AutomationElement element)
    {
      TogglePattern togglePattern = null;
      AutomationPattern automationPatternFromElement = GetSpecifiedPattern( element, "TogglePatternIdentifiers.Pattern" );
      if (automationPatternFromElement != null)
      {
        togglePattern = (TogglePattern)element.GetCurrentPattern(automationPatternFromElement);
      }
      else
      {
        Assert.Fail("The AutomationElement does not support this pattern");
      }
      return togglePattern;
    }

/// <summary>
    /// Gets the specified pattern.
    /// </summary>
    /// <param name="element">The element.</param>
    /// <param name="value">The value.</param>
    /// <returns></returns>
    public static AutomationPattern GetSpecifiedPattern(AutomationElement element, string value)
    {
      AutomationPattern[] supportedPattern = element.GetSupportedPatterns();

      AutomationPattern automationValuePattern = null;
      foreach (AutomationPattern pattern in supportedPattern)
      {
        if (pattern.ProgrammaticName == value)
        {
          automationValuePattern = pattern;
          break;
        }
      }
      return automationValuePattern;
    }

/// <summary>
    /// Determines whether [is UI item toggled] [the specified item].
    /// </summary>
    /// <param name="item">The item.</param>
    /// <returns>
    /// 	<c>true</c> if [is UI item toggled] [the specified item]; otherwise, <c>false</c>.
    /// </returns>
    public static bool IsUIItemToggled( UIItem item )
    {
      TogglePattern togglePattern = GetToggleAutomationElement( item.AutomationElement );
      // Return the toggle state
      ToggleState buttonState = togglePattern.Current.ToggleState;

      return ( ToggleState.On == buttonState ) ? true : false;
    }

now you can ask the UIItem if it is toggled or not

Throndorin

 

 

Apr 1, 2011 at 8:40 AM

Thanks for the detailed reply.

In my case the only pattern supported by the menu item automation element is Invoke so the Assert fires and I can't query the toggle state.  Any clues about this?

Environment: WinXP SP3; .NET 3.5 WinForms application under test; test code is .NET 4.0

Apr 1, 2011 at 8:44 AM

Hi

ok in that case the check is only an image.

Has the menu item subelements in UISpy or UIAVerify?

 

Throndorin

Apr 1, 2011 at 10:05 AM

This appears to be an issue with support for the "legacy" technology Win32 according to this MSDN article: http://msdn.microsoft.com/en-us/library/ms746680.aspx#Legacy_Issues.  Basically the UI Automation can't tell if the control supports toggling or not.  In my experiments it doesn't support the toggle pattern in the toggled on state either.  UISpy shows the patterns supported and it's only Invoke in the toggled on and toggled off state.

BTW there appears to be a shorter method for getting the checked state, using TryGetCurrentPattern: http://msdn.microsoft.com/en-us/library/ms750424.aspx

Thanks for your help.