TryGetCurrentPattern() public method

public TryGetCurrentPattern ( AutomationPattern pattern, object &patternObject ) : bool
pattern AutomationPattern
patternObject object
return bool
Ejemplo n.º 1
0
 public static bool CanExpandElement(this AutomationElement el, ControlType elementType)
 {
     if (elementType == ControlType.SplitButton || elementType == ControlType.Button || elementType == ControlType.MenuItem)
     {
         object ecPattern;
         if (el.TryGetCurrentPattern(LegacyIAccessiblePattern.Pattern, out ecPattern))
         {
             uint state = ((LegacyIAccessiblePattern)ecPattern).Current.State;
             if ((state & 0x40000000) != 0)
             {
                 return(true);
             }
         }
     }
     else if (elementType == ControlType.ComboBox)
     {
         object ecPattern;
         if (el.TryGetCurrentPattern(LegacyIAccessiblePattern.Pattern, out ecPattern))
         {
             uint state = ((LegacyIAccessiblePattern)ecPattern).Current.State;
             if ((state & 0x400) != 0 || (state & 0x200) != 0)
             {
                 return(true);
             }
         }
         var buttonCondition = new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Button);
         var button          = el.FindFirst(TreeScope.Children, buttonCondition);
         if (button != null)
         {
             return(true);
         }
     }
     return(false);
 }
Ejemplo n.º 2
0
        public static bool TryDoDefaultAction(this AutomationElement el)
        {
            if (!el.Current.IsEnabled)
            {
                return(false);
            }
            object invoke;

            if (el.TryGetCurrentPattern(InvokePattern.Pattern, out invoke))
            {
                ((InvokePattern)invoke).Invoke();
                return(true);
            }

            object legacyPattern;

            if (el.TryGetCurrentPattern(LegacyIAccessiblePattern.Pattern, out legacyPattern))
            {
                LegacyIAccessiblePattern legacy = (LegacyIAccessiblePattern)legacyPattern;
                if (legacy.Current.DefaultAction != "")
                {
                    legacy.DoDefaultAction();
                }
                return(true);
            }

            return(false);
        }
Ejemplo n.º 3
0
        public static T GetPattern <T>(this AutomationElement el, AutomationPattern pattern) where T : BasePattern
        {
            Object obj;

            el.TryGetCurrentPattern(pattern, out obj);
            return((T)obj);
        }
Ejemplo n.º 4
0
 protected object TryGetPattern(AutomationPattern pattern,AutomationElement elementNeedToGet = null)
 {
     elementNeedToGet = elementNeedToGet ?? this.self;
     object returnPattern;
     elementNeedToGet.TryGetCurrentPattern(pattern, out returnPattern);
     return returnPattern ?? null;
 }
Ejemplo n.º 5
0
 //获取选择框元素
 public static SelectionItemPattern GetSelectionItemPattern(AutomationElement element)
 {
     object currentPattern;
        if (!element.TryGetCurrentPattern(SelectionItemPattern.Pattern, out currentPattern))
        {
        throw new Exception(string.Format("Element with AutomationId '{0}' and Name '{1}' does not support the SelectionItemPattern.", element.Current.AutomationId, element.Current.Name));
        }
        return currentPattern as SelectionItemPattern;
 }
Ejemplo n.º 6
0
        public static bool TryCollapse(this AutomationElement el)
        {
            object expand;

            if (el.TryGetCurrentPattern(ExpandCollapsePattern.Pattern, out expand))
            {
                ((ExpandCollapsePattern)expand).Collapse();
                return(true);
            }
            return(false);
        }
Ejemplo n.º 7
0
 public static bool SelectOne(AutoElem e)
 {
     if (e.TryGetCurrentPattern(SelectionItemPattern.Pattern, out object obj))
     {
         var Pattern = obj as SelectionItemPattern;
         Pattern.Select();
         return(true);
     }
     else
     {
         return(false);
     }
 }
Ejemplo n.º 8
0
        public static bool IsSelected(this AutomationElement el)
        {
            object legacyPattern;

            if (el.TryGetCurrentPattern(LegacyIAccessiblePattern.Pattern, out legacyPattern))
            {
                if ((((LegacyIAccessiblePattern)legacyPattern).Current.State & 2) == 0)
                {
                    return(false);
                }
            }
            return(true);
        }
        private static TogglePattern ValidateControlForTogglePattern(AutomationElement element)
        {
            object togglePattern = null;
            bool isValid = element.TryGetCurrentPattern(TogglePattern.Pattern, out togglePattern);

            if (isValid)
            {
                return (TogglePattern)togglePattern;
            }
            else
            {
                throw new InvalidOperationException("Invalid operation");
            }
        }
        private static ValuePattern ValidateTextControl(AutomationElement element)
        {
            object valPattern = null;
            bool isValid = element.TryGetCurrentPattern(ValuePattern.Pattern,out valPattern);

            if (isValid)
            {
                return (ValuePattern)valPattern;
            }
            else
            {
                throw new InvalidOperationException("Invalid operation");
            }
        }
        private static ExpandCollapsePattern ValidateExpandCollapseControl(AutomationElement element)
        {
            object expColPattern = null;
            bool isValid = element.TryGetCurrentPattern(ExpandCollapsePattern.Pattern, out expColPattern);

            if (isValid)
            {
                return (ExpandCollapsePattern)expColPattern;
            }
            else
            {
                throw new InvalidOperationException("Invalid operation");
            }
        }
Ejemplo n.º 12
0
 public static bool Dock(AutoElem e, DockPosition Pos)
 {
     if (e.TryGetCurrentPattern(DockPattern.Pattern, out object obj))
     {
         var Pattern = obj as DockPattern;
         Pattern.SetDockPosition(Pos);
         Console.WriteLine("docked");
         return(true);
     }
     else
     {
         return(false);
     }
 }
        private static SelectionItemPattern ValidateControlForSelectionPattern(AutomationElement element)
        {
            object selPattern = null;
            bool isValid = element.TryGetCurrentPattern(SelectionItemPattern.Pattern, out selPattern);

            if (isValid)
            {
                return (SelectionItemPattern)selPattern;
            }
            else
            {
                throw new InvalidOperationException("Invalid operation");
            }
        }
Ejemplo n.º 14
0
 public static bool Toggle(AutoElem e)
 {
     if (e.TryGetCurrentPattern(TogglePattern.Pattern, out object obj))
     {
         var Pattern = obj as TogglePattern;
         Pattern.Toggle();
         Console.WriteLine("toggled");
         return(true);
     }
     else
     {
         return(Expand(e));
     }
 }
Ejemplo n.º 15
0
 public static bool Invoke(AutoElem e)
 {
     if (e.TryGetCurrentPattern(InvokePattern.Pattern, out object obj))
     {
         InvokePattern Pattern = obj as InvokePattern;
         Pattern.Invoke();
         Console.WriteLine("invoked");
         return(true);
     }
     else
     {
         return(false);
     }
 }
Ejemplo n.º 16
0
 public static bool ScrollTo(AutoElem e)
 {
     if (e.TryGetCurrentPattern(ScrollItemPattern.Pattern, out object obj))
     {
         ScrollItemPattern Pattern = obj as ScrollItemPattern;
         Pattern.ScrollIntoView();
         Console.WriteLine("scrolled to");
         return(true);
     }
     else
     {
         return(false);
     }
 }
        private static InvokePattern ValidateButtonControl(AutomationElement element)
        {
            object invPattern = null;
            bool isValid = element.TryGetCurrentPattern(InvokePattern.Pattern,out invPattern);

            if (isValid)
            {
                return (InvokePattern)invPattern;
            }
            else
            {
                throw new InvalidOperationException("Invalid operation");
            }
        }
Ejemplo n.º 18
0
        public static void TrySetFocusLegacy(this AutomationElement element)
        {
            object selectionItemPattern;

            if (element.TryGetCurrentPattern(LegacyIAccessiblePattern.Pattern, out selectionItemPattern))
            {
                try
                {
                    ((LegacyIAccessiblePattern)selectionItemPattern).Select((int)AccessibleSelection.TakeSelection);
                }
                catch
                {
                }
            }
        }
Ejemplo n.º 19
0
        public static void ScrollIntoView(this AutomationElement el)
        {
            object scrollItemPattern;

            if (el.TryGetCurrentPattern(ScrollItemPattern.Pattern, out scrollItemPattern))
            {
                // デスクトップの検索時に Exception になるため try catch
                try
                {
                    ((ScrollItemPattern)scrollItemPattern).ScrollIntoView();
                }
                catch
                {
                }
            }
        }
Ejemplo n.º 20
0
        public static bool IsGroup(this AutomationElement el)
        {
            if (el.Current.ClassName == "TGroupBox")
            {
                return(true);
            }
            object legacyPattern;

            if (el.TryGetCurrentPattern(LegacyIAccessiblePattern.Pattern, out legacyPattern))
            {
                if (((LegacyIAccessiblePattern)legacyPattern).Current.Role == 0x14)
                {
                    return(true);
                }
            }
            return(false);
        }
Ejemplo n.º 21
0
        public static bool TrySelectItem(this AutomationElement el)
        {
            object selectionItem;

            if (el.TryGetCurrentPattern(SelectionItemPattern.Pattern, out selectionItem))
            {
                try
                {
                    SelectionItemPattern item = (SelectionItemPattern)selectionItem;
                    item.Select();
                    return(true);
                }
                catch
                {
                    return(false);
                }
            }
            return(false);
        }
Ejemplo n.º 22
0
 public static bool Select(AutoElem e)
 {
     if (e.TryGetCurrentPattern(SelectionItemPattern.Pattern, out object obj))
     {
         var Pattern = obj as SelectionItemPattern;
         if (Pattern.Current.IsSelected)
         {
             Pattern.RemoveFromSelection();
         }
         else
         {
             Pattern.AddToSelection();
         }
         return(true);
     }
     else
     {
         return(false);
     }
 }
Ejemplo n.º 23
0
 public static bool Scroll(AutoElem e,
                           ScrollAmount Amount, bool HScroll = false)
 {
     if (e.TryGetCurrentPattern(ScrollPattern.Pattern, out object obj))
     {
         var Pattern = obj as ScrollPattern;
         if (HScroll)
         {
             Pattern.ScrollHorizontal(Amount);
             Console.WriteLine("Hscrolled");
         }
         else
         {
             Pattern.ScrollVertical(Amount);
             Console.WriteLine("Vscrolled");
         }
         return(true);
     }
     else
     {
         return(false);
     }
 }
Ejemplo n.º 24
0
 public static bool ScrollPercent(AutoElem e,
                                  double Percent, bool HScroll = false)
 {
     if (e.TryGetCurrentPattern(ScrollPattern.Pattern, out object obj))
     {
         var Pattern = obj as ScrollPattern;
         if (HScroll)
         {
             Pattern.SetScrollPercent(Percent, -1);
             Console.WriteLine("Hpercented");
         }
         else
         {
             Pattern.SetScrollPercent(-1, Percent);
             Console.WriteLine("Vpercented");
         }
         return(true);
     }
     else
     {
         return(false);
     }
 }
Ejemplo n.º 25
0
 private int IsMenuChecked(AutomationElement menuHandle)
 {
     if (menuHandle == null)
     {
         LogMessage("Invalid menu handle");
         return 0;
     }
     Object pattern = null;
     if (menuHandle.TryGetCurrentPattern(LegacyIAccessiblePattern.Pattern,
                                 out pattern))
     {
         int isChecked;
         uint state = ((LegacyIAccessiblePattern)pattern).Current.State;
         // Use fifth bit of current state to determine menu item is checked or not checked
         isChecked = (state & 16) == 16 ? 1 : 0;
         LogMessage("IsMenuChecked: " + menuHandle.Current.Name + " : " + "Checked: " +
             isChecked + " : " + "Current State: " + state);
         pattern = null;
         return isChecked;
     }
     else
         LogMessage("Unable to get LegacyIAccessiblePattern");
     return 0;
 }
Ejemplo n.º 26
0
 public static bool Expand(AutoElem e)
 {
     if (e.TryGetCurrentPattern(ExpandCollapsePattern.Pattern, out object obj))
     {
         var Pattern = obj as ExpandCollapsePattern;
         var state   = Pattern.Current.ExpandCollapseState;
         if (ExpandCollapseState.Collapsed == state ||
             ExpandCollapseState.PartiallyExpanded == state)
         {
             Pattern.Expand();
             Console.WriteLine("expanded");
         }
         else if (ExpandCollapseState.Expanded == state)
         {
             Pattern.Collapse();
             Console.WriteLine("collapsed");
         }
         return(true);
     }
     else
     {
         return(false);
     }
 }
Ejemplo n.º 27
0
        /// -------------------------------------------------------------------
        /// <summary></summary>
        /// -------------------------------------------------------------------
        private void TS_CloseWindow(AutomationElement element, CheckType checkType)
        {
            object wpObject = null;
            WindowPattern wp = null;

            if (true == element.TryGetCurrentPattern(WindowPattern.Pattern, out wpObject))
            {
                wp = wpObject as WindowPattern;

                if (wp == null)
                    ThrowMe(checkType, "Could not find the WindowPattern");

                wp.Close();
                Comment("Called WindowPattern.Close() on the element");
            }
            m_TestStep++;
        }
Ejemplo n.º 28
0
        public static void AutomationElementTryGetCurrentPattern(AutomationElement element)
        {
            ArrayList automationProperties = null;
            ArrayList automationPatterns = null;
            Random rnd = new Random((int)DateTime.Now.Ticks);
            PopulateAutomationElementProperties(ref automationProperties, ref automationPatterns);
            AutomationPattern pattern = (AutomationPattern)automationPatterns[rnd.Next(automationPatterns.Count)];
            object retPattern = null;

            Dump("TryGetCurrentPattern(" + pattern + ")", true, element);
            try
            {
                object val = element.TryGetCurrentPattern(pattern, out retPattern);
            }
            catch (Exception exception)
            {
                VerifyException(element, exception, typeof(ElementNotAvailableException));
            }
        }
Ejemplo n.º 29
0
        /// <summary>
        /// Tracks any changes in the target text window. Changes could be due
        /// to editing or even just a cursor movement.  Uses Windows automation to
        /// track editing changes and cursor movements
        /// </summary>
        /// <param name="handleMainWindow">Active target window</param>
        /// <param name="textElement">The text control</param>
        /// <returns>true on success</returns>
        private bool trackTextChanges(IntPtr handleMainWindow, AutomationElement textElement)
        {
            bool retVal = true;

            Log.Debug();

            try
            {
                object objPattern;
                if (textElement.TryGetCurrentPattern(TextPattern.Pattern, out objPattern))
                {
                    int nativeHandle = textElement.Current.NativeWindowHandle;
                    if (nativeHandle != 0)
                    {
                        _handleTextWindow = new IntPtr(nativeHandle);
                    }

                    AutomationEventManager.RemoveAutomationEventHandler(handleMainWindow,
                                                    TextPattern.TextSelectionChangedEvent,
                                                    textElement);
                    Log.Debug("Adding onTextChanged event handler");
                    AutomationEventManager.AddAutomationEventHandler(handleMainWindow,
                                                    TextPattern.TextSelectionChangedEvent,
                                                    textElement,
                                                    onTextChanged);

                    if (nativeHandle == 0)
                    {
                        Log.Debug("handle is zero");
                        retVal = false;
                    }
                }
                else
                {
                    Log.Debug("Focused element does not support textpattern");
                    retVal = false;
                }
            }
            catch (Exception ex)
            {
                // exception can be thrown by AddAutomationEventHandler to the effect that
                // WindowClosed event can only be attached to top level windows.
                // For instance, the "Start" menu would throw this exception.
                Log.Debug(ex.ToString());
                retVal = false;
            }

            if (!retVal)
            {
                _handleTextWindow = IntPtr.Zero;
            }

            return retVal;
        }
        /// <summary>
        /// Select datagrid row and ensure visible after select
        /// </summary>
        /// <param name="el"></param>
        /// <param name="gridValueName"></param>
        /// <param name="nameCol"></param>
        /// <exception cref="ApplicationException">Element does not support GridPattern</exception>
        /// <exception cref="ApplicationException">Specified row not found</exception>
        /// <returns>Selected row element</returns>
        public static AutomationElement SelectGridRow(AutomationElement el, string gridValueName, int nameCol)
        {
            //TODO: Improve current brittleness here when used with custom control SortedListView
            //Doesn't always find item depending on where it is in the list
            //Using a backward search for now since that works with the current [email protected] / DemoUrl data set

            object gridPatternObj;
            if (!el.TryGetCurrentPattern(GridPattern.Pattern, out gridPatternObj))
            {
                throw new ApplicationException("Specified element '" + UIAUtility.GetIdOrName(el) + "' does not support GridPattern");
            }

            var gridPattern = (GridPattern) gridPatternObj;
            
            AutomationElement matchRow = null;

            for (int i = gridPattern.Current.RowCount - 1; i >= 0; i--)
            {
                var elem = gridPattern.GetItem(i, nameCol);
                if (elem == null)
                {
                    elem = gridPattern.GetItem(i, nameCol);
                }
                if (elem != null)
                {
                    if (elem.Current.Name == gridValueName)
                    {
                        var tw = TreeWalker.ContentViewWalker;
                        matchRow = tw.GetParent(elem);
                        break;
                    }
                }
            }

            if (matchRow == null)
            {
                throw new ApplicationException("Did not find specified row '" + gridValueName + "'");
            }

            WaitForElementEnabledWithTimeout(matchRow, AddinTestUtility.DialogControlEventStateUpdateTimeout);

            //Select grid row
            object selectionPattern;
            if ( ! matchRow.TryGetCurrentPattern(SelectionItemPattern.Pattern, out selectionPattern))
            {
                throw new ApplicationException("Specified element '" + UIAUtility.GetIdOrName(matchRow) + "' does not support SelectionItemPattern");
            }
            ((SelectionItemPattern) selectionPattern).Select();

            return matchRow;
        }
        public static string GetText(AutomationElement element)
        {
            object textPattern = null;

            if (!element.TryGetCurrentPattern(
                TextPattern.Pattern, out textPattern))
            {
                throw new InvalidOperationException(
                        "The control with an AutomationID of "
                        + element.Current.AutomationId.ToString()
                        + " does not support TextPattern.");
            }

            return ((TextPattern)textPattern).DocumentRange.GetText(-1);
        }
Ejemplo n.º 32
0
 private bool SelectListItem(AutomationElement element, String itemText,
     bool verify = false)
 {
     if (element == null || String.IsNullOrEmpty(itemText))
     {
         throw new XmlRpcFaultException(123,
             "Argument cannot be null or empty.");
     }
     LogMessage("SelectListItem Element: " + element.Current.Name +
         " - Type: " + element.Current.ControlType.ProgrammaticName);
     Object pattern = null;
     AutomationElement elementItem;
     try
     {
         elementItem = utils.GetObjectHandle(element, itemText);
         if (elementItem != null)
         {
             LogMessage(elementItem.Current.Name + " : " +
                 elementItem.Current.ControlType.ProgrammaticName);
             if (verify)
             {
                 bool status = false;
                 if (elementItem.TryGetCurrentPattern(SelectionItemPattern.Pattern,
                     out pattern))
                 {
                     status = ((SelectionItemPattern)pattern).Current.IsSelected;
                 }
                 if (element.TryGetCurrentPattern(ExpandCollapsePattern.Pattern,
                     out pattern))
                 {
                     LogMessage("ExpandCollapsePattern");
                     element.SetFocus();
                     ((ExpandCollapsePattern)pattern).Collapse();
                 }
                 return status;
             }
             if (elementItem.TryGetCurrentPattern(ScrollItemPattern.Pattern,
                 out pattern))
             {
                 LogMessage("ScrollItemPattern");
                 ((ScrollItemPattern)pattern).ScrollIntoView();
             }
             if (elementItem.TryGetCurrentPattern(SelectionItemPattern.Pattern,
                 out pattern))
             {
                 LogMessage("SelectionItemPattern");
                 //((SelectionItemPattern)pattern).Select();
                 // NOTE: Work around, as the above doesn't seem to work
                 // with UIAComWrapper and UIAComWrapper is required
                 // to Edit value in Spin control
                 return utils.InternalClick(elementItem);
             }
             else if (elementItem.TryGetCurrentPattern(ExpandCollapsePattern.Pattern,
                 out pattern))
             {
                 LogMessage("ExpandCollapsePattern");
                 ((ExpandCollapsePattern)pattern).Expand();
                 element.SetFocus();
                 return true;
             }
             else
             {
                 throw new XmlRpcFaultException(123,
                     "Unsupported pattern.");
             }
         }
     }
     catch (Exception ex)
     {
         LogMessage(ex);
         if (ex is XmlRpcFaultException)
             throw;
         else
             throw new XmlRpcFaultException(123,
                 "Unhandled exception: " + ex.Message);
     }
     finally
     {
         pattern = null;
         elementItem = null;
     }
     throw new XmlRpcFaultException(123,
         "Unable to find item in the list: " + itemText);
 }
        /// <summary>
        /// Tries to get the invoke pattern (if available)
        /// </summary>
        /// <param name="automationElement">The automation element to get the pattern from</param>
        /// <param name="pattern">The pattern that was retrieved</param>
        /// <returns>True if the pattern was retrieved, false otherwise</returns>
        private bool TryGetInvokePattern(AutomationElement automationElement, out InvokePattern pattern)
        {
            object invokePattern;
            if(automationElement.TryGetCurrentPattern(InvokePattern.Pattern, out invokePattern))
            {
                pattern = invokePattern as InvokePattern;
                return pattern != null;
            }

            pattern = null;
            return false;
        }
Ejemplo n.º 34
0
        /// <summary>
        /// Inserts the specified string value into the control
        /// referenced by the element
        /// </summary>
        /// <param name="element">the element into which to insert text</param>
        /// <param name="value">text to insert</param>
        public static void InsertTextIntoElement(AutomationElement element, string value)
        {
            if (element == null || value == null)
            {
                return;
            }

            try
            {
                if (!element.Current.IsEnabled || !element.Current.IsKeyboardFocusable)
                {
                    Log.Debug("Control not enabled or keyboard focusable. AutomationID " + element.Current.AutomationId);
                    return;
                }

                object valuePattern;
                if (!element.TryGetCurrentPattern(ValuePattern.Pattern, out valuePattern))
                {
                    element.SetFocus();
                    SendKeys.SendWait(value);
                }
                else
                {
                    element.SetFocus();
                    ((ValuePattern)valuePattern).SetValue(value);
                }
            }
            catch (Exception ex)
            {
                Log.Debug(ex.ToString());
            }
        }
Ejemplo n.º 35
0
        /// <summary>
        ///     Gets a specified control pattern.
        /// </summary>
        /// <param name="ae">
        ///     The automation element we want to obtain the control pattern from.
        /// </param>
        /// <param name="ap">The control pattern of interest.</param>
        /// <returns>A ControlPattern object.</returns>
        private object GetControlPattern(
            AutomationElement ae, AutomationPattern ap)
        {
            object oPattern = null;

            if (false == ae.TryGetCurrentPattern(ap, out oPattern))
            {
                Feedback("Object does not support the " +
                         ap.ProgrammaticName + " Pattern");
                return null;
            }

            Feedback("Object supports the " +
                     ap.ProgrammaticName + " Pattern.");

            return oPattern;
        }
Ejemplo n.º 36
0
        private static void VerifyValuePattern (AutomationElement element)
        {
            object patternObj;
            if (!element.TryGetCurrentPattern (ValuePattern.Pattern, out patternObj))
                return;

            ValuePattern pattern = patternObj as ValuePattern;
            try {
                if (!pattern.Current.IsReadOnly) {
                    string oldValue = pattern.Current.Value;
                    pattern.SetValue ("hello world!");
                    // This test fails and confirms that we should implement IValueProvider
                    // but return IsReadOnly = true
                    Assert.AreEqual ("hello world!", pattern.Current.Value, "Value not set even when IsReadOnly is false");
                }
            // This is weird, it should not throw any excepiont but: 
            // ElementNotEnabledException, ArgumentException or InvalidDataException
            } catch (System.Runtime.InteropServices.COMException) { }
        }
 public SelectionItemPattern GetSelectionItemPattern(AutomationElement element, AutomationProperty property, object value, TreeScope searchScope)
 {
     AutomationElement aeSelectionPattern;
     int numWaits = 0;
     do
     {
         aeSelectionPattern = GetFirstChildNode(element, property, value, searchScope);
         ++numWaits;
         Thread.Sleep(300);
     } while (aeSelectionPattern == null && numWaits < 75);
     object objPattern;
     SelectionItemPattern selectionItemPattern;
     if (true == element.TryGetCurrentPattern(SelectionItemPattern.Pattern, out objPattern))
     {
         selectionItemPattern = objPattern as SelectionItemPattern;
         return selectionItemPattern;
     }
     else
         return null;
 }
 public SelectionItemPattern GetSelectionItemWithoutWait(AutomationElement element, AutomationProperty property, object value, TreeScope searchScope)
 {
     try
     {
         AutomationElement aeExpanderElement = GetFirstChildNode(element, property, value, searchScope);
         if (aeExpanderElement == null)
             throw new ElementNotAvailableException("Expander Element not available. Try the GetSelectionItemPattern which has a wait");
         object objPattern;
         SelectionItemPattern togPattern;
         if (true == element.TryGetCurrentPattern(SelectionItemPattern.Pattern, out objPattern))
         {
             togPattern = objPattern as SelectionItemPattern;
             return togPattern;
         }
         else
             return null;
     }
     catch
     {
         return null;
     }
 }
Ejemplo n.º 39
0
 internal static BasePattern Pattern(AutomationElement automationElement, AutomationPattern pattern)
 {
     object patternObject;
     if (automationElement.TryGetCurrentPattern(pattern, out patternObject))
     {
         return (BasePattern) patternObject;
     }
     return null;
 }
Ejemplo n.º 40
0
 private bool IsElementSelected(AutomationElement element)
 {
     object pattern;
     if (element.TryGetCurrentPattern(TogglePattern.Pattern, out pattern))
     {
         var state = ((TogglePattern)pattern).Current.ToggleState;
         return state != ToggleState.Off;
     }
     else
     {
         return false;
     }
 }
        /// <summary> 
        /// Inserts a string into textbox control
        /// </summary> 
        /// <param name="element">A text control.</param>
        /// <param name="value">The string to be inserted.</param>
        public static void InsertText(AutomationElement element,
                                            string value)
        {
            // Validate arguments / initial setup 
            if (value == null)
                throw new ArgumentNullException(
                    "String parameter must not be null.");

            if (element == null)
                throw new ArgumentNullException(
                    "AutomationElement parameter must not be null");

            // A series of basic checks prior to attempting an insertion. 
            // 
            // Check #1: Is control enabled? 
            // An alternative to testing for static or read-only controls  
            // is to filter using  
            // PropertyCondition(AutomationElement.IsEnabledProperty, true)  
            // and exclude all read-only text controls from the collection. 
            if ( ! element.Current.IsEnabled)
            {
                throw new InvalidOperationException(
                    "The control with an AutomationID of "
                    + element.Current.AutomationId.ToString()
                    + " is not enabled");
            }

            // Once you have an instance of an AutomationElement,   
            // check if it supports the ValuePattern pattern. 
            object valuePattern = null;

            // Control does not support the ValuePattern pattern  
            // so use keyboard input to insert content. 
            // 
            // NOTE: Elements that support TextPattern  
            //       do not support ValuePattern and TextPattern 
            //       does not support setting the text of  
            //       multi-line edit or document controls. 
            //       For this reason, text input must be simulated 
            //       using one of the following methods. 
            //        
            if (!element.TryGetCurrentPattern(
                ValuePattern.Pattern, out valuePattern))
            {
                throw new InvalidOperationException(
                        "The control with an AutomationID of "
                        + element.Current.AutomationId.ToString()
                        + " does not support ValuePattern.");
            }

            // Control supports the ValuePattern pattern so we can  
            // use the SetValue method to insert content. 
            // Set focus for input functionality and begin.
            element.SetFocus();

            ((ValuePattern)valuePattern).SetValue(value);
        }
Ejemplo n.º 42
0
		protected bool SupportsPattern (AutomationElement element, AutomationPattern pattern)
		{
			object rtnPattern;
			return element.TryGetCurrentPattern (pattern, out rtnPattern);
		}
Ejemplo n.º 43
0
        /// --------------------------------------------------------------------
        /// <summary>
        ///     Inserts a string into each text control of interest.
        /// </summary>
        /// <param name="element">A text control.</param>
        /// <param name="value">The string to be inserted.</param>
        /// --------------------------------------------------------------------
        private void InsertTextUsingUiAutomation(AutomationElement element,
            string value)
        {
            try
            {
                // Validate arguments / initial setup
                if (value == null)
                    throw new ArgumentNullException(
                        "String parameter must not be null.");

                if (element == null)
                    throw new ArgumentNullException(
                        "AutomationElement parameter must not be null");

                // A series of basic checks prior to attempting an insertion.
                //
                // Check #1: Is control enabled?
                // An alternative to testing for static or read-only controls 
                // is to filter using 
                // PropertyCondition(AutomationElement.IsEnabledProperty, true) 
                // and exclude all read-only text controls from the collection.
                if (!element.Current.IsEnabled)
                {
                    throw new InvalidOperationException(
                        "The control with an AutomationID of "
                        + element.Current.AutomationId
                        + " is not enabled.\n\n");
                }

                // Check #2: Are there styles that prohibit us 
                //           from sending text to this control?
                if (!element.Current.IsKeyboardFocusable)
                {
                    throw new InvalidOperationException(
                        "The control with an AutomationID of "
                        + element.Current.AutomationId
                        + "is read-only.\n\n");
                }


                // Once you have an instance of an AutomationElement,  
                // check if it supports the ValuePattern pattern.
                object valuePattern = null;

                // Control does not support the ValuePattern pattern 
                // so use keyboard input to insert content.
                //
                // NOTE: Elements that support TextPattern 
                //       do not support ValuePattern and TextPattern
                //       does not support setting the text of 
                //       multi-line edit or document controls.
                //       For this reason, text input must be simulated
                //       using one of the following methods.
                //       
                if (!element.TryGetCurrentPattern(
                    ValuePattern.Pattern, out valuePattern))
                {
                    _feedbackText.Append("The control with an AutomationID of ")
                        .Append(element.Current.AutomationId)
                        .Append(" does not support ValuePattern.")
                        .AppendLine(" Using keyboard input.\n");

                    // Set focus for input functionality and begin.
                    element.SetFocus();

                    // Pause before sending keyboard input.
                    Thread.Sleep(100);

                    // Delete existing content in the control and insert new content.
                    SendKeys.SendWait("^{HOME}"); // Move to start of control
                    SendKeys.SendWait("^+{END}"); // Select everything
                    SendKeys.SendWait("{DEL}"); // Delete selection
                    SendKeys.SendWait(value);
                }
                // Control supports the ValuePattern pattern so we can 
                // use the SetValue method to insert content.
                else
                {
                    _feedbackText.Append("The control with an AutomationID of ")
                        .Append(element.Current.AutomationId)
                        .Append((" supports ValuePattern."))
                        .AppendLine(" Using ValuePattern.SetValue().\n");

                    // Set focus for input functionality and begin.
                    element.SetFocus();

                    ((ValuePattern) valuePattern).SetValue(value);
                }
            }
            catch (ArgumentNullException exc)
            {
                _feedbackText.Append(exc.Message);
            }
            catch (InvalidOperationException exc)
            {
                _feedbackText.Append(exc.Message);
            }
            finally
            {
                Feedback(_feedbackText.ToString());
            }
        }