3

Selecting a checkbox present in a grid using UIAutomation. Below code returns invalidpattern for invoke pattern:

AutomationElement mainGrid = appElement1.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.AutomationIdProperty, "ReadinessTestList"));
// find the grid in the window 
if (mainGrid != null)
 {

    MessageBox.Show("inside the grid");

    // select just the first cell 
    var item = mainGridPattern.GetItem(0, 0);
    MessageBox.Show(Convert.ToString(item));
    //item.SetFocus();

    AutomationElement FirstCheckBox = GetTextElement(item, "SystemNameCheckBox");

    if (FirstCheckBox != null)
    {
        MessageBox.Show(Convert.ToString(FirstCheckBox));
        TogglePattern SelectedFirstCheckBox = FirstCheckBox.GetCurrentPattern(TogglePattern.Pattern) as TogglePattern;
        MessageBox.Show(Convert.ToString(SelectedFirstCheckBox));

        ToggleState FirstCheckBxState = SelectedFirstCheckBox.Current.ToggleState;
        string try2 = Convert.ToString(FirstCheckBxState);
        MessageBox.Show(try2);

        //FirstCheckBxState.On;

        if (FirstCheckBxState != ToggleState.On) // not on? click it
        {            
            InvokePattern invokefirstCheckBox = (InvokePattern)FirstCheckBox.GetCurrentPattern(InvokePattern.Pattern);
            Thread.Sleep(2000);
            invokefirstCheckBox.Invoke();
        }

    }
Vasily Ryabov
  • 9,386
  • 6
  • 25
  • 78
Archana DV
  • 31
  • 2

1 Answers1

0

You should use the toggle pattern instead of the invoke pattern for a checkbox.

 TogglePattern tpToggle = (TogglePattern)aeElement.GetCurrentPattern(TogglePattern.Pattern);
 tpToggle.Toggle();

Also you should use inspect.exe which can be found at C:\Program Files (x86)\Windows Kits\8.1\bin\x86 after installing windows development kit. This tool will allow you to see the supported patterns of what you are attempting to automate.

Max Young
  • 1,522
  • 1
  • 16
  • 42