Exemplo n.º 1
0
        private void CheckNewWindows()
        {
            /*
             * Need this API, as the OnWindowCreate doesn't
             * get all the subwindow events (WindowOpenedEvent), let us add such window
             * with this API
             * */
            AutomationElementCollection c;
            InternalTreeWalker          w = new InternalTreeWalker();
            Condition condition           = new PropertyCondition(
                AutomationElement.ControlTypeProperty, ControlType.Window);
            AutomationElement element = w.walker.GetFirstChild(AutomationElement.RootElement);

            try
            {
                while (null != element)
                {
                    try
                    {
                        if (this.IndexOf(element) == -1)
                        {
                            // New parent window is available, add it to the list
                            this.Add(element);
                            common.LogMessage(element.Current.Name);
                        }
                    }
                    catch (System.UnauthorizedAccessException ex)
                    {
                        // https://bugzilla.gnome.org/show_bug.cgi?id=706992
                        // Cobra looses all objects after steps specified inside
                        common.LogMessage(ex);
                        common.Wait(1);
                        element = w.walker.GetFirstChild(AutomationElement.RootElement);
                        this.Clear();
                        continue;
                    }
                    c = element.FindAll(TreeScope.Subtree, condition);
                    foreach (AutomationElement e in c)
                    {
                        if (this.IndexOf(e) == -1)
                        {
                            // New subwindow is available, add it to the list
                            this.Add(e);
                            common.LogMessage(e.Current.Name);
                        }
                    }
                    // Get next application in the list
                    element = w.walker.GetNextSibling(element);
                }
            }
            catch (Exception ex)
            {
                common.LogMessage(ex);
            }
            finally
            {
                c = null;
                w = null;
            }
        }
Exemplo n.º 2
0
        public string[] GetAppList()
        {
            Process   process;
            ArrayList appList = new ArrayList();

            foreach (AutomationElement e in utils.windowList)
            {
                // Get a process using the process id.
                try
                {
                    process = Process.GetProcessById(e.Current.ProcessId);
                }
                catch
                {
                    continue;
                }
                appList.Add(process.ProcessName);
            }
            InternalTreeWalker w       = new InternalTreeWalker();
            AutomationElement  element = w.walker.GetFirstChild(AutomationElement.RootElement);

            try
            {
                while (null != element)
                {
                    // Get a process using the process id.
                    try
                    {
                        process = Process.GetProcessById(element.Current.ProcessId);
                    }
                    catch
                    {
                        process = null;
                        continue;
                    }
                    if (!appList.Contains(process.ProcessName))
                    {
                        // If added from the existing window list
                        // then ignore it
                        appList.Add(process.ProcessName);
                    }
                    process = null;
                    element = w.walker.GetNextSibling(element);
                }
                return(appList.ToArray(typeof(string)) as string[]);
            }
            catch (Exception ex)
            {
                LogMessage(ex);
                return(appList.ToArray(typeof(string)) as string[]);
            }
            finally
            {
                w       = null;
                appList = null;
                element = null;
            }
        }
Exemplo n.º 3
0
        public string[] GetAppList()
        {
            Process process;
            ArrayList appList = new ArrayList();
            foreach (AutomationElement e in utils.windowList)
            {
                // Get a process using the process id.
                try
                {
                    process = Process.GetProcessById(e.Current.ProcessId);
                }
                catch
                {
                    continue;
                }
                appList.Add(process.ProcessName);
            }
            InternalTreeWalker w = new InternalTreeWalker();
            AutomationElement element = w.walker.GetFirstChild(AutomationElement.RootElement);
            try
            {
                while (null != element)
                {

                    // Get a process using the process id.
                    try
                    {
                        process = Process.GetProcessById(element.Current.ProcessId);
                    }
                    catch
                    {
                        process = null;
                        continue;
                    }
                    if (!appList.Contains(process.ProcessName))
                        // If added from the existing window list
                        // then ignore it
                        appList.Add(process.ProcessName);
                    process = null;
                    element = w.walker.GetNextSibling(element);
                }
                return appList.ToArray(typeof(string)) as string[];
            }
            catch (Exception ex)
            {
                LogMessage(ex);
                return appList.ToArray(typeof(string)) as string[];
            }
            finally
            {
                w = null;
                appList = null;
                element = null;
            }
        }
Exemplo n.º 4
0
        internal AutomationElement GetObjectHandle(AutomationElement e,
                                                   String objName, ControlType[] type, bool waitForObj)
        {
            AutomationElement o = null;

            if (e == null || String.IsNullOrEmpty(objName))
            {
                LogMessage("GetObjectHandle: Child handle NULL");
                return(null);
            }
            InternalTreeWalker w       = new InternalTreeWalker();
            ObjInfo            objInfo = new ObjInfo(false);
            int retry = waitForObj ? objectTimeOut : 1;
            // For debugging use the following value
            //int retry = waitForObj ? 1 : 1;
            ArrayList objectList = new ArrayList();

            for (int i = 0; i < retry; i++)
            {
                try
                {
                    o = InternalGetObjectHandle(w.walker.GetFirstChild(e),
                                                objName, type, ref objectList);
                }
                catch (Exception ex)
                {
                    LogMessage(ex);
                    o = null;
                }
                finally
                {
                    objectList.Clear();
                    // Collect all generations of memory.
                    GC.Collect();
                }
                if (o != null)
                {
                    w          = null;
                    objectList = null;
                    return(o);
                }
                // Wait 1 second, rescan for object
                Thread.Sleep(1000);
            }
            w          = null;
            objectList = null;
            return(o);
        }
Exemplo n.º 5
0
        public String[] GetObjectList(String windowName)
        {
            if (String.IsNullOrEmpty(windowName))
            {
                throw new XmlRpcFaultException(123, "Argument cannot be empty.");
            }
            string            matchedKey   = null;
            Hashtable         objectHT     = new Hashtable();
            ArrayList         objectList   = new ArrayList();
            AutomationElement windowHandle = utils.GetWindowHandle(windowName);

            if (windowHandle == null)
            {
                throw new XmlRpcFaultException(123, "Unable to find window: " +
                                               windowName);
            }
            InternalTreeWalker walker = new InternalTreeWalker();

            utils.InternalGetObjectList(walker.walker.GetFirstChild(windowHandle),
                                        ref objectList, ref objectHT, ref matchedKey,
                                        true, null, windowHandle.Current.Name);
            if (utils.debug || utils.writeToFile != null)
            {
                LogMessage(objectList.Count);
                foreach (string key in objectHT.Keys)
                {
                    LogMessage("Key: " + ((Hashtable)objectHT[key])["key"]);
                    LogMessage("Parent: " + ((Hashtable)objectHT[key])["parent"]);
                    LogMessage("Obj index: " + ((Hashtable)objectHT[key])["obj_index"]);
                    LogMessage("Class: " + ((Hashtable)objectHT[key])["class"]);
                    foreach (string child in (ArrayList)((Hashtable)objectHT[key])["children"])
                    {
                        LogMessage("Children: " + child);
                    }
                }
            }
            walker       = null;
            objectHT     = null;
            windowHandle = null;
            try
            {
                return(objectList.ToArray(typeof(string)) as string[]);
            }
            finally
            {
                objectList = null;
            }
        }
Exemplo n.º 6
0
        private AutomationElement InternalGetObjectHandle(AutomationElement childHandle,
            String objName, ControlType[] type, ref ArrayList objectList)
        {
            if (childHandle == null)
            {
                LogMessage("InternalGetObjectHandle: Child handle NULL");
                return null;
            }
            int index;
            String s = null;
            AutomationElement element;
            CurrentObjInfo currObjInfo;
            String actualString = null;
            ObjInfo objInfo = new ObjInfo(false);

            InternalTreeWalker w = new InternalTreeWalker();
            // Trying to mimic python fnmatch.translate
            String tmp = Regex.Replace(objName, @"( |:|\.|_|\r|\n|<|>)", "");
            tmp = Regex.Replace(tmp, @"\*", @".*");
            tmp = Regex.Replace(tmp, @"\\", @"\\");
            tmp = Regex.Replace(tmp, @"\(", @"\(");
            tmp = Regex.Replace(tmp, @"\)", @"\)");
            tmp = @"\A(?ms)" + tmp + @"\Z(?ms)";
            // This fails for some reason, commenting out for now
            //tmp += @"\Z(?ms)";
            Regex rx = new Regex(tmp, RegexOptions.Compiled |
                RegexOptions.IgnorePatternWhitespace | RegexOptions.Multiline |
                RegexOptions.CultureInvariant);
            try
            {
                element = childHandle;
                while (null != element)
                {
                    s = element.Current.Name;
                    currObjInfo = objInfo.GetObjectType(element);
                    if (s == null)
                    {
                        LogMessage("Current object Name is null");
                    }
                    else
                    {
                        s = s.ToString();
                        if (debug)
                            LogMessage("Obj name: " + s + " : " +
                                element.Current.ControlType.ProgrammaticName);
                        if (element.Current.ControlType == ControlType.MenuItem)
                        { // Do this only for menuitem type
                            // Split keyboard shortcut, as that might not be
                            // part of user provided object name
                            string[] tmpStrArray = Regex.Split(s, @"\t");
                            LogMessage("Menuitem shortcut length: " +
                                tmpStrArray.Length);
                            if (tmpStrArray.Length > 1)
                                // Keyboard shortcut found,
                                // just take first element from array
                                s = tmpStrArray[0];
                            tmpStrArray = null;
                        }
                    }
                    if (currObjInfo.objType != null)
                    {
                        if (s != null)
                            s = Regex.Replace(s, @"( |\t|:|\.|_|\r|\n|<|>)", "");
                        if (s == null || s.Length == 0)
                        {
                            // txt0, txt1
                            actualString = currObjInfo.objType +
                                currObjInfo.objCount;
                        }
                        else
                        {
                            // txtName, txtPassword
                            actualString = currObjInfo.objType + s;
                            LogMessage("###" + actualString + "###");
                            index = 1;
                            while (true)
                            {
                                if (objectList.IndexOf(actualString) < 0)
                                {
                                    // Object doesn't exist, assume this is the first
                                    // element with the name and type
                                    break;
                                }
                                actualString = currObjInfo.objType + s + index;
                                index++;
                            }
                        }
                        if (debug)
                        {
                            LogMessage(objName + " : " + actualString + " : " + s + " : " +
                                tmp);
                            LogMessage((s != null && rx.Match(s).Success) + " : " +
                                (rx.Match(actualString).Success));
                        }
                        objectList.Add(actualString);
                    }
                    if ((s != null && rx.Match(s).Success) ||
                        (actualString != null && rx.Match(actualString).Success))
                    {
                        if (type == null)
                            return element;
                        else
                        {
                            foreach (ControlType t in type)
                            {
                                if (debug)
                                    LogMessage((t == element.Current.ControlType) +
                                        " : " + element.Current.ControlType.ProgrammaticName);
                                if (t == element.Current.ControlType)
                                    return element;
                            }
                            LogMessage("type doesn't match !!!!!!!!!!!!!!");
                        }
                    }
                    // If any subchild exist for the current element navigate to it
                    AutomationElement subChild = InternalGetObjectHandle(
                        w.walker.GetFirstChild(element),
                        objName, type, ref objectList);
                    if (subChild != null)
                    {
                        // Object found, don't loop further
                        return subChild;
                    }
                    element = w.walker.GetNextSibling(element);
                }
            }
            catch (ElementNotAvailableException ex)
            {
                LogMessage(ex);
            }
            catch (Exception ex)
            {
                LogMessage(ex);
            }
            finally
            {
                w = null;
                rx = null;
            }
            return null;
        }
Exemplo n.º 7
0
 internal AutomationElement GetObjectHandle(AutomationElement e,
     String objName, ControlType[] type, bool waitForObj)
 {
     AutomationElement o = null;
     if (e == null || String.IsNullOrEmpty(objName))
     {
         LogMessage("GetObjectHandle: Child handle NULL");
         return null;
     }
     InternalTreeWalker w = new InternalTreeWalker();
     ObjInfo objInfo = new ObjInfo(false);
     int retry = waitForObj ? objectTimeOut : 1;
     // For debugging use the following value
     //int retry = waitForObj ? 1 : 1;
     ArrayList objectList = new ArrayList();
     for (int i = 0; i < retry; i++)
     {
         try
         {
             o = InternalGetObjectHandle(w.walker.GetFirstChild(e),
                 objName, type, ref objectList);
         }
         catch (Exception ex)
         {
             LogMessage(ex);
             o = null;
         }
         finally
         {
             objectList.Clear();
             // Collect all generations of memory.
             GC.Collect();
         }
         if (o != null)
         {
             w = null;
             objectList = null;
             return o;
         }
         // Wait 1 second, rescan for object
         Thread.Sleep(1000);
     }
     w = null;
     objectList = null;
     return o;
 }
Exemplo n.º 8
0
        private int InternalComboHandler(String windowName, String objName,
                                         String item, ref String selectedItem, String actionType = "Select",
                                         ArrayList childList = null)
        {
            bool verify = actionType == "Verify" ? true : false;

            ControlType[] comboTtype = new ControlType[3] {
                ControlType.ComboBox,
                ControlType.ListItem, ControlType.List/*, ControlType.Text */
            };
            AutomationElement childHandle = utils.GetObjectHandle(windowName,
                                                                  objName, comboTtype, !verify);
            Object            pattern       = null;
            Object            invokePattern = null;
            AutomationElement elementItem   = null;

            ControlType[] type = new ControlType[1] {
                ControlType.Button
            };
            try
            {
                LogMessage("Handle name: " + childHandle.Current.Name +
                           " - " + childHandle.Current.ControlType.ProgrammaticName);
                if (!utils.IsEnabled(childHandle, !verify))
                {
                    throw new XmlRpcFaultException(123, "Object state is disabled");
                }
                elementItem = null;

/*
 *              elementItem = utils.GetObjectHandle(childHandle, "Open", type, true);
 *              if (elementItem != null)
 *              {
 *                  LogMessage("elementItem: " + elementItem.Current.Name +
 *                                  " - " + elementItem.Current.ControlType.ProgrammaticName);
 *              }
 */
                if (childHandle.TryGetCurrentPattern(ExpandCollapsePattern.Pattern,
                                                     out pattern) || childHandle.TryGetCurrentPattern(
                        InvokePattern.Pattern, out invokePattern) ||
                    (elementItem != null && elementItem.TryGetCurrentPattern(
                         InvokePattern.Pattern, out invokePattern)))
                {
                    LogMessage("ExpandCollapsePattern");
                    // Retry max 5 times
                    for (int i = 0; i < 5; i++)
                    {
                        switch (actionType)
                        {
                        case "Hide":
                            if (invokePattern != null)
                            {
                                ((InvokePattern)invokePattern).Invoke();
                                return(1);
                            }
                            else if (pattern != null)
                            {
                                ((ExpandCollapsePattern)pattern).Collapse();
                                // Required to wait 1 second,
                                // before checking the state and retry collapsing
                                utils.InternalWait(1);
                                if (((ExpandCollapsePattern)pattern).Current.ExpandCollapseState ==
                                    ExpandCollapseState.Collapsed)
                                {
                                    // Hiding same combobox multiple time consecutively
                                    // fails. Check for the state and retry to collapse
                                    LogMessage("Collapsed");
                                    return(1);
                                }
                            }
                            break;

                        case "Show":
                        case "Select":
                        case "Verify":
//                                elementItem = utils.GetObjectHandle(childHandle, "Open",
//                                    type, !verify);
                            elementItem = null;

                            if (invokePattern != null || (elementItem != null &&
                                                          elementItem.TryGetCurrentPattern(InvokePattern.Pattern,
                                                                                           out invokePattern)))
                            {
                                ((InvokePattern)invokePattern).Invoke();
                            }
                            else if (pattern != null)
                            {
                                ((ExpandCollapsePattern)pattern).Expand();
                            }
                            // Required to wait 1 second,
                            // before checking the state and retry expanding
                            utils.InternalWait(1);
                            if (invokePattern != null || (pattern != null &&
                                                          ((ExpandCollapsePattern)pattern).Current.ExpandCollapseState ==
                                                          ExpandCollapseState.Expanded))
                            {
                                // Selecting same combobox multiple time consecutively
                                // fails. Check for the state and retry to expand
                                LogMessage("Expaneded");
                                if (actionType == "Show")
                                {
                                    return(1);
                                }
                                else
                                {
                                    return(SelectListItem(childHandle, item, verify) ? 1 : 0);
                                }
                            }
                            break;

                        case "GetComboValue":
                            Object selectionPattern = null;
                            LogMessage("GetComboValue");
//                                elementItem = utils.GetObjectHandle(childHandle, "Open",
//                                    type, true);
                            elementItem = null;

                            if (invokePattern != null || (elementItem != null &&
                                                          elementItem.TryGetCurrentPattern(InvokePattern.Pattern,
                                                                                           out invokePattern)))
                            {
                                LogMessage("InvokePattern");
                                childHandle.SetFocus();
                                utils.InternalClick(elementItem);
                                // InvokePattern doesn't work with Virtual Network
                                // Editor of VMware Workstation, so used the above InternalClick
                                //((InvokePattern)invokePattern).Invoke();
                            }
                            else if (pattern != null)
                            {
                                LogMessage("ExpandCollapsePattern");
                                ((ExpandCollapsePattern)pattern).Expand();
                            }
                            Object valuePattern;
                            if (childHandle.TryGetCurrentPattern(ValuePattern.Pattern,
                                                                 out valuePattern))
                            {
                                selectedItem = ((ValuePattern)valuePattern).Current.Value;
                                return(1);
                            }

                            // Required to wait 1 second,
                            // before checking the state and retry expanding
                            utils.InternalWait(1);
                            LogMessage("Handle name: " + childHandle.Current.Name +
                                       " - " + childHandle.Current.ControlType.ProgrammaticName);
                            bool typeExist = utils.InternalWaitTillChildControlTypeExist(childHandle, type);
                            LogMessage("Control type exist: " + typeExist);
                            AutomationElementCollection c = childHandle.FindAll(TreeScope.Subtree,
                                                                                Condition.TrueCondition);
                            LogMessage("AutomationElementCollection " + c.Count);
                            foreach (AutomationElement e in c)
                            {
                                LogMessage(e.Current.Name + " : " + e.Current.ControlType.ProgrammaticName);
                                bool status = false;
                                if (e.TryGetCurrentPattern(SelectionItemPattern.Pattern,
                                                           out selectionPattern))
                                {
                                    status = ((SelectionItemPattern)selectionPattern).Current.IsSelected;
                                    if (status)
                                    {
                                        LogMessage("Selected: " + e.Current.Name);
                                        selectedItem = e.Current.Name;
                                        ((ExpandCollapsePattern)pattern).Collapse();
                                        return(1);
                                    }
                                }
                            }
                            LogMessage("Unable to find selected combo box value");
                            c = null;
                            selectionPattern = null;
                            if (invokePattern != null)
                            {
                                ((InvokePattern)invokePattern).Invoke();
                            }
                            else if (pattern != null)
                            {
                                ((ExpandCollapsePattern)pattern).Collapse();
                            }
                            return(0);

                        case "GetAllItem":
                            string             matchedKey   = null;
                            Hashtable          objectHT     = new Hashtable();
                            ArrayList          tmpChildList = new ArrayList();
                            InternalTreeWalker w            = new InternalTreeWalker();
                            elementItem = utils.GetObjectHandle(childHandle, "Open",
                                                                type, true);
                            // Changes based on QT 5.0.2
                            if (invokePattern != null || (elementItem != null &&
                                                          elementItem.TryGetCurrentPattern(InvokePattern.Pattern,
                                                                                           out invokePattern)))
                            {
                                ((InvokePattern)invokePattern).Invoke();
                            }
                            else if (pattern != null)
                            {
                                ((ExpandCollapsePattern)pattern).Expand();
                            }
                            // Required to wait 1 second,
                            // before checking the state and retry expanding
                            utils.InternalWait(1);
                            utils.InternalGetObjectList(
                                w.walker.GetFirstChild(childHandle),
                                ref tmpChildList, ref objectHT, ref matchedKey,
                                true, null, null, ControlType.ListItem);
                            if (invokePattern != null)
                            {
                                ((InvokePattern)invokePattern).Invoke();
                            }
                            else if (pattern != null)
                            {
                                ((ExpandCollapsePattern)pattern).Collapse();
                            }
                            // For Linux compatibility
                            Hashtable propertyHT;
                            foreach (String key in objectHT.Keys)
                            {
                                propertyHT = (Hashtable)objectHT[key];
                                string className = (string)propertyHT["class"];
                                if (className != null &&
                                    className.Contains("list_item"))
                                {
                                    // Add only list items
                                    childList.Add(propertyHT["label"]);
                                }
                            }
                            w            = null;
                            tmpChildList = null;
                            propertyHT   = objectHT = null;
                            if (childList.Count > 0)
                            {
                                // Don't process the last item
                                return(1);
                            }
                            else
                            {
                                LogMessage("childList.Count <= 0: " + childList.Count);
                            }
                            return(0);
                        }
                    }
                }
                // Handle selectitem and verifyselect on list.
                // Get ExpandCollapsePattern fails on list,
                // VM Library items are selected and
                // verified correctly on Player with this fix
                else
                {
                    LogMessage("SelectListItem");
                    childHandle.SetFocus();
                    return(SelectListItem(childHandle, item, verify) ? 1 : 0);
                }
            }
            catch (Exception ex)
            {
                LogMessage(ex);
                if (ex is XmlRpcFaultException)
                {
                    throw;
                }
                else
                {
                    throw new XmlRpcFaultException(123,
                                                   "Unhandled exception: " + ex.Message);
                }
            }
            finally
            {
                comboTtype  = type = null;
                pattern     = invokePattern = null;
                elementItem = childHandle = null;
            }
            return(0);
        }
Exemplo n.º 9
0
 public String[] GetWindowList()
 {
     int index;
     String s, actualString;
     ArrayList windowArrayList = new ArrayList();
     CurrentObjInfo currObjInfo;
     ObjInfo objInfo = new ObjInfo(false);
     AutomationElement element;
     InternalTreeWalker w = new InternalTreeWalker();
     element = w.walker.GetFirstChild(AutomationElement.RootElement);
     Condition condition = new PropertyCondition(
         AutomationElement.ControlTypeProperty,
         ControlType.Window);
     try
     {
         AutomationElementCollection c;
         // FIXME: Check whether resetting the ObjInfo is appropriate here
         objInfo = new ObjInfo(false);
         while (null != element)
         {
             if (utils.windowList.IndexOf(element) == -1)
                 utils.windowList.Add(element);
             s = element.Current.Name;
             LogMessage("Window name: " + s);
             currObjInfo = objInfo.GetObjectType(element);
             if (String.IsNullOrEmpty(s))
                 actualString = currObjInfo.objType + currObjInfo.objCount;
             else
                 actualString = currObjInfo.objType + s;
             index = 1;
             while (true)
             {
                 if (windowArrayList.IndexOf(actualString) < 0)
                     break;
                 actualString = currObjInfo.objType + s + index;
                 index++;
             }
             windowArrayList.Add(actualString);
             try
             {
                 c = element.FindAll(TreeScope.Subtree, condition);
                 foreach (AutomationElement e in c)
                 {
                     if (utils.windowList.IndexOf(e) == -1)
                         utils.windowList.Add(e);
                     s = e.Current.Name;
                     currObjInfo = objInfo.GetObjectType(e);
                     if (String.IsNullOrEmpty(s))
                         actualString = currObjInfo.objType + currObjInfo.objCount;
                     else
                     {
                         actualString = currObjInfo.objType + s;
                         index = 1;
                         while (true)
                         {
                             if (windowArrayList.IndexOf(actualString) < 0)
                                 break;
                             actualString = currObjInfo.objType + s + index;
                             index++;
                         }
                     }
                     windowArrayList.Add(actualString);
                 }
             }
             catch (Exception ex)
             {
                 LogMessage(ex);
             }
             element = w.walker.GetNextSibling(element);
         }
         return windowArrayList.ToArray(typeof(string)) as string[];
     }
     catch (Exception ex)
     {
         LogMessage(ex);
     }
     finally
     {
         w = null;
         windowArrayList = null;
         condition = null;
     }
     // Unable to find window
     return null;
 }
Exemplo n.º 10
0
 public string[] GetObjectNameAtCoords(int waitTime = 0)
 {
     ObjInfo objInfo = new ObjInfo(false);
     CurrentObjInfo currObjInfo;
     ArrayList objectList = new ArrayList();
     String parentWindow = null;
     String objectString = null;
     System.Drawing.Point mouse;
     AutomationElement aimElement, parentElement, firstElement;
     InternalTreeWalker w = new InternalTreeWalker();
     utils.InternalWait(waitTime);
     try
     {
         mouse = System.Windows.Forms.Cursor.Position;
         aimElement = AutomationElement.FromPoint(new System.Windows.
             Point(mouse.X, mouse.Y));
         if (aimElement == null ||
             aimElement.Current.ClassName == "SysListView32" ||
             aimElement.Current.ClassName == "Shell_TrayWnd" ||
             aimElement.Current.ClassName == "MSTaskListWClass" ||
             aimElement.Current.ControlType == ControlType.Window ||
             aimElement.Current.ControlType == ControlType.TitleBar ||
             w.walker.GetParent(aimElement).Current.Name == "Context")
         {
             // If mouse is directly on desktop, taskbar, window, titlebar
             // and context menu, no other controls, then return None
             return new string[] { "None", "None" };
         }
         parentElement = aimElement;
         while (true)
         {
             if (parentElement.Current.ControlType == ControlType.Window ||
                 parentElement.Current.ClassName == "Progman" ||
                 parentElement.Current.ClassName == "Shell_TrayWnd")
             {
                 // Use window, desktop and taskbar as parent window
                 break;
             }
             parentElement = w.walker.GetParent(parentElement);
         }
         currObjInfo = objInfo.GetObjectType(parentElement);
         parentWindow = parentElement.Current.Name;
         if (parentWindow != null)
             parentWindow = Regex.Replace(parentWindow, "( |\r|\n)", "");
         if (String.IsNullOrEmpty(parentWindow))
         {
             // txt0, txt1
             parentWindow = currObjInfo.objType + currObjInfo.objCount;
         }
         else
         {
             // txtName, txtPassword
             parentWindow = currObjInfo.objType + parentWindow;
         }
         firstElement = w.walker.GetFirstChild(parentElement);
         if (firstElement == null)
         {
             LogMessage("Can not get object on window");
             return new string[] { "None", "None" };
         }
         GetObjectName(firstElement, ref objectList, aimElement, ref objectString);
         return new string[] { parentWindow, objectString };
     }
     catch (ElementNotAvailableException ex)
     {
         LogMessage("Exception: " + ex);
     }
     catch (Exception ex)
     {
         LogMessage(ex);
     }
     finally
     {
         w = null;
         objectList = null;
     }
     throw new XmlRpcFaultException(123, "Unable to get object name");
 }
Exemplo n.º 11
0
        private AutomationElement InternalGetObjectHandle(
            AutomationElement childHandle, String objName,
            ControlType[] type, ref ArrayList objectList,
            ObjInfo objInfo = null)
        {
            if (childHandle == null)
            {
                LogMessage("InternalGetObjectHandle: Child handle NULL");
                return(null);
            }
            int               index;
            String            s = null;
            AutomationElement element;
            CurrentObjInfo    currObjInfo;
            String            actualString = null;

            if (objInfo == null)
            {
                objInfo = new ObjInfo(false);
            }

            InternalTreeWalker w = new InternalTreeWalker();
            // Trying to mimic python fnmatch.translate
            String tmp = Regex.Replace(objName, @"( |:|\.|_|\r|\n|<|>)", "");

            tmp = Regex.Replace(tmp, @"\*", @".*");
            tmp = Regex.Replace(tmp, @"\?", @".");
            tmp = Regex.Replace(tmp, @"\\", @"\\");
            tmp = Regex.Replace(tmp, @"\(", @"\(");
            tmp = Regex.Replace(tmp, @"\)", @"\)");
            tmp = @"\A(?ms)" + tmp + @"\Z(?ms)";
            // This fails for some reason, commenting out for now
            //tmp += @"\Z(?ms)";
            Regex rx = new Regex(tmp, RegexOptions.Compiled |
                                 RegexOptions.IgnorePatternWhitespace | RegexOptions.Multiline |
                                 RegexOptions.CultureInvariant);

            try
            {
                element = childHandle;
                while (null != element)
                {
                    s           = element.Current.Name;
                    currObjInfo = objInfo.GetObjectType(element);
                    if (s == null)
                    {
                        LogMessage("Current object Name is null");
                    }
                    else
                    {
                        s = s.ToString();
                        if (debug)
                        {
                            LogMessage("Obj name: " + s + " : " +
                                       element.Current.ControlType.ProgrammaticName);
                        }
                        if (element.Current.ControlType == ControlType.MenuItem)
                        { // Do this only for menuitem type
                            // Split keyboard shortcut, as that might not be
                            // part of user provided object name
                            string[] tmpStrArray = Regex.Split(s, @"\t");
                            LogMessage("Menuitem shortcut length: " +
                                       tmpStrArray.Length);
                            if (tmpStrArray.Length > 1)
                            {
                                // Keyboard shortcut found,
                                // just take first element from array
                                s = tmpStrArray[0];
                            }
                            tmpStrArray = null;
                        }
                    }
                    if (currObjInfo.objType != null)
                    {
                        if (s != null)
                        {
                            s = Regex.Replace(s, @"( |\t|:|\.|_|\r|\n|<|>)", "");
                        }
                        if (s == null || s.Length == 0)
                        {
                            // txt0, txt1
                            actualString = currObjInfo.objType +
                                           currObjInfo.objCount;
                        }
                        else
                        {
                            // txtName, txtPassword
                            actualString = currObjInfo.objType + s;
                            LogMessage("###" + actualString + "###");
                            index = 1;
                            while (true)
                            {
                                if (objectList.IndexOf(actualString) < 0)
                                {
                                    // Object doesn't exist, assume this is the first
                                    // element with the name and type
                                    break;
                                }
                                actualString = currObjInfo.objType + s + index;
                                index++;
                            }
                        }
                        if (debug)
                        {
                            LogMessage(objName + " : " + actualString + " : " + s + " : " +
                                       tmp);
                            LogMessage((s != null && rx.Match(s).Success) + " : " +
                                       (rx.Match(actualString).Success));
                        }
                        objectList.Add(actualString);
                    }
                    if ((s != null && rx.Match(s).Success) ||
                        (actualString != null && rx.Match(actualString).Success))
                    {
                        if (type == null)
                        {
                            return(element);
                        }
                        else
                        {
                            foreach (ControlType t in type)
                            {
                                if (debug)
                                {
                                    LogMessage((t == element.Current.ControlType) +
                                               " : " + element.Current.ControlType.ProgrammaticName);
                                }
                                if (t == element.Current.ControlType)
                                {
                                    return(element);
                                }
                            }
                            LogMessage("type doesn't match !!!!!!!!!!!!!!");
                        }
                    }
                    // If any subchild exist for the current element navigate to it
                    AutomationElement subChild = InternalGetObjectHandle(
                        w.walker.GetFirstChild(element),
                        objName, type, ref objectList, objInfo);
                    if (subChild != null)
                    {
                        // Object found, don't loop further
                        return(subChild);
                    }
                    element = w.walker.GetNextSibling(element);
                }
            }
            catch (ElementNotAvailableException ex)
            {
                LogMessage(ex);
            }
            catch (Exception ex)
            {
                LogMessage(ex);
            }
            finally
            {
                w  = null;
                rx = null;
            }
            return(null);
        }
Exemplo n.º 12
0
        private AutomationElement InternalGetWindowHandle(String windowName,
                                                          ControlType[] type = null)
        {
            String            s;
            int               index;
            String            actualString;
            AutomationElement element;
            CurrentObjInfo    currObjInfo;
            ObjInfo           objInfo    = new ObjInfo(false);
            ArrayList         objectList = new ArrayList();
            // Trying to mimic python fnmatch.translate
            String tmp = Regex.Replace(windowName, @"\*", @".*");

            tmp = Regex.Replace(tmp, @"\?", @".");
            tmp = Regex.Replace(tmp, @"\\", @"\\");
            tmp = Regex.Replace(tmp, "( |\r|\n)", "");
            tmp = @"\A(?ms)" + tmp + @"\Z(?ms)";
            //tmp += @"\Z(?ms)";
            Regex rx = new Regex(tmp, RegexOptions.Compiled |
                                 RegexOptions.IgnorePatternWhitespace | RegexOptions.Multiline |
                                 RegexOptions.CultureInvariant);
            List <AutomationElement> windowTmpList = new List <AutomationElement>();
            InternalTreeWalker       w             = new InternalTreeWalker();

            try
            {
                foreach (AutomationElement e in windowList)
                {
                    try
                    {
                        currObjInfo = objInfo.GetObjectType(e);
                        s           = e.Current.Name;
                        if (s != null)
                        {
                            s = Regex.Replace(s, "( |\r|\n)", "");
                        }
                        if (s == null || s.Length == 0)
                        {
                            // txt0, txt1
                            actualString = currObjInfo.objType +
                                           currObjInfo.objCount;
                        }
                        else
                        {
                            // txtName, txtPassword
                            actualString = currObjInfo.objType + s;
                            index        = 1;
                            while (true)
                            {
                                if (objectList.IndexOf(actualString) < 0)
                                {
                                    // Object doesn't exist, assume this is the first
                                    // element with the name and type
                                    break;
                                }
                                actualString = currObjInfo.objType + s + index;
                                index++;
                            }
                        }
                        LogMessage("Window: " + actualString + " : " + tmp);
                        objectList.Add(actualString);
                        // FIXME: Handle dlg0 as in Linux
                        if ((s != null && rx.Match(s).Success) ||
                            rx.Match(actualString).Success)
                        {
                            if (type == null)
                            {
                                LogMessage(windowName + " - Window found");
                                w             = null;
                                rx            = null;
                                objectList    = null;
                                windowTmpList = null;
                                return(e);
                            }
                            else
                            {
                                foreach (ControlType t in type)
                                {
                                    if (debug)
                                    {
                                        LogMessage((t == e.Current.ControlType) +
                                                   " : " + e.Current.ControlType.ProgrammaticName);
                                    }
                                    if (t == e.Current.ControlType)
                                    {
                                        w             = null;
                                        rx            = null;
                                        objectList    = null;
                                        windowTmpList = null;
                                        return(e);
                                    }
                                }
                                LogMessage("type doesn't match !!!!!!!!!!!!!!");
                            }
                        }
                    }
                    catch (ElementNotAvailableException ex)
                    {
                        // Don't alter the current list, remove it later
                        windowTmpList.Add(e);
                        LogMessage(ex);
                    }
                    catch (Exception ex)
                    {
                        // Don't alter the current list, remove it later
                        windowTmpList.Add(e);
                        LogMessage(ex);
                    }
                }
            }
            catch (Exception ex)
            {
                LogMessage(ex);
            }
            try
            {
                foreach (AutomationElement e in windowTmpList)
                {
                    try
                    {
                        // Remove element from the list
                        windowList.Remove(e);
                    }
                    catch (Exception ex)
                    {
                        LogMessage(ex);
                    }
                }
            }
            catch (Exception ex)
            {
                LogMessage(ex);
            }
            windowTmpList = null;
            objectList.Clear();
            objInfo = new ObjInfo(false);
            element = w.walker.GetFirstChild(AutomationElement.RootElement);
            try
            {
                while (null != element)
                {
                    currObjInfo = objInfo.GetObjectType(element);
                    s           = element.Current.Name;
                    if (s != null)
                    {
                        s = Regex.Replace(s, "( |\r|\n)", "");
                    }
                    if (s == null || s == "")
                    {
                        // txt0, txt1
                        actualString = currObjInfo.objType +
                                       currObjInfo.objCount;
                    }
                    else
                    {
                        // txtName, txtPassword
                        actualString = currObjInfo.objType + s;
                        index        = 1;
                        while (true)
                        {
                            if (objectList.IndexOf(actualString) < 0)
                            {
                                // Object doesn't exist, assume this is the first
                                // element with the name and type
                                break;
                            }
                            actualString = currObjInfo.objType + s + index;
                            index++;
                        }
                    }
                    LogMessage("Window: " + actualString + " : " + tmp);
                    objectList.Add(actualString);
                    // FIXME: Handle dlg0 as in Linux
                    if ((s != null && rx.Match(s).Success) ||
                        rx.Match(actualString).Success)
                    {
                        if (type == null)
                        {
                            LogMessage(windowName + " - Window found");
                            return(element);
                        }
                        else
                        {
                            foreach (ControlType t in type)
                            {
                                if (debug)
                                {
                                    LogMessage((t == element.Current.ControlType) +
                                               " : " + element.Current.ControlType.ProgrammaticName);
                                }
                                if (t == element.Current.ControlType)
                                {
                                    return(element);
                                }
                            }
                            LogMessage("type doesn't match !!!!!!!!!!!!!!");
                        }
                    }
                    element = w.walker.GetNextSibling(element);
                }
                element = w.walker.GetFirstChild(
                    AutomationElement.RootElement);
                // Reset object info
                objInfo = new ObjInfo(false);
                objectList.Clear();
                AutomationElement subChild;
                while (null != element)
                {
                    subChild = w.walker.GetFirstChild(
                        element);
                    while (subChild != null)
                    {
                        if (subChild.Current.Name != null)
                        {
                            currObjInfo = objInfo.GetObjectType(subChild);
                            s           = subChild.Current.Name;
                            if (s != null)
                            {
                                s = Regex.Replace(s, "( |\r|\n)", "");
                            }
                            if (s == null || s == "")
                            {
                                // txt0, txt1
                                actualString = currObjInfo.objType +
                                               currObjInfo.objCount;
                            }
                            else
                            {
                                // txtName, txtPassword
                                actualString = currObjInfo.objType + s;
                                index        = 1;
                                while (true)
                                {
                                    if (objectList.IndexOf(actualString) < 0)
                                    {
                                        // Object doesn't exist, assume this is the first
                                        // element with the name and type
                                        break;
                                    }
                                    actualString = currObjInfo.objType + s + index;
                                    index++;
                                }
                            }
                            LogMessage("SubWindow: " + actualString + " : " + tmp);
                            if ((s != null && rx.Match(s).Success) ||
                                rx.Match(actualString).Success)
                            {
                                if (type == null)
                                {
                                    LogMessage(windowName + " - Window found");
                                    return(subChild);
                                }
                                else
                                {
                                    foreach (ControlType t in type)
                                    {
                                        if (debug)
                                        {
                                            LogMessage((t == subChild.Current.ControlType) +
                                                       " : " + subChild.Current.ControlType.ProgrammaticName);
                                        }
                                        if (t == subChild.Current.ControlType)
                                        {
                                            LogMessage(windowName + " - Window found");
                                            return(subChild);
                                        }
                                    }
                                    LogMessage("type doesn't match !!!!!!!!!!!!!!");
                                }
                            }
                        }
                        subChild = w.walker.GetNextSibling(subChild);
                    }
                    element = w.walker.GetNextSibling(element);
                }
            }
            catch (Exception ex)
            {
                LogMessage(ex);
            }
            finally
            {
                w          = null;
                rx         = null;
                objectList = null;
            }
            // Unable to find window
            return(null);
        }
Exemplo n.º 13
0
        public string[] GetObjectNameAtCoords(int waitTime = 0)
        {
            ObjInfo        objInfo = new ObjInfo(false);
            CurrentObjInfo currObjInfo;
            ArrayList      objectList   = new ArrayList();
            String         parentWindow = null;
            String         objectString = null;

            System.Drawing.Point mouse;
            AutomationElement    aimElement, parentElement, firstElement;
            InternalTreeWalker   w = new InternalTreeWalker();

            utils.InternalWait(waitTime);
            try
            {
                mouse      = System.Windows.Forms.Cursor.Position;
                aimElement = AutomationElement.FromPoint(new System.Windows.
                                                         Point(mouse.X, mouse.Y));
                if (aimElement == null ||
                    aimElement.Current.ClassName == "SysListView32" ||
                    aimElement.Current.ClassName == "Shell_TrayWnd" ||
                    aimElement.Current.ClassName == "MSTaskListWClass" ||
                    aimElement.Current.ControlType == ControlType.Window ||
                    aimElement.Current.ControlType == ControlType.TitleBar ||
                    w.walker.GetParent(aimElement).Current.Name == "Context")
                {
                    // If mouse is directly on desktop, taskbar, window, titlebar
                    // and context menu, no other controls, then return None
                    return(new string[] { "None", "None" });
                }
                parentElement = aimElement;
                while (true)
                {
                    if (parentElement.Current.ControlType == ControlType.Window ||
                        parentElement.Current.ClassName == "Progman" ||
                        parentElement.Current.ClassName == "Shell_TrayWnd")
                    {
                        // Use window, desktop and taskbar as parent window
                        break;
                    }
                    parentElement = w.walker.GetParent(parentElement);
                }
                currObjInfo  = objInfo.GetObjectType(parentElement);
                parentWindow = parentElement.Current.Name;
                if (parentWindow != null)
                {
                    parentWindow = Regex.Replace(parentWindow, "( |\r|\n)", "");
                }
                if (String.IsNullOrEmpty(parentWindow))
                {
                    // txt0, txt1
                    parentWindow = currObjInfo.objType + currObjInfo.objCount;
                }
                else
                {
                    // txtName, txtPassword
                    parentWindow = currObjInfo.objType + parentWindow;
                }
                firstElement = w.walker.GetFirstChild(parentElement);
                if (firstElement == null)
                {
                    LogMessage("Can not get object on window");
                    return(new string[] { "None", "None" });
                }
                GetObjectName(firstElement, ref objectList, aimElement, ref objectString);
                return(new string[] { parentWindow, objectString });
            }
            catch (ElementNotAvailableException ex)
            {
                LogMessage("Exception: " + ex);
            }
            catch (Exception ex)
            {
                LogMessage(ex);
            }
            finally
            {
                w          = null;
                objectList = null;
            }
            throw new XmlRpcFaultException(123, "Unable to get object name");
        }
Exemplo n.º 14
0
        public bool GetObjectName(AutomationElement firstElement, ref ArrayList objectList,
                                  AutomationElement aimElement, ref string actualString)
        {
            if (firstElement == null)
            {
                LogMessage("Invalid object handle");
                return(false);
            }
            CurrentObjInfo     currObjInfo;
            ObjInfo            objInfo = new ObjInfo(false);
            InternalTreeWalker w       = new InternalTreeWalker();
            int               index;
            string            s = null;
            AutomationElement childElement;

            try
            {
                childElement = firstElement;
                while (childElement != null)
                {
                    s           = childElement.Current.Name;
                    currObjInfo = objInfo.GetObjectType(childElement);
                    if (s == null)
                    {
                        LogMessage("Current object Name is null");
                    }
                    else
                    {
                        s = s.ToString();
                        if (true)
                        {
                            LogMessage("Obj name: " + s + " : " +
                                       childElement.Current.ControlType.ProgrammaticName);
                        }
                    }
                    if (currObjInfo.objType != null)
                    {
                        if (s != null)
                        {
                            s = Regex.Replace(s, @"( |\t|:|\.|_|\r|\n|<|>)", "");
                        }
                        if (String.IsNullOrEmpty(s))
                        {
                            // txt0, txt1
                            actualString = currObjInfo.objType +
                                           currObjInfo.objCount;
                        }
                        else
                        {
                            // txtName, txtPassword
                            actualString = currObjInfo.objType + s;
                            LogMessage("###" + actualString + "###");
                            index = 1;
                            while (true)
                            {
                                if (objectList.IndexOf(actualString) < 0)
                                {
                                    // Object doesn't exist, assume this is the first
                                    // element with the name and type
                                    break;
                                }
                                actualString = currObjInfo.objType + s + index;
                                index++;
                            }
                        }
                        objectList.Add(actualString);
                    }
                    if (aimElement == childElement && aimElement.Current.BoundingRectangle ==
                        childElement.Current.BoundingRectangle)
                    {
                        // Different controls on toolbar have same handle, probably it is a
                        // underlying bug with toolbar, so also use BoundingRectangle here
                        return(true);
                    }
                    // If any subchild exist for the current element navigate to it
                    AutomationElement subChild = w.walker.GetFirstChild(childElement);
                    if (subChild != null)
                    {
                        if (GetObjectName(subChild, ref objectList, aimElement,
                                          ref actualString))
                        {
                            return(true);
                        }
                    }
                    childElement = w.walker.GetNextSibling(childElement);
                }
                // If aimElement is not found, set actualString as None
                actualString = "None";
            }
            catch (ElementNotAvailableException ex)
            {
                LogMessage("Exception: " + ex);
            }
            catch (Exception ex)
            {
                LogMessage(ex);
            }
            finally
            {
                w = null;
            }
            return(false);
        }
Exemplo n.º 15
0
        public string GetObjectProperty(String windowName, String objName,
                                        string property)
        {
            if (String.IsNullOrEmpty(windowName) ||
                String.IsNullOrEmpty(objName) ||
                String.IsNullOrEmpty(property))
            {
                throw new XmlRpcFaultException(123, "Argument cannot be empty.");
            }
            bool               flag;
            string             matchedKey = "";
            ArrayList          objectList = new ArrayList();
            Hashtable          objectHT   = new Hashtable();
            InternalTreeWalker w;
            AutomationElement  windowHandle = utils.GetWindowHandle(windowName);

            if (windowHandle == null)
            {
                throw new XmlRpcFaultException(123,
                                               "Unable to find window: " + windowName);
            }
            w = new InternalTreeWalker();
            Hashtable ht;
            ArrayList childrenList;

            try
            {
                if (property == "children")
                {
                    flag = true;
                }
                else
                {
                    flag = false;
                }
                if (utils.InternalGetObjectList(w.walker.GetFirstChild(windowHandle),
                                                ref objectList, ref objectHT, ref matchedKey,
                                                flag, objName, windowHandle.Current.Name) || flag)
                {
                    if (utils.debug || utils.writeToFile != null)
                    {
                        LogMessage(objectList.Count);
                        foreach (string key in objectHT.Keys)
                        {
                            LogMessage("Key: " +
                                       ((Hashtable)objectHT[key])["key"]);
                            LogMessage("Parent: " +
                                       ((Hashtable)objectHT[key])["parent"]);
                            LogMessage("Obj index: " +
                                       ((Hashtable)objectHT[key])["obj_index"]);
                            LogMessage("Class: " +
                                       ((Hashtable)objectHT[key])["class"]);
                            foreach (string child in
                                     (ArrayList)((Hashtable)objectHT[key])["children"])
                            {
                                LogMessage("Children: " + child);
                            }
                        }
                    }
                    LogMessage(objectHT.Count + " : " + objectList.Count);
                    LogMessage(objectList[objectList.Count - 1]);
                    LogMessage("matchedKey: " + matchedKey + " : " + flag);
                    ht = (Hashtable)objectHT[matchedKey];
                    if (ht != null)
                    {
                        foreach (string key in ht.Keys)
                        {
                            LogMessage(key);
                            if (key == property)
                            {
                                if (property == "children")
                                {
                                    childrenList = (ArrayList)ht[key];
                                    LogMessage("Count: " + childrenList.Count);
                                    string value = "";
                                    foreach (string child in childrenList)
                                    {
                                        if (value == "")
                                        {
                                            value = child;
                                        }
                                        else
                                        {
                                            value += ", " + child;
                                        }
                                    }
                                    return(value);
                                }
                                else
                                {
                                    return((string)ht[key]);
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogMessage(ex);
                if (ex is XmlRpcFaultException)
                {
                    throw;
                }
                else
                {
                    throw new XmlRpcFaultException(123,
                                                   "Unhandled exception: " + ex.Message);
                }
            }
            finally
            {
                w            = null;
                ht           = null;
                windowHandle = null;
            }
            throw new XmlRpcFaultException(123, "Unable to find Object property: " +
                                           property + " of object: " + objName);
        }
Exemplo n.º 16
0
        public string[] GetObjectInfo(String windowName, String objName)
        {
            if (String.IsNullOrEmpty(windowName) ||
                String.IsNullOrEmpty(objName))
            {
                throw new XmlRpcFaultException(123, "Argument cannot be empty.");
            }
            string             matchedKey = "";
            ArrayList          objectList = new ArrayList();
            Hashtable          objectHT   = new Hashtable();
            InternalTreeWalker w;
            AutomationElement  windowHandle = utils.GetWindowHandle(windowName);

            if (windowHandle == null)
            {
                throw new XmlRpcFaultException(123,
                                               "Unable to find window: " + windowName);
            }
            w = new InternalTreeWalker();
            Hashtable ht;
            ArrayList keyList = new ArrayList();

            try
            {
                if (utils.InternalGetObjectList(w.walker.GetFirstChild(windowHandle),
                                                ref objectList, ref objectHT, ref matchedKey,
                                                false, objName, windowHandle.Current.Name))
                {
                    LogMessage(objectHT.Count + " : " + objectList.Count);
                    LogMessage(objectList[objectList.Count - 1]);
                    ht = (Hashtable)objectHT[matchedKey];
                    if (ht != null)
                    {
                        foreach (string key in ht.Keys)
                        {
                            LogMessage(key);
                            keyList.Add(key);
                        }
                        return(keyList.ToArray(typeof(string)) as string[]);
                    }
                }
            }
            catch (Exception ex)
            {
                LogMessage(ex);
                if (ex is XmlRpcFaultException)
                {
                    throw;
                }
                else
                {
                    throw new XmlRpcFaultException(123,
                                                   "Unhandled exception: " + ex.Message);
                }
            }
            finally
            {
                w            = null;
                ht           = null;
                keyList      = null;
                windowHandle = null;
            }
            throw new XmlRpcFaultException(123,
                                           "Unable to find Object info: " + objName);
        }
Exemplo n.º 17
0
        public string[] GetChild(String windowName, String childName = null,
                                 string role = null, string parentName = null)
        {
            if (String.IsNullOrEmpty(windowName) ||
                (String.IsNullOrEmpty(parentName) &&
                 String.IsNullOrEmpty(childName) &&
                 String.IsNullOrEmpty(role)))
            {
                throw new XmlRpcFaultException(123, "Argument cannot be empty.");
            }
            AutomationElement windowHandle = utils.GetWindowHandle(windowName);

            if (windowHandle == null)
            {
                throw new XmlRpcFaultException(123,
                                               "Unable to find window: " + windowName);
            }
            AutomationElement childHandle;

            if (!String.IsNullOrEmpty(childName))
            {
                childHandle = utils.GetObjectHandle(windowHandle, childName);
                if (childHandle == null)
                {
                    throw new XmlRpcFaultException(123,
                                                   "Unable to find child object: " + childName);
                }
            }
            if (!String.IsNullOrEmpty(role))
            {
                role = Regex.Replace(role, @" ", @"_");
            }
            Hashtable          ht;
            string             matchedKey = "";
            Hashtable          objectHT   = new Hashtable();
            ArrayList          childList  = new ArrayList();
            ArrayList          objectList = new ArrayList();
            InternalTreeWalker w          = new InternalTreeWalker();

            try
            {
                if (!String.IsNullOrEmpty(childName) ||
                    !String.IsNullOrEmpty(role))
                {
                    utils.InternalGetObjectList(w.walker.GetFirstChild(windowHandle),
                                                ref objectList, ref objectHT, ref matchedKey,
                                                true, childName, windowHandle.Current.Name);
                    Regex rx;
                    foreach (string key in objectHT.Keys)
                    {
                        try
                        {
                            if (utils.debug || utils.writeToFile != null)
                            {
                                LogMessage("Key: " + key);
                            }
                            ht = (Hashtable)objectHT[key];
                            String tmp = Regex.Replace(childName, @"\*", @".*");
                            tmp = Regex.Replace(tmp, " ", "");
                            tmp = Regex.Replace(tmp, @"\(", @"\(");
                            tmp = Regex.Replace(tmp, @"\)", @"\)");
                            rx  = new Regex(tmp, RegexOptions.Compiled |
                                            RegexOptions.IgnorePatternWhitespace |
                                            RegexOptions.Multiline |
                                            RegexOptions.CultureInvariant);
                            if (utils.debug || utils.writeToFile != null)
                            {
                                LogMessage("Role matched: " +
                                           (string)ht["class"] == role);
                                if (ht.ContainsKey("label") &&
                                    (string)ht["label"] != null)
                                {
                                    LogMessage("Label matched: " +
                                               rx.Match((string)ht["label"]).Success);
                                }
                            }
                            if ((String.IsNullOrEmpty(role) ||
                                 (!String.IsNullOrEmpty(role) &&
                                  (string)ht["class"] == role)) &&
                                ((ht.ContainsKey("label") &&
                                  (string)ht["label"] != null &&
                                  rx.Match((string)ht["label"]).Success) ||
                                 (ht.ContainsKey("key") &&
                                  (string)ht["key"] != null &&
                                  rx.Match((string)ht["key"]).Success)))
                            {
                                childList.Add(ht["key"]);
                            }
                        }
                        catch (Exception ex)
                        {
                            LogMessage(ex);
                        }
                        finally
                        {
                            rx = null;
                        }
                    }
                    return(childList.ToArray(typeof(string)) as string[]);
                }
            }
            catch (Exception ex)
            {
                LogMessage(ex);
                if (ex is XmlRpcFaultException)
                {
                    throw;
                }
                else
                {
                    throw new XmlRpcFaultException(123,
                                                   "Unhandled exception: " + ex.Message);
                }
            }
            finally
            {
                w           = null;
                ht          = objectHT = null;
                childHandle = windowHandle = null;
                childList   = objectList = null;
            }
            throw new XmlRpcFaultException(123,
                                           "Unsupported parameter type passed");
        }
Exemplo n.º 18
0
        private void CheckNewWindows()
        {
            /*
             * Need this API, as the OnWindowCreate doesn't
             * get all the subwindow events (WindowOpenedEvent), let us add such window
             * with this API
             * */
            AutomationElementCollection c;
            InternalTreeWalker          w = new InternalTreeWalker();
            Condition condition           = new PropertyCondition(
                AutomationElement.ControlTypeProperty, ControlType.Window);
            AutomationElement element = w.walker.GetFirstChild(AutomationElement.RootElement);

            try
            {
                while (null != element)
                {
                    try
                    {
                        mutex.WaitOne();

                        if (this.IndexOf(element) == -1)
                        {
                            // New parent window is available, add it to the list
                            this.Add(element);
                            common.LogMessage("New window (parent): " + element.Current.Name);
                        }
                    }
                    catch (System.UnauthorizedAccessException ex)
                    {
                        // https://bugzilla.gnome.org/show_bug.cgi?id=706992
                        // Cobra looses all objects after steps specified inside
                        common.LogMessage(ex);
                        common.Wait(1);
                        element = w.walker.GetFirstChild(AutomationElement.RootElement);
                        this.Clear();
                        continue;
                    }
                    finally
                    {
                        mutex.ReleaseMutex();
                    }
                    c = element.FindAll(TreeScope.Subtree, condition);
                    foreach (AutomationElement e in c)
                    {
                        try // ".IndexOf" sometimes throws an exception on my system
                        {
                            mutex.WaitOne();

                            if (this.IndexOf(e) == -1)
                            {
                                // New subwindow is available, add it to the list
                                this.Add(e);
                                common.LogMessage("New window (sub): " + e.Current.Name);
                            }
                        }
                        catch (System.UnauthorizedAccessException ex)
                        {
                            common.LogMessage(ex);
                            common.Wait(1);

                            // In case of an exception during IndexOf-call, the program adds the element as
                            // new element to the windowList. Same code like on other positions.
                            this.Add(e);
                            common.LogMessage("New window (via System.UnauthorizedAccessException exception): " + e.Current.Name);
                        }
                        finally
                        {
                            mutex.ReleaseMutex();
                        }
                    }
                    // Get next application in the list
                    element = w.walker.GetNextSibling(element);
                }
            }
            catch (Exception ex)
            {
                common.LogMessage(ex);
            }
            finally
            {
                c = null;
                w = null;
            }
        }
Exemplo n.º 19
0
 public string[] GetChild(String windowName, String childName = null,
     string role = null, string parentName = null)
 {
     if (String.IsNullOrEmpty(windowName) ||
         (String.IsNullOrEmpty(parentName) &&
         String.IsNullOrEmpty(childName) &&
         String.IsNullOrEmpty(role)))
     {
         throw new XmlRpcFaultException(123, "Argument cannot be empty.");
     }
     AutomationElement windowHandle = utils.GetWindowHandle(windowName);
     if (windowHandle == null)
     {
         throw new XmlRpcFaultException(123,
             "Unable to find window: " + windowName);
     }
     AutomationElement childHandle;
     if (!String.IsNullOrEmpty(childName))
     {
         childHandle = utils.GetObjectHandle(windowHandle, childName);
         if (childHandle == null)
         {
             throw new XmlRpcFaultException(123,
                 "Unable to find child object: " + childName);
         }
     }
     if (!String.IsNullOrEmpty(role))
         role = Regex.Replace(role, @" ", @"_");
     Hashtable ht;
     string matchedKey = "";
     Hashtable objectHT = new Hashtable();
     ArrayList childList = new ArrayList();
     ArrayList objectList = new ArrayList();
     InternalTreeWalker w = new InternalTreeWalker();
     try
     {
         if (!String.IsNullOrEmpty(childName) ||
             !String.IsNullOrEmpty(role))
         {
             utils.InternalGetObjectList(w.walker.GetFirstChild(windowHandle),
                 ref objectList, ref objectHT, ref matchedKey,
                 true, childName, windowHandle.Current.Name);
             Regex rx;
             foreach (string key in objectHT.Keys)
             {
                 try
                 {
                     if (utils.debug || utils.writeToFile != null)
                         LogMessage("Key: " + key);
                     ht = (Hashtable)objectHT[key];
                     String tmp = Regex.Replace(childName, @"\*", @".*");
                     tmp = Regex.Replace(tmp, " ", "");
                     tmp = Regex.Replace(tmp, @"\(", @"\(");
                     tmp = Regex.Replace(tmp, @"\)", @"\)");
                     rx = new Regex(tmp, RegexOptions.Compiled |
                         RegexOptions.IgnorePatternWhitespace |
                         RegexOptions.Multiline |
                         RegexOptions.CultureInvariant);
                     if (utils.debug || utils.writeToFile != null)
                     {
                         LogMessage("Role matched: " +
                             (string)ht["class"] == role);
                         if (ht.ContainsKey("label") &&
                             (string)ht["label"] != null)
                             LogMessage("Label matched: " +
                                 rx.Match((string)ht["label"]).Success);
                     }
                     if ((String.IsNullOrEmpty(role) ||
                         (!String.IsNullOrEmpty(role) &&
                         (string)ht["class"] == role)) &&
                         ((ht.ContainsKey("label") &&
                         (string)ht["label"] != null &&
                         rx.Match((string)ht["label"]).Success) ||
                         (ht.ContainsKey("key") &&
                         (string)ht["key"] != null &&
                         rx.Match((string)ht["key"]).Success)))
                     {
                         childList.Add(ht["key"]);
                     }
                 }
                 catch (Exception ex)
                 {
                     LogMessage(ex);
                 }
                 finally
                 {
                     rx = null;
                 }
             }
             return childList.ToArray(typeof(string)) as string[];
         }
     }
     catch (Exception ex)
     {
         LogMessage(ex);
         if (ex is XmlRpcFaultException)
             throw;
         else
             throw new XmlRpcFaultException(123,
                 "Unhandled exception: " + ex.Message);
     }
     finally
     {
         w = null;
         ht = objectHT = null;
         childHandle = windowHandle = null;
         childList = objectList = null;
     }
     throw new XmlRpcFaultException(123,
         "Unsupported parameter type passed");
 }
Exemplo n.º 20
0
        private int InternalMenuHandler(String windowName, String objName,
                                        ref ArrayList menuList, String actionType = "Select")
        {
            if (String.IsNullOrEmpty(windowName) ||
                String.IsNullOrEmpty(objName))
            {
                throw new XmlRpcFaultException(123, "Argument cannot be empty.");
            }
            Object pattern                = null;
            String currObjName            = null;
            AutomationElementCollection c = null;

            ControlType[] type = new ControlType[3] {
                ControlType.Menu,
                ControlType.MenuBar, ControlType.MenuItem
            };
            ControlType[] controlType = new ControlType[3] {
                ControlType.Menu,
                ControlType.MenuItem, ControlType.MenuBar
            };
            AutomationElement tmpContextHandle = null;
            AutomationElement windowHandle, childHandle;
            AutomationElement prevObjHandle = null, firstObjHandle = null;

            InternalTreeWalker w = new InternalTreeWalker();

            try
            {
                windowHandle = utils.GetWindowHandle(windowName);
                if (windowHandle == null)
                {
                    throw new XmlRpcFaultException(123,
                                                   "Unable to find window: " + windowName);
                }
                processId = windowHandle.Current.ProcessId;
                windowHandle.SetFocus();
                LogMessage("Window name: " + windowHandle + " : " +
                           windowHandle.Current.Name +
                           " : " + windowHandle.Current.ControlType.ProgrammaticName);
                childHandle = windowHandle;

                /*
                 * // element is an AutomationElement.
                 * AutomationPattern[] patterns = childHandle.GetSupportedPatterns();
                 * foreach (AutomationPattern pattern1 in patterns)
                 * {
                 *  Console.WriteLine("ProgrammaticName: " + pattern1.ProgrammaticName);
                 *  Console.WriteLine("PatternName: " + Automation.PatternName(pattern1));
                 * }
                 * /**/
                while (true)
                {
                    if (objName.Contains(";"))
                    {
                        int index = objName.IndexOf(";",
                                                    StringComparison.CurrentCulture);
                        currObjName = objName.Substring(0, index);
                        objName     = objName.Substring(index + 1);
                    }
                    else
                    {
                        currObjName = objName;
                    }
                    LogMessage("childHandle: " + childHandle.Current.Name +
                               " : " + currObjName + " : " +
                               childHandle.Current.ControlType.ProgrammaticName);
                    childHandle = utils.GetObjectHandle(childHandle,
                                                        currObjName, type, false);
                    if (childHandle == null)
                    {
                        if (currObjName == objName)
                        {
                            throw new XmlRpcFaultException(123,
                                                           "Unable to find Object: " + objName);
                        }
                        else
                        {
                            throw new XmlRpcFaultException(123,
                                                           "Unable to find Object: " + currObjName);
                        }
                    }
                    // Store previous handle for later use
                    prevObjHandle = childHandle;
                    if (firstObjHandle == null)
                    {
                        // Save it for later use
                        firstObjHandle = childHandle;
                    }
                    if ((actionType == "Select" || actionType == "SubMenu" ||
                         actionType == "Check" || actionType == "UnCheck" ||
                         actionType == "VerifyCheck" || actionType == "Window") &&
                        !utils.IsEnabled(childHandle, false))
                    {
                        throw new XmlRpcFaultException(123,
                                                       "Object state is disabled");
                    }
                    try
                    {
                        if (actionType == "Window")
                        {
                            utils.InternalXYClick(childHandle);
                        }
                        else
                        {
                            // SetFocus() fails on Windows Explorer
                            childHandle.SetFocus();
                        }
                    }
                    catch (Exception ex)
                    {
                        LogMessage(ex);
                    }
                    if (childHandle.TryGetCurrentPattern(InvokePattern.Pattern,
                                                         out pattern) || childHandle.TryGetCurrentPattern(
                            ExpandCollapsePattern.Pattern, out pattern))
                    {
                        if (actionType == "Select" || currObjName != objName ||
                            actionType == "SubMenu" || actionType == "VerifyCheck" ||
                            actionType == "Window")
                        {
                            try
                            {
                                LogMessage("Invoking menu item: " + currObjName +
                                           " : " + objName + " : " +
                                           childHandle.Current.ControlType.ProgrammaticName +
                                           " : " + childHandle.Current.Name);
                            }
                            catch (Exception ex)
                            {
                                // Noticed with closewindow() to close Notepad
                                //    System.UnauthorizedAccessException: Access is denied
                                //       Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)
                                LogMessage(ex);
                            }
                            if (actionType != "Window")
                            {
                                try
                                {
                                    // SetFocus() fails on Windows Explorer
                                    childHandle.SetFocus();
                                }
                                catch (Exception ex)
                                {
                                    LogMessage(ex);
                                }
                            }
                            if (!(actionType == "VerifyCheck" && currObjName == objName) &&
                                (actionType != "Window"))
                            {
                                utils.InternalClick(childHandle);
                            }
                            try
                            {
                                // Invoke doesn't work for VMware Workstation
                                // But they work for Notepad
                                // MoveToAndClick works for VMware Workstation
                                // But not for Notepad (on first time)
                                // Requires 2 clicks !
                                //((InvokePattern)pattern).Invoke();
                                utils.InternalWait(1);
                                c = childHandle.FindAll(TreeScope.Children,
                                                        Condition.TrueCondition);
                            }
                            catch (System.NotImplementedException ex)
                            {
                                // Noticed with VMware Workstation
                                //    System.Runtime.InteropServices.COMException (0x80040200):
                                //       Exception from HRESULT: 0x80040200
                                LogMessage("NotImplementedException");
                                LogMessage(ex);
                            }
                            catch (System.Windows.Automation.ElementNotEnabledException ex)
                            {
                                // Noticed with VMware Workstation
                                //    System.Runtime.InteropServices.COMException (0x80040200):
                                //       Exception from HRESULT: 0x80040200
                                LogMessage("Element not enabled");
                                LogMessage(ex);
                            }
                            catch (Exception ex)
                            {
                                LogMessage(ex);
                            }
                        }
                    }
                    if (currObjName == objName && actionType != "SubMenu")
                    {
                        int state;
                        switch (actionType)
                        {
                        case "Select":
                        case "Window":
                            // No child menu item to be processed
                            return(1);

                        case "Check":
                        case "UnCheck":
                            state = IsMenuChecked(childHandle);
                            LogMessage("IsMenuChecked(childHandle): " +
                                       childHandle.Current.ControlType.ProgrammaticName);
                            LogMessage("actionType: " + actionType);
                            // Don't process the last item
                            if (actionType == "Check")
                            {
                                if (state == 1)
                                {
                                    // Already checked, just click back the main menu
                                    utils.InternalClick(firstObjHandle);
                                }
                                else
                                {
                                    // Check menu
                                    utils.InternalClick(childHandle);
                                }
                                return(1);
                            }
                            else if (actionType == "UnCheck")
                            {
                                if (state == 0)
                                {
                                    // Already unchecked, just click back the main menu
                                    utils.InternalClick(firstObjHandle);
                                }
                                else
                                {
                                    // Uncheck menu
                                    utils.InternalClick(childHandle);
                                }
                                return(1);
                            }
                            break;

                        case "Exist":
                        case "Enabled":
                            state = utils.IsEnabled(childHandle) == true ? 1 : 0;
                            LogMessage("IsEnabled(childHandle): " +
                                       childHandle.Current.Name + " : " + state);
                            LogMessage("IsEnabled(childHandle): " +
                                       childHandle.Current.ControlType.ProgrammaticName);
                            // Set it back to old state, else the menu selection left there
                            utils.InternalClick(firstObjHandle);
                            // Don't process the last item
                            if (actionType == "Enabled")
                            {
                                return(state);
                            }
                            else if (actionType == "Exist")
                            {
                                return(1);
                            }
                            break;

                        case "SubMenu":
                            int status = HandleSubMenu(w.walker.GetFirstChild(childHandle),
                                                       firstObjHandle, ref menuList);
                            if (status == 1)
                            {
                                return(1);
                            }
                            break;

                        case "VerifyCheck":
                            state = IsMenuChecked(childHandle);
                            utils.InternalClick(firstObjHandle);
                            return(state);

                        default:
                            break;
                        }
                    }
                    else if ((tmpContextHandle = utils.InternalWaitTillControlTypeExist(
                                  ControlType.Menu, processId, 3)) != null)
                    {
                        LogMessage("InternalWaitTillControlTypeExist");
                        // Find object from current handle, rather than navigating
                        // the complete window
                        childHandle = tmpContextHandle;
                        if (actionType != "SubMenu")
                        {
                            continue;
                        }
                        else if (currObjName == objName)
                        {
                            switch (actionType)
                            {
                            case "SubMenu":
                                int status = HandleSubMenu(w.walker.GetFirstChild(childHandle),
                                                           firstObjHandle, ref menuList);
                                if (status == 1)
                                {
                                    return(1);
                                }
                                break;
                            }
                        }
                    }
                    else if (c != null && c.Count > 0)
                    {
                        if (currObjName == objName)
                        {
                            switch (actionType)
                            {
                            case "SubMenu":
                                int status = HandleSubMenu(w.walker.GetFirstChild(childHandle),
                                                           firstObjHandle, ref menuList);
                                if (status == 1)
                                {
                                    return(1);
                                }
                                break;
                            }
                        }
                        LogMessage("c != null && c.Count > 0");
                        childHandle = windowHandle;
                        continue;
                    }
                    // Required for Notepad like app
                    if ((c == null || c.Count == 0))
                    {
                        LogMessage("Work around for Windows application");
                        LogMessage(windowHandle.Current.Name + " : " + objName);
                        AutomationElement tmpChildHandle = utils.GetObjectHandle(
                            windowHandle, objName,
                            type, false);
                        // Work around for Notepad, as it doesn't find the menuitem
                        // on clicking any menu
                        if (tmpChildHandle != null)
                        {
                            LogMessage("Work around: tmpChildHandle != null");
                            if (actionType == "SubMenu" && currObjName == objName)
                            {
                                // Work around for Notepad like app
                                childHandle = tmpChildHandle;
                            }
                            else
                            {
                                // Work around for Notepad like app,
                                // but for actionType other than SubMenu
                                childHandle = windowHandle;
                            }
                        }
                    }
                    if (currObjName == objName)
                    {
                        switch (actionType)
                        {
                        case "SubMenu":
                            int status = HandleSubMenu(w.walker.GetFirstChild(childHandle),
                                                       firstObjHandle, ref menuList);
                            if (status == 1)
                            {
                                return(1);
                            }
                            break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogMessage(ex);
                if (firstObjHandle != null && actionType != "Window")
                {
                    // Set it back to old state, else the menu selection left there
                    utils.InternalXYClick(firstObjHandle);
                }
                if (((ex is ElementNotAvailableException) ||
                     (ex is UnauthorizedAccessException)) &&
                    actionType == "Window")
                {
                    // API closewindow() can close Windows Explorer on XP, but:
                    // -----------------------------------------------------------
                    // if (childHandle.TryGetCurrentPattern(InvokePattern.Pattern,
                    //     out pattern) || childHandle.TryGetCurrentPattern(
                    //     ExpandCollapsePattern.Pattern, out pattern))
                    // -----------------------------------------------------------
                    // Sometimes above code will throw exception, sometimes not:
                    //    System.Runtime.InteropServices.COMException (0x80040201):
                    //       Exception from HRESULT: 0x80040201
                    //    System.UnauthorizedAccessException, Access is denied:
                    //       Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
                    // So use this if block as workaround
                    return(1);
                }
                if (ex is XmlRpcFaultException)
                {
                    throw;
                }
                else
                {
                    throw new XmlRpcFaultException(123,
                                                   "Unhandled exception: " + ex.Message);
                }
            }
            finally
            {
                c             = null;
                w             = null;
                pattern       = null;
                windowHandle  = childHandle = null;
                prevObjHandle = firstObjHandle = null;
            }
        }
Exemplo n.º 21
0
 public String[] GetObjectList(String windowName)
 {
     if (String.IsNullOrEmpty(windowName))
     {
         throw new XmlRpcFaultException(123, "Argument cannot be empty.");
     }
     string matchedKey = null;
     Hashtable objectHT = new Hashtable();
     ArrayList objectList = new ArrayList();
     AutomationElement windowHandle = utils.GetWindowHandle(windowName);
     if (windowHandle == null)
     {
         throw new XmlRpcFaultException(123, "Unable to find window: " +
             windowName);
     }
     InternalTreeWalker walker = new InternalTreeWalker();
     utils.InternalGetObjectList(walker.walker.GetFirstChild(windowHandle),
         ref objectList, ref objectHT, ref matchedKey,
         true, null, windowHandle.Current.Name);
     if (utils.debug || utils.writeToFile != null)
     {
         LogMessage(objectList.Count);
         foreach (string key in objectHT.Keys)
         {
             LogMessage("Key: " + ((Hashtable)objectHT[key])["key"]);
             LogMessage("Parent: " + ((Hashtable)objectHT[key])["parent"]);
             LogMessage("Obj index: " + ((Hashtable)objectHT[key])["obj_index"]);
             LogMessage("Class: " + ((Hashtable)objectHT[key])["class"]);
             foreach (string child in (ArrayList)((Hashtable)objectHT[key])["children"])
                 LogMessage("Children: " + child);
         }
     }
     walker = null;
     objectHT = null;
     windowHandle = null;
     try
     {
         return objectList.ToArray(typeof(string)) as string[];
     }
     finally
     {
         objectList = null;
     }
 }
Exemplo n.º 22
0
        private int InternalMenuHandler(String windowName, String objName,
            ref ArrayList menuList, String actionType = "Select")
        {
            if (String.IsNullOrEmpty(windowName) ||
                String.IsNullOrEmpty(objName))
            {
                throw new XmlRpcFaultException(123, "Argument cannot be empty.");
            }
            String mainMenu = objName;
            String currObjName = null;
            Object invokePattern = null;
            AutomationElementCollection c = null;
            ControlType[] type = new ControlType[3] { ControlType.Menu,
                ControlType.MenuBar, ControlType.MenuItem };
            ControlType[] controlType = new ControlType[3] { ControlType.Menu,
                ControlType.MenuItem, ControlType.MenuBar };
            bool bContextNavigated = false;
            AutomationElement windowHandle, childHandle;
            AutomationElement prevObjHandle = null, firstObjHandle = null;

            InternalTreeWalker w = new InternalTreeWalker();
            try
            {
                windowHandle = utils.GetWindowHandle(windowName);
                if (windowHandle == null)
                {
                    throw new XmlRpcFaultException(123,
                        "Unable to find window: " + windowName);
                }
                windowHandle.SetFocus();
                LogMessage("Window name: " + windowHandle + " : " +
                    windowHandle.Current.Name +
                    " : " + windowHandle.Current.ControlType.ProgrammaticName);
                childHandle = windowHandle;
                /*
                // element is an AutomationElement.
                AutomationPattern[] patterns = childHandle.GetSupportedPatterns();
                foreach (AutomationPattern pattern1 in patterns)
                {
                    Console.WriteLine("ProgrammaticName: " + pattern1.ProgrammaticName);
                    Console.WriteLine("PatternName: " + Automation.PatternName(pattern1));
                }
                /**/
                while (true)
                {
                    if (objName.Contains(";"))
                    {
                        int index = objName.IndexOf(";",
                            StringComparison.CurrentCulture);
                        currObjName = objName.Substring(0, index);
                        objName = objName.Substring(index + 1);
                    }
                    else
                    {
                        currObjName = objName;
                    }
                    LogMessage("childHandle: " + childHandle.Current.Name +
                        " : " + currObjName + " : " +
                        childHandle.Current.ControlType.ProgrammaticName);
                    childHandle = utils.GetObjectHandle(childHandle,
                        currObjName, type, false);
                    if (childHandle == null)
                    {
                        if (currObjName == objName)
                        {
                            throw new XmlRpcFaultException(123,
                                "Unable to find Object: " + objName);
                        }
                        else
                        {
                            throw new XmlRpcFaultException(123,
                                "Unable to find Object: " + currObjName);
                        }
                    }
                    // Store previous handle for later use
                    prevObjHandle = childHandle;
                    if (firstObjHandle == null)
                    {
                        // Save it for later use
                        firstObjHandle = childHandle;
                    }
                    if ((actionType == "Select" || actionType == "SubMenu" ||
                        actionType == "Check" || actionType == "UnCheck" ||
                        actionType == "VerifyCheck") &&
                        !utils.IsEnabled(childHandle, false))
                    {
                        throw new XmlRpcFaultException(123,
                            "Object state is disabled");
                    }
                    childHandle.SetFocus();
                    if (childHandle.TryGetCurrentPattern(InvokePattern.Pattern,
                        out invokePattern))
                    {
                        if (actionType == "Select" || currObjName != objName ||
                             actionType == "SubMenu" || actionType == "VerifyCheck")
                        {
                            LogMessage("Invoking menu item: " + currObjName +
                                " : " + objName + " : " +
                                childHandle.Current.ControlType.ProgrammaticName +
                                " : " + childHandle.Current.Name);
                            childHandle.SetFocus();
                            if (!(actionType == "VerifyCheck" && currObjName == objName))
                            {
                                utils.InternalClick(childHandle);
                            }
                            try
                            {
                                // Invoke doesn't work for VMware Workstation
                                // But they work for Notepad
                                // MoveToAndClick works for VMware Workstation
                                // But not for Notepad (on first time)
                                // Requires 2 clicks !
                                //((InvokePattern)invokePattern).Invoke();
                                utils.InternalWait(1);
                                c = childHandle.FindAll(TreeScope.Children,
                                    Condition.TrueCondition);
                            }
                            catch (System.NotImplementedException ex)
                            {
                                // Noticed with VMware Workstation
                                //    System.Runtime.InteropServices.COMException (0x80040200):
                                //       Exception from HRESULT: 0x80040200
                                LogMessage("NotImplementedException");
                                LogMessage(ex);
                            }
                            catch (System.Windows.Automation.ElementNotEnabledException ex)
                            {
                                // Noticed with VMware Workstation
                                //    System.Runtime.InteropServices.COMException (0x80040200):
                                //       Exception from HRESULT: 0x80040200
                                LogMessage("Element not enabled");
                                LogMessage(ex);
                            }
                            catch (Exception ex)
                            {
                                LogMessage(ex);
                            }
                        }
                    }
                    if (currObjName == objName && actionType != "SubMenu")
                    {
                        int state;
                        switch (actionType)
                        {
                            case "Select":
                                // No child menu item to be processed
                                return 1;
                            case "Check":
                            case "UnCheck":
                                state = IsMenuChecked(childHandle);
                                LogMessage("IsMenuChecked(childHandle): " +
                                    childHandle.Current.ControlType.ProgrammaticName);
                                LogMessage("actionType: " + actionType);
                                // Don't process the last item
                                if (actionType == "Check")
                                {
                                    if (state == 1)
                                        // Already checked, just click back the main menu
                                        utils.InternalClick(firstObjHandle);
                                    else
                                        // Check menu
                                        utils.InternalClick(childHandle);
                                    return 1;
                                }
                                else if (actionType == "UnCheck")
                                {
                                    if (state == 0)
                                        // Already unchecked, just click back the main menu
                                        utils.InternalClick(firstObjHandle);
                                    else
                                        // Uncheck menu
                                        utils.InternalClick(childHandle);
                                    return 1;
                                }
                                break;
                            case "Exist":
                            case "Enabled":
                                state = utils.IsEnabled(childHandle) == true ? 1 : 0;
                                LogMessage("IsEnabled(childHandle): " +
                                    childHandle.Current.Name + " : " + state);
                                LogMessage("IsEnabled(childHandle): " +
                                    childHandle.Current.ControlType.ProgrammaticName);
                                // Set it back to old state, else the menu selection left there
                                utils.InternalClick(firstObjHandle);
                                // Don't process the last item
                                if (actionType == "Enabled")
                                    return state;
                                else if (actionType == "Exist")
                                    return 1;
                                break;
                            case "SubMenu":
                                string matchedKey = null;
                                Hashtable objectHT = new Hashtable();
                                menuList.Clear();
                                utils.InternalGetObjectList(
                                    w.walker.GetFirstChild(childHandle),
                                    ref menuList, ref objectHT, ref matchedKey);
                                objectHT = null;
                                if (menuList.Count > 0)
                                {
                                    // Set it back to old state,
                                    // else the menu selection left there
                                    utils.InternalClick(firstObjHandle);
                                    // Don't process the last item
                                    return 1;
                                }
                                else
                                    LogMessage("menuList.Count <= 0: " + menuList.Count);
                                break;
                            case "VerifyCheck":
                                state = IsMenuChecked(childHandle);
                                utils.InternalClick(firstObjHandle);
                                return state;
                            default:
                                break;
                        }
                    }
                    else if (!bContextNavigated && utils.InternalWaitTillGuiExist(
                        "Context", null, 3) == 1)
                    {
                        LogMessage("Context");
                        AutomationElement tmpWindowHandle;
                        // Menu item under Menu are listed under Menu Window
                        if (actionType == "VerifyCheck")
                            tmpWindowHandle = utils.GetWindowHandle("Context",
                                true, controlType);
                        else
                            tmpWindowHandle = utils.GetWindowHandle("Context");
                        if (tmpWindowHandle == null)
                        {
                            throw new XmlRpcFaultException(123,
                                "Unable to find window: Context");
                        }
                        // Find object from current handle, rather than navigating
                        // the complete window
                        childHandle = tmpWindowHandle;
                        bContextNavigated = true;
                        LogMessage("bContextNavigated: " + bContextNavigated);
                        if (actionType != "SubMenu")
                            continue;
                        else if (currObjName == objName)
                        {
                            switch (actionType)
                            {
                                case "SubMenu":
                                    string matchedKey = null;
                                    Hashtable objectHT = new Hashtable();
                                    menuList.Clear();
                                    utils.InternalGetObjectList(
                                        w.walker.GetFirstChild(childHandle),
                                        ref menuList, ref objectHT, ref matchedKey);
                                    if (menuList.Count > 0)
                                    {
                                        // Set it back to old state,
                                        // else the menu selection left there
                                        utils.InternalClick(firstObjHandle);
                                        // Don't process the last item
                                        return 1;
                                    }
                                    else
                                        LogMessage("menuList.Count <= 0: " + menuList.Count);
                                    break;
                            }
                        }
                    }
                    else if (c != null && c.Count > 0)
                    {
                        LogMessage("c != null && c.Count > 0");
                        childHandle = windowHandle;
                        continue;
                    }
                    else if (utils.InternalWaitTillGuiExist(prevObjHandle.Current.Name,
                        null, 3) == 1)
                    {
                        LogMessage("prevObjHandle: " + prevObjHandle.Current.Name);
                        // Menu item under Menu are listed under Menu Window
                        LogMessage("Menu item under Menu are listed under Menu Window: " +
                            prevObjHandle.Current.Name);
                        AutomationElement tmpWindowHandle;
                        if (actionType == "VerifyCheck")
                            tmpWindowHandle = utils.GetWindowHandle(
                                prevObjHandle.Current.Name,
                                true, controlType);
                        else
                            tmpWindowHandle = utils.GetWindowHandle(
                                prevObjHandle.Current.Name);
                        if (tmpWindowHandle != null)
                        {
                            // Find object from current handle, rather than navigating
                            // the complete window
                            LogMessage("Assigning tmpWindowHandle as childHandle");
                            childHandle = tmpWindowHandle;
                            LogMessage("childHandle: " + childHandle.Current.Name + " : " +
                                childHandle.Current.ControlType.ProgrammaticName);
                        }
                    }
                    // Required for Notepad like app
                    if ((c == null || c.Count == 0))
                    {
                        LogMessage("Work around for Windows application");
                        LogMessage(windowHandle.Current.Name + " : " + objName);
                        AutomationElement tmpChildHandle = utils.GetObjectHandle(
                            windowHandle, objName,
                            type, false);
                        // Work around for Notepad, as it doesn't find the menuitem
                        // on clicking any menu
                        if (tmpChildHandle != null)
                        {
                            LogMessage("Work around: tmpChildHandle != null");
                            if (actionType == "SubMenu" && currObjName == objName)
                                // Work around for Notepad like app
                                childHandle = tmpChildHandle;
                            else
                                // Work around for Notepad like app,
                                // but for actionType other than SubMenu
                                childHandle = windowHandle;
                        }
                    }
                    if (currObjName == objName)
                    {
                        switch (actionType)
                        {
                            case "SubMenu":
                                string matchedKey = null;
                                Hashtable objectHT = new Hashtable();
                                menuList.Clear();
                                utils.InternalGetObjectList(w.walker.GetFirstChild(childHandle),
                                    ref menuList, ref objectHT, ref matchedKey);
                                // Set it back to old state,
                                // else the menu selection left there
                                utils.InternalClick(firstObjHandle);
                                objectHT = null;
                                // Don't process the last item
                                return 1;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogMessage(ex);
                if (firstObjHandle != null)
                {
                    // Set it back to old state, else the menu selection left there
                    utils.InternalClick(firstObjHandle);
                }
                if (ex is XmlRpcFaultException)
                    throw;
                else
                    throw new XmlRpcFaultException(123,
                                    "Unhandled exception: " + ex.Message);
            }
            finally
            {
                c = null;
                w = null;
                windowHandle = childHandle = null;
                prevObjHandle = firstObjHandle = null;
            }
        }
Exemplo n.º 23
0
        public String[] GetWindowList()
        {
            int                index;
            String             s, actualString;
            ArrayList          windowArrayList = new ArrayList();
            CurrentObjInfo     currObjInfo;
            ObjInfo            objInfo = new ObjInfo(false);
            AutomationElement  element;
            InternalTreeWalker w = new InternalTreeWalker();

            element = w.walker.GetFirstChild(AutomationElement.RootElement);
            Condition condition = new PropertyCondition(
                AutomationElement.ControlTypeProperty,
                ControlType.Window);

            try
            {
                AutomationElementCollection c;
                // FIXME: Check whether resetting the ObjInfo is appropriate here
                objInfo = new ObjInfo(false);
                while (null != element)
                {
                    if (utils.windowList.IndexOf(element) == -1)
                    {
                        utils.windowList.Add(element);
                    }
                    s = element.Current.Name;
                    LogMessage("Window name: " + s);
                    currObjInfo = objInfo.GetObjectType(element);
                    if (String.IsNullOrEmpty(s))
                    {
                        actualString = currObjInfo.objType + currObjInfo.objCount;
                    }
                    else
                    {
                        actualString = currObjInfo.objType + s;
                    }
                    index = 1;
                    while (true)
                    {
                        if (windowArrayList.IndexOf(actualString) < 0)
                        {
                            break;
                        }
                        actualString = currObjInfo.objType + s + index;
                        index++;
                    }
                    windowArrayList.Add(actualString);
                    try
                    {
                        c = element.FindAll(TreeScope.Subtree, condition);
                        foreach (AutomationElement e in c)
                        {
                            if (utils.windowList.IndexOf(e) == -1)
                            {
                                utils.windowList.Add(e);
                            }
                            s           = e.Current.Name;
                            currObjInfo = objInfo.GetObjectType(e);
                            if (String.IsNullOrEmpty(s))
                            {
                                actualString = currObjInfo.objType + currObjInfo.objCount;
                            }
                            else
                            {
                                actualString = currObjInfo.objType + s;
                                index        = 1;
                                while (true)
                                {
                                    if (windowArrayList.IndexOf(actualString) < 0)
                                    {
                                        break;
                                    }
                                    actualString = currObjInfo.objType + s + index;
                                    index++;
                                }
                            }
                            windowArrayList.Add(actualString);
                        }
                    }
                    catch (Exception ex)
                    {
                        LogMessage(ex);
                    }
                    element = w.walker.GetNextSibling(element);
                }
                return(windowArrayList.ToArray(typeof(string)) as string[]);
            }
            catch (Exception ex)
            {
                LogMessage(ex);
            }
            finally
            {
                w = null;
                windowArrayList = null;
                condition       = null;
            }
            // Unable to find window
            return(null);
        }
Exemplo n.º 24
0
        internal bool InternalGetObjectList(AutomationElement windowHandle,
                                            ref ArrayList objectList, ref Hashtable objectHT,
                                            ref string matchedKey, bool needAll = false,
                                            string objName   = null, string parentName = null,
                                            ControlType type = null, ObjInfo objInfo   = null)
        {
            if (windowHandle == null)
            {
                LogMessage("Invalid window handle");
                return(false);
            }
            int    index;
            Regex  rx = null;
            String s  = null;

            if (objInfo == null)
            {
                objInfo = new ObjInfo(false);
            }
            String         actualString = null;
            CurrentObjInfo currObjInfo;
            Hashtable      propertyHT;
            // Trying to mimic python fnmatch.translate
            String tmp = null;

            InternalTreeWalker w = new InternalTreeWalker();

            if (objName != null)
            {
                tmp = Regex.Replace(objName, @"\*", @".*");
                tmp = Regex.Replace(tmp, @"\?", @".");
                tmp = Regex.Replace(tmp, @"( |:|\.|_|\r|\n|<|>)", "");
                tmp = Regex.Replace(tmp, @"\(", @"\(");
                tmp = Regex.Replace(tmp, @"\)", @"\)");
                //tmp += @"\Z(?ms)";
                rx = new Regex(tmp, RegexOptions.Compiled |
                               RegexOptions.IgnorePatternWhitespace | RegexOptions.Multiline |
                               RegexOptions.CultureInvariant);
            }
            try
            {
                AutomationElement element = windowHandle;
                while (null != element)
                {
                    s           = element.Current.Name;
                    currObjInfo = objInfo.GetObjectType(element);
                    if (s == null)
                    {
                        LogMessage("Current object Name is null");
                    }
                    else
                    {
                        s = s.ToString();
                        if (debug)
                        {
                            LogMessage("Obj name: " + s + " : " +
                                       element.Current.ControlType.ProgrammaticName);
                        }
                    }
                    actualString = null;
                    if (s != null)
                    {
                        s = Regex.Replace(s, @"( |\t|:|\.|_|\r|\n|<|>)", "");
                    }
                    if (s == null || s.Length == 0)
                    {
                        // txt0, txt1
                        actualString = currObjInfo.objType + currObjInfo.objCount;
                    }
                    else
                    {
                        // txtName, txtPassword
                        actualString = currObjInfo.objType + s;
                        index        = 1;
                        while (true)
                        {
                            if (objectList.IndexOf(actualString) < 0)
                            {
                                // Object doesn't exist, assume this is the first
                                // element with the name and type
                                break;
                            }
                            actualString = currObjInfo.objType + s + index;
                            index++;
                        }
                    }
                    if (type == null || type == element.Current.ControlType)
                    {
                        // Add if ControlType is null
                        // or only matching type, if available
                        objectList.Add(actualString);
                    }
                    if (objName != null || needAll)
                    {
                        //needAll - Required for GetChild
                        propertyHT = new Hashtable();
                        propertyHT.Add("key", actualString);
                        try
                        {
                            LogMessage(element.Current.LocalizedControlType);
                            string className = element.Current.LocalizedControlType;
                            if (element.Current.ControlType == ControlType.Button)
                            {
                                // For LDTP compatibility
                                className = "push_button";
                            }
                            propertyHT.Add("class",
                                           Regex.Replace(className, " ", "_"));
                        }
                        catch (Exception ex)
                        {
                            LogMessage(ex);
                            propertyHT.Add("class",
                                           element.Current.ControlType.ProgrammaticName);
                        }
                        propertyHT.Add("parent", parentName);
                        //propertyHT.Add("child_index", element.Current);
                        ArrayList childrenList = new ArrayList();
                        propertyHT.Add("children", childrenList);
                        // Check if parent exists
                        if (parentName != null && parentName.Length > 0 &&
                            objectHT[parentName] != null)
                        {
                            LogMessage("parentName NOT NULL");
                            // Add current child to the parent
                            ((ArrayList)((Hashtable)objectHT[parentName])["children"]).Add(actualString);
                        }
                        else
                        {
                            LogMessage("parentName NULL");
                        }
                        propertyHT.Add("obj_index",
                                       currObjInfo.objType + "#" + currObjInfo.objCount);
                        if (s != null)
                        {
                            propertyHT.Add("label", element.Current.Name);
                        }
                        // Following 2 properties exist in Linux
                        //propertyHT.Add("label_by", s == null ? "" : s);
                        //propertyHT.Add("description", element.Current.DescribedBy);
                        if (element.Current.AcceleratorKey != "")
                        {
                            propertyHT.Add("key_binding",
                                           element.Current.AcceleratorKey);
                        }
                        // Add individual property to object property
                        objectHT.Add(actualString, propertyHT);
                        if (debug && rx != null)
                        {
                            LogMessage(objName + " : " + actualString + " : " + s
                                       + " : " + tmp);
                            LogMessage((s != null && rx.Match(s).Success) + " : " +
                                       (actualString != null && rx.Match(actualString).Success));
                        }
                        if ((s != null && rx != null && rx.Match(s).Success) ||
                            (actualString != null && rx != null && rx.Match(actualString).Success))
                        {
                            matchedKey = actualString;
                            LogMessage("String matched: " + needAll);
                            if (!needAll)
                            {
                                return(true);
                            }
                        }
                    }

                    // If any subchild exist for the current element navigate to it
                    if (InternalGetObjectList(w.walker.GetFirstChild(element),
                                              ref objectList, ref objectHT, ref matchedKey,
                                              needAll, objName, actualString, type, objInfo))
                    {
                        return(true);
                    }
                    element = w.walker.GetNextSibling(element);
                }
            }
            catch (ElementNotAvailableException ex)
            {
                LogMessage("Exception: " + ex);
            }
            catch (Exception ex)
            {
                LogMessage("Exception: " + ex);
            }
            finally
            {
                w          = null;
                rx         = null;
                propertyHT = null;
            }
            return(false);
        }
Exemplo n.º 25
0
        private int InternalComboHandler(String windowName, String objName,
                                         String item, String actionType = "Select",
                                         ArrayList childList            = null)
        {
            AutomationElement childHandle = GetObjectHandle(windowName,
                                                            objName);
            Object pattern = null;

            try
            {
                LogMessage("Handle name: " + childHandle.Current.Name +
                           " - " + childHandle.Current.ControlType.ProgrammaticName);
                if (!utils.IsEnabled(childHandle))
                {
                    throw new XmlRpcFaultException(123, "Object state is disabled");
                }
                if (childHandle.TryGetCurrentPattern(ExpandCollapsePattern.Pattern,
                                                     out pattern))
                {
                    LogMessage("ExpandCollapsePattern");
                    // Retry max 5 times
                    for (int i = 0; i < 5; i++)
                    {
                        switch (actionType)
                        {
                        case "Hide":
                            ((ExpandCollapsePattern)pattern).Collapse();
                            // Required to wait 1 second,
                            // before checking the state and retry collapsing
                            utils.InternalWait(1);
                            if (((ExpandCollapsePattern)pattern).Current.ExpandCollapseState ==
                                ExpandCollapseState.Collapsed)
                            {
                                // Hiding same combobox multiple time consecutively
                                // fails. Check for the state and retry to collapse
                                LogMessage("Collapsed");
                                return(1);
                            }
                            break;

                        case "Show":
                        case "Select":
                        case "Verify":
                            ((ExpandCollapsePattern)pattern).Expand();
                            // Required to wait 1 second,
                            // before checking the state and retry expanding
                            utils.InternalWait(1);
                            if (((ExpandCollapsePattern)pattern).Current.ExpandCollapseState ==
                                ExpandCollapseState.Expanded)
                            {
                                // Selecting same combobox multiple time consecutively
                                // fails. Check for the state and retry to expand
                                LogMessage("Expaneded");
                                if (actionType == "Show")
                                {
                                    return(1);
                                }
                                else
                                {
                                    childHandle.SetFocus();
                                    bool verify = actionType == "Verify" ? true : false;
                                    return(SelectListItem(childHandle, item, verify) ? 1 : 0);
                                }
                            }
                            break;

                        case "GetAllItem":
                            string             matchedKey   = null;
                            Hashtable          objectHT     = new Hashtable();
                            ArrayList          tmpChildList = new ArrayList();
                            InternalTreeWalker w            = new InternalTreeWalker();
                            utils.InternalGetObjectList(
                                w.walker.GetFirstChild(childHandle),
                                ref tmpChildList, ref objectHT, ref matchedKey,
                                true, null, null, ControlType.ListItem);
                            // For Linux compatibility
                            Hashtable propertyHT;
                            foreach (String key in objectHT.Keys)
                            {
                                propertyHT = (Hashtable)objectHT[key];
                                string className = (string)propertyHT["class"];
                                if (className != null &&
                                    className.Contains("list_item"))
                                {
                                    // Add only list items
                                    childList.Add(propertyHT["label"]);
                                }
                            }
                            w            = null;
                            tmpChildList = null;
                            propertyHT   = objectHT = null;
                            if (childList.Count > 0)
                            {
                                // Don't process the last item
                                return(1);
                            }
                            else
                            {
                                LogMessage("childList.Count <= 0: " + childList.Count);
                            }
                            return(0);
                        }
                    }
                }
                // Handle selectitem and verifyselect on list.
                // Get ExpandCollapsePattern fails on list,
                // VM Library items are selected and
                // verified correctly on Player with this fix
                else
                {
                    childHandle.SetFocus();
                    bool verify = actionType == "Verify" ? true : false;
                    return(SelectListItem(childHandle, item, verify) ? 1 : 0);
                }
            }
            catch (Exception ex)
            {
                LogMessage(ex);
                if (ex is XmlRpcFaultException)
                {
                    throw;
                }
                else
                {
                    throw new XmlRpcFaultException(123,
                                                   "Unhandled exception: " + ex.Message);
                }
            }
            finally
            {
                pattern     = null;
                childHandle = null;
            }
            return(0);
        }
Exemplo n.º 26
0
 public string[] GetObjectInfo(String windowName, String objName)
 {
     if (String.IsNullOrEmpty(windowName) ||
         String.IsNullOrEmpty(objName))
     {
         throw new XmlRpcFaultException(123, "Argument cannot be empty.");
     }
     string matchedKey = "";
     ArrayList objectList = new ArrayList();
     Hashtable objectHT = new Hashtable();
     InternalTreeWalker w;
     AutomationElement windowHandle = utils.GetWindowHandle(windowName);
     if (windowHandle == null)
     {
         throw new XmlRpcFaultException(123,
             "Unable to find window: " + windowName);
     }
     w = new InternalTreeWalker();
     Hashtable ht;
     ArrayList keyList = new ArrayList();
     try
     {
         if (utils.InternalGetObjectList(w.walker.GetFirstChild(windowHandle),
             ref objectList, ref objectHT, ref matchedKey,
             false, objName, windowHandle.Current.Name))
         {
             LogMessage(objectHT.Count + " : " + objectList.Count);
             LogMessage(objectList[objectList.Count - 1]);
             ht = (Hashtable)objectHT[matchedKey];
             if (ht != null)
             {
                 foreach (string key in ht.Keys)
                 {
                     LogMessage(key);
                     keyList.Add(key);
                 }
                 return keyList.ToArray(typeof(string)) as string[];
             }
         }
     }
     catch (Exception ex)
     {
         LogMessage(ex);
         if (ex is XmlRpcFaultException)
             throw;
         else
             throw new XmlRpcFaultException(123,
                 "Unhandled exception: " + ex.Message);
     }
     finally
     {
         w = null;
         ht = null;
         keyList = null;
         windowHandle = null;
     }
     throw new XmlRpcFaultException(123,
         "Unable to find Object info: " + objName);
 }
Exemplo n.º 27
0
 public String[] GetWindowList()
 {
     int index;
     String s, actualString;
     ArrayList windowArrayList = new ArrayList();
     CurrentObjInfo currObjInfo;
     ObjInfo objInfo = new ObjInfo(false);
     AutomationElement element;
     InternalTreeWalker w = new InternalTreeWalker();
     element = w.walker.GetFirstChild(AutomationElement.RootElement);
     Condition condition = new PropertyCondition(
         AutomationElement.ControlTypeProperty,
         ControlType.Window);
     try
     {
         AutomationElementCollection c;
         List<AutomationElement> windowTmpList = new List<AutomationElement>();
         LogMessage("GetWindowList - Window list count: " +
             utils.windowList.Count);
         try
         {
             foreach (AutomationElement e in utils.windowList)
             {
                 try
                 {
                     s = e.Current.Name;
                     LogMessage("Cached window name: " + s);
                     currObjInfo = objInfo.GetObjectType(e);
                     actualString = currObjInfo.objType + s;
                     index = 1;
                     while (true)
                     {
                         if (windowArrayList.IndexOf(actualString) < 0)
                             break;
                         actualString = currObjInfo.objType + s + index;
                         index++;
                     }
                     windowArrayList.Add(actualString);
                 }
                 catch (ElementNotAvailableException ex)
                 {
                     // If window doesn't exist, remove it from list
                     windowTmpList.Add(e);
                     LogMessage(ex);
                 }
                 catch (Exception ex)
                 {
                     // Capture any unhandled exception,
                     // so that the framework won't crash
                     windowTmpList.Add(e);
                     LogMessage(ex);
                 }
             }
         }
         catch (Exception ex)
         {
             // Since window list is added / removed in different thread
             // values of windowList might be altered and an exception is thrown
             // Just handle the global exception
             LogMessage(ex);
         }
         try
         {
             foreach (AutomationElement e in windowTmpList)
                 utils.windowList.Remove(e);
         }
         catch (Exception ex)
         {
             LogMessage(ex);
         }
         // FIXME: Check whether resetting the ObjInfo is appropriate here
         objInfo = new ObjInfo(false);
         while (null != element)
         {
             if (utils.windowList.IndexOf(element) != -1)
             {
                 // As the window info already added to the windowArrayList
                 // let us not re-add it
                 LogMessage(element.Current.Name + " already in windowList");
                 element = w.walker.GetNextSibling(element);
                 continue;
             }
             s = element.Current.Name;
             LogMessage("Window name: " + s);
             currObjInfo = objInfo.GetObjectType(element);
             if (s == null || s == "")
                 actualString = currObjInfo.objType + currObjInfo.objCount;
             else
                 actualString = currObjInfo.objType + s;
             index = 1;
             while (true)
             {
                 if (windowArrayList.IndexOf(actualString) < 0)
                     break;
                 actualString = currObjInfo.objType + s + index;
                 index++;
             }
             windowArrayList.Add(actualString);
             try
             {
                 c = element.FindAll(TreeScope.Children, condition);
                 foreach (AutomationElement e in c)
                 {
                     s = e.Current.Name;
                     currObjInfo = objInfo.GetObjectType(e);
                     if (s == null || s == "")
                         actualString = currObjInfo.objType + currObjInfo.objCount;
                     else
                     {
                         actualString = currObjInfo.objType + s;
                         index = 1;
                         while (true)
                         {
                             if (windowArrayList.IndexOf(actualString) < 0)
                                 break;
                             actualString = currObjInfo.objType + s + index;
                             index++;
                         }
                     }
                     windowArrayList.Add(actualString);
                 }
             }
             catch (Exception ex)
             {
                 LogMessage(ex);
             }
             element = w.walker.GetNextSibling(element);
         }
         return windowArrayList.ToArray(typeof(string)) as string[];
     }
     catch (Exception ex)
     {
         LogMessage(ex);
     }
     finally
     {
         w = null;
         windowArrayList = null;
     }
     // Unable to find window
     return null;
 }
Exemplo n.º 28
0
 public bool GetObjectName(AutomationElement firstElement, ref ArrayList objectList,
     AutomationElement aimElement, ref string actualString)
 {
     if (firstElement == null)
     {
         LogMessage("Invalid object handle");
         return false;
     }
     CurrentObjInfo currObjInfo;
     ObjInfo objInfo = new ObjInfo(false);
     InternalTreeWalker w = new InternalTreeWalker();
     int index;
     string s = null;
     AutomationElement childElement;
     try
     {
         childElement = firstElement;
         while (childElement != null)
         {
             s = childElement.Current.Name;
             currObjInfo = objInfo.GetObjectType(childElement);
             if (s == null)
             {
                 LogMessage("Current object Name is null");
             }
             else
             {
                 s = s.ToString();
                 if (true)
                     LogMessage("Obj name: " + s + " : " +
                         childElement.Current.ControlType.ProgrammaticName);
             }
             if (currObjInfo.objType != null)
             {
                 if (s != null)
                     s = Regex.Replace(s, @"( |\t|:|\.|_|\r|\n|<|>)", "");
                 if (String.IsNullOrEmpty(s))
                 {
                     // txt0, txt1
                     actualString = currObjInfo.objType +
                         currObjInfo.objCount;
                 }
                 else
                 {
                     // txtName, txtPassword
                     actualString = currObjInfo.objType + s;
                     LogMessage("###" + actualString + "###");
                     index = 1;
                     while (true)
                     {
                         if (objectList.IndexOf(actualString) < 0)
                         {
                             // Object doesn't exist, assume this is the first
                             // element with the name and type
                             break;
                         }
                         actualString = currObjInfo.objType + s + index;
                         index++;
                     }
                 }
                 objectList.Add(actualString);
             }
             if (aimElement == childElement && aimElement.Current.BoundingRectangle ==
                 childElement.Current.BoundingRectangle)
             {
                 // Different controls on toolbar have same handle, probably it is a
                 // underlying bug with toolbar, so also use BoundingRectangle here
                 return true;
             }
             // If any subchild exist for the current element navigate to it
             AutomationElement subChild = w.walker.GetFirstChild(childElement);
             if (subChild != null)
             {
                 if (GetObjectName(subChild, ref objectList, aimElement,
                     ref actualString))
                     return true;
             }
             childElement = w.walker.GetNextSibling(childElement);
         }
         // If aimElement is not found, set actualString as None
         actualString = "None";
     }
     catch (ElementNotAvailableException ex)
     {
         LogMessage("Exception: " + ex);
     }
     catch (Exception ex)
     {
         LogMessage(ex);
     }
     finally
     {
         w = null;
     }
     return false;
 }
Exemplo n.º 29
0
        internal AutomationElement InternalWaitTillControlTypeExist(ControlType type,
            int processId, int guiTimeOut = 30)
        {
            InternalTreeWalker w;
            AutomationElementCollection c;
            Condition condition = new PropertyCondition(
                AutomationElement.ControlTypeProperty,
                ControlType.Menu);
            w = new InternalTreeWalker();
            try
            {
                int waitTime = 0;
                while (waitTime < guiTimeOut)
                {
                    AutomationElement element = w.walker.GetFirstChild(AutomationElement.RootElement);
                    while (null != element)
                    {
                        try
                        {
                            int Index = -1;

                            // In case of an exception, the program adds the element as
                            // new element to the windowList. Now I see my missing Qt window.
                            try
                            {
                                Index = windowList.IndexOf(element);
                            }
                            catch (Exception ex)
                            {
                                LogMessage(ex);
                            }

                            if (Index == -1)
                                windowList.Add(element);
/*
                            if (windowList.IndexOf(element) == -1) // original code
                                // Add parent window handle,
                                // if it doesn't exist
                                windowList.Add(element);
 */ 
                        }
                        catch (Exception ex)
                        {
                            LogMessage(ex); // only a try, perhaps the try around IndexOf is enough
                        }
                        try
                        {
                            if (element.Current.ProcessId != processId)
                            {
                                // app name doesn't match
                                element = w.walker.GetNextSibling(element);
                                continue;
                            }
                            if (element.Current.ControlType == type)
                                return element;
                        }
                        catch
                        {
                            // Something went wrong, since app name
                            // is provided, search for next app
                            element = w.walker.GetNextSibling(element);
                            continue;
                        }
                        c = element.FindAll(TreeScope.Subtree, condition);
                        foreach (AutomationElement e in c)
                        {
                            try
                            {
                                if (e.Current.ControlType == type)
                                    return e;
                            }
                            catch
                            {
                                // Something went wrong, since app name
                                // is provided, search for next app
                                element = w.walker.GetNextSibling(element);
                                continue;
                            }
                        }
                        element = w.walker.GetNextSibling(element);
                        waitTime++;
                        //InternalWait(1);
                    }
                }
            }
            catch (Exception ex)
            {
                LogMessage(ex);
            }
            return null;
        }
Exemplo n.º 30
0
 public string GetObjectProperty(String windowName, String objName,
     string property)
 {
     if (String.IsNullOrEmpty(windowName) ||
         String.IsNullOrEmpty(objName) ||
         String.IsNullOrEmpty(property))
     {
         throw new XmlRpcFaultException(123, "Argument cannot be empty.");
     }
     bool flag;
     string matchedKey = "";
     ArrayList objectList = new ArrayList();
     Hashtable objectHT = new Hashtable();
     InternalTreeWalker w;
     AutomationElement windowHandle = utils.GetWindowHandle(windowName);
     if (windowHandle == null)
     {
         throw new XmlRpcFaultException(123,
             "Unable to find window: " + windowName);
     }
     w = new InternalTreeWalker();
     Hashtable ht;
     ArrayList childrenList;
     try
     {
         if (property == "children")
             flag = true;
         else
             flag = false;
         if (utils.InternalGetObjectList(w.walker.GetFirstChild(windowHandle),
             ref objectList, ref objectHT, ref matchedKey,
             flag, objName, windowHandle.Current.Name) || flag)
         {
             if (utils.debug || utils.writeToFile != null)
             {
                 LogMessage(objectList.Count);
                 foreach (string key in objectHT.Keys)
                 {
                     LogMessage("Key: " +
                         ((Hashtable)objectHT[key])["key"]);
                     LogMessage("Parent: " +
                         ((Hashtable)objectHT[key])["parent"]);
                     LogMessage("Obj index: " +
                         ((Hashtable)objectHT[key])["obj_index"]);
                     LogMessage("Class: " +
                         ((Hashtable)objectHT[key])["class"]);
                     foreach (string child in
                         (ArrayList)((Hashtable)objectHT[key])["children"])
                         LogMessage("Children: " + child);
                 }
             }
             LogMessage(objectHT.Count + " : " + objectList.Count);
             LogMessage(objectList[objectList.Count - 1]);
             LogMessage("matchedKey: " + matchedKey + " : " + flag);
             ht = (Hashtable)objectHT[matchedKey];
             if (ht != null)
             {
                 foreach (string key in ht.Keys)
                 {
                     LogMessage(key);
                     if (key == property)
                     {
                         if (property == "children")
                         {
                             childrenList = (ArrayList)ht[key];
                             LogMessage("Count: " + childrenList.Count);
                             string value = "";
                             foreach (string child in childrenList)
                             {
                                 if (value == "")
                                     value = child;
                                 else
                                     value += ", " + child;
                             }
                             return value;
                         }
                         else
                             return (string)ht[key];
                     }
                 }
             }
         }
     }
     catch (Exception ex)
     {
         LogMessage(ex);
         if (ex is XmlRpcFaultException)
             throw;
         else
             throw new XmlRpcFaultException(123,
                 "Unhandled exception: " + ex.Message);
     }
     finally
     {
         w = null;
         ht = null;
         windowHandle = null;
     }
     throw new XmlRpcFaultException(123, "Unable to find Object property: " +
         property + " of object: " + objName);
 }
Exemplo n.º 31
0
 private int InternalComboHandler(String windowName, String objName,
     String item, ref String selectedItem, String actionType = "Select",
     ArrayList childList = null)
 {
     bool verify = actionType == "Verify" ? true : false;
     ControlType[] comboTtype = new ControlType[3] { ControlType.ComboBox,
         ControlType.ListItem, ControlType.List/*, ControlType.Text */ };
     AutomationElement childHandle = utils.GetObjectHandle(windowName,
         objName, comboTtype, !verify);
     Object pattern = null;
     Object invokePattern = null;
     AutomationElement elementItem = null;
     ControlType[]  type = new ControlType[1] { ControlType.Button };
     try
     {
         LogMessage("Handle name: " + childHandle.Current.Name +
             " - " + childHandle.Current.ControlType.ProgrammaticName);
         if (!utils.IsEnabled(childHandle, !verify))
         {
             throw new XmlRpcFaultException(123, "Object state is disabled");
         }
         elementItem = utils.GetObjectHandle(childHandle, "Open", type, true);
         LogMessage("elementItem: " + elementItem.Current.Name +
                             " - " + elementItem.Current.ControlType.ProgrammaticName);
         if (childHandle.TryGetCurrentPattern(ExpandCollapsePattern.Pattern,
             out pattern) || childHandle.TryGetCurrentPattern(
             InvokePattern.Pattern, out invokePattern) ||
             (elementItem != null && elementItem.TryGetCurrentPattern(
             InvokePattern.Pattern, out invokePattern)))
         {
             LogMessage("ExpandCollapsePattern");
             // Retry max 5 times
             for (int i = 0; i < 5; i++)
             {
                 switch (actionType)
                 {
                     case "Hide":
                         if (invokePattern != null)
                         {
                             ((InvokePattern)invokePattern).Invoke();
                             return 1;
                         }
                         else if (pattern != null)
                         {
                             ((ExpandCollapsePattern)pattern).Collapse();
                             // Required to wait 1 second,
                             // before checking the state and retry collapsing
                             utils.InternalWait(1);
                             if (((ExpandCollapsePattern)pattern).Current.ExpandCollapseState ==
                                 ExpandCollapseState.Collapsed)
                             {
                                 // Hiding same combobox multiple time consecutively
                                 // fails. Check for the state and retry to collapse
                                 LogMessage("Collapsed");
                                 return 1;
                             }
                         }
                         break;
                     case "Show":
                     case "Select":
                     case "Verify":
                         elementItem = utils.GetObjectHandle(childHandle, "Open",
                             type, !verify);
                         if (invokePattern != null || (elementItem != null &&
                             elementItem.TryGetCurrentPattern(InvokePattern.Pattern,
                             out invokePattern)))
                         {
                             ((InvokePattern)invokePattern).Invoke();
                         }
                         else if (pattern != null)
                             ((ExpandCollapsePattern)pattern).Expand();
                         // Required to wait 1 second,
                         // before checking the state and retry expanding
                         utils.InternalWait(1);
                         if (invokePattern != null || (pattern != null &&
                             ((ExpandCollapsePattern)pattern).Current.ExpandCollapseState ==
                             ExpandCollapseState.Expanded))
                         {
                             // Selecting same combobox multiple time consecutively
                             // fails. Check for the state and retry to expand
                             LogMessage("Expaneded");
                             if (actionType == "Show")
                                 return 1;
                             else
                             {
                                 return SelectListItem(childHandle, item, verify) ? 1 : 0;
                             }
                         }
                         break;
                     case "GetComboValue":
                         Object selectionPattern = null;
                         LogMessage("GetComboValue");
                         elementItem = utils.GetObjectHandle(childHandle, "Open",
                             type, true);
                         if (invokePattern != null || (elementItem != null &&
                             elementItem.TryGetCurrentPattern(InvokePattern.Pattern,
                             out invokePattern)))
                         {
                             LogMessage("InvokePattern");
                             childHandle.SetFocus();
                             utils.InternalClick(elementItem);
                             // InvokePattern doesn't work with Virtual Network
                             // Editor of VMware Workstation, so used the above InternalClick
                             //((InvokePattern)invokePattern).Invoke();
                         }
                         else if (pattern != null)
                         {
                             LogMessage("ExpandCollapsePattern");
                             ((ExpandCollapsePattern)pattern).Expand();
                         }
                         // Required to wait 1 second,
                         // before checking the state and retry expanding
                         utils.InternalWait(1);
                         LogMessage("Handle name: " + childHandle.Current.Name +
                             " - " + childHandle.Current.ControlType.ProgrammaticName);
                         bool typeExist = utils.InternalWaitTillChildControlTypeExist(childHandle, comboTtype);
                         LogMessage("Control type exist: " + typeExist);
                         AutomationElementCollection c = childHandle.FindAll(TreeScope.Subtree,
                             Condition.TrueCondition);
                         LogMessage("AutomationElementCollection " + c.Count);
                         foreach (AutomationElement e in c)
                         {
                             LogMessage(e.Current.Name + " : " + e.Current.ControlType.ProgrammaticName);
                             bool status = false;
                             if (e.TryGetCurrentPattern(SelectionItemPattern.Pattern,
                                 out selectionPattern))
                             {
                                 status = ((SelectionItemPattern)selectionPattern).Current.IsSelected;
                                 if (status)
                                 {
                                     LogMessage("Selected: " + e.Current.Name);
                                     selectedItem = e.Current.Name;
                                     ((ExpandCollapsePattern)pattern).Collapse();
                                     return 1;
                                 }
                             }
                         }
                         LogMessage("Unable to find selected combo box value");
                         c = null;
                         selectionPattern = null;
                         if (invokePattern != null)
                         {
                             ((InvokePattern)invokePattern).Invoke();
                         }
                         else if (pattern != null)
                             ((ExpandCollapsePattern)pattern).Collapse();
                         return 0;
                     case "GetAllItem":
                         string matchedKey = null;
                         Hashtable objectHT = new Hashtable();
                         ArrayList tmpChildList = new ArrayList();
                         InternalTreeWalker w = new InternalTreeWalker();
                         elementItem = utils.GetObjectHandle(childHandle, "Open",
                             type, true);
                         // Changes based on QT 5.0.2
                         if (invokePattern != null || (elementItem != null &&
                             elementItem.TryGetCurrentPattern(InvokePattern.Pattern,
                             out invokePattern)))
                         {
                             ((InvokePattern)invokePattern).Invoke();
                         }
                         else if (pattern != null)
                             ((ExpandCollapsePattern)pattern).Expand();
                         // Required to wait 1 second,
                         // before checking the state and retry expanding
                         utils.InternalWait(1);
                         utils.InternalGetObjectList(
                             w.walker.GetFirstChild(childHandle),
                             ref tmpChildList, ref objectHT, ref matchedKey,
                             true, null, null, ControlType.ListItem);
                         if (invokePattern != null)
                         {
                             ((InvokePattern)invokePattern).Invoke();
                         }
                         else if (pattern != null)
                             ((ExpandCollapsePattern)pattern).Collapse();
                         // For Linux compatibility
                         Hashtable propertyHT;
                         foreach (String key in objectHT.Keys)
                         {
                             propertyHT = (Hashtable)objectHT[key];
                             string className = (string)propertyHT["class"];
                             if (className != null &&
                                 className.Contains("list_item"))
                                 // Add only list items
                                 childList.Add(propertyHT["label"]);
                         }
                         w = null;
                         tmpChildList = null;
                         propertyHT = objectHT = null;
                         if (childList.Count > 0)
                         {
                             // Don't process the last item
                             return 1;
                         }
                         else
                         {
                             LogMessage("childList.Count <= 0: " + childList.Count);
                         }
                         return 0;
                 }
             }
         }
         // Handle selectitem and verifyselect on list.
         // Get ExpandCollapsePattern fails on list,
         // VM Library items are selected and
         // verified correctly on Player with this fix
         else
         {
             LogMessage("SelectListItem");
             childHandle.SetFocus();
             return SelectListItem(childHandle, item, verify) ? 1 : 0;
         }
     }
     catch (Exception ex)
     {
         LogMessage(ex);
         if (ex is XmlRpcFaultException)
             throw;
         else
             throw new XmlRpcFaultException(123,
                 "Unhandled exception: " + ex.Message);
     }
     finally
     {
         comboTtype = type = null;
         pattern = invokePattern = null;
         elementItem = childHandle = null;
     }
     return 0;
 }
Exemplo n.º 32
0
 private int InternalComboHandler(String windowName, String objName,
     String item, String actionType = "Select",
     ArrayList childList = null)
 {
     AutomationElement childHandle = GetObjectHandle(windowName,
         objName);
     Object pattern = null;
     try
     {
         LogMessage("Handle name: " + childHandle.Current.Name +
             " - " + childHandle.Current.ControlType.ProgrammaticName);
         if (!utils.IsEnabled(childHandle))
         {
             throw new XmlRpcFaultException(123, "Object state is disabled");
         }
         if (childHandle.TryGetCurrentPattern(ExpandCollapsePattern.Pattern,
             out pattern))
         {
             LogMessage("ExpandCollapsePattern");
             // Retry max 5 times
             for (int i = 0; i < 5; i++)
             {
                 switch (actionType)
                 {
                     case "Hide":
                         ((ExpandCollapsePattern)pattern).Collapse();
                         // Required to wait 1 second,
                         // before checking the state and retry collapsing
                         utils.InternalWait(1);
                         if (((ExpandCollapsePattern)pattern).Current.ExpandCollapseState ==
                             ExpandCollapseState.Collapsed)
                         {
                             // Hiding same combobox multiple time consecutively
                             // fails. Check for the state and retry to collapse
                             LogMessage("Collapsed");
                             return 1;
                         }
                         break;
                     case "Show":
                     case "Select":
                     case "Verify":
                         ((ExpandCollapsePattern)pattern).Expand();
                         // Required to wait 1 second,
                         // before checking the state and retry expanding
                         utils.InternalWait(1);
                         if (((ExpandCollapsePattern)pattern).Current.ExpandCollapseState ==
                             ExpandCollapseState.Expanded)
                         {
                             // Selecting same combobox multiple time consecutively
                             // fails. Check for the state and retry to expand
                             LogMessage("Expaneded");
                             if (actionType == "Show")
                                 return 1;
                             else
                             {
                                 childHandle.SetFocus();
                                 bool verify = actionType == "Verify" ? true : false;
                                 return SelectListItem(childHandle, item, verify) ? 1 : 0;
                             }
                         }
                         break;
                     case "GetAllItem":
                         string matchedKey = null;
                         Hashtable objectHT = new Hashtable();
                         InternalTreeWalker w = new InternalTreeWalker();
                         utils.InternalGetObjectList(
                             w.walker.GetFirstChild(childHandle),
                             ref childList, ref objectHT, ref matchedKey,
                             false, null, null, ControlType.ListItem);
                         w = null;
                         objectHT = null;
                         if (childList.Count > 0)
                         {
                             // Don't process the last item
                             return 1;
                         }
                         else
                         {
                             LogMessage("childList.Count <= 0: " + childList.Count);
                         }
                         return 0;
                 }
             }
         }
         // Handle selectitem and verifyselect on list.
         // Get ExpandCollapsePattern fails on list,
         // VM Library items are selected and
         // verified correctly on Player with this fix
         else
         {
             childHandle.SetFocus();
             bool verify = actionType == "Verify" ? true : false;
             return SelectListItem(childHandle, item, verify) ? 1 : 0;
         }
     }
     catch (Exception ex)
     {
         LogMessage(ex);
         if (ex is XmlRpcFaultException)
             throw;
         else
             throw new XmlRpcFaultException(123,
                 "Unhandled exception: " + ex.Message);
     }
     finally
     {
         pattern = null;
         childHandle = null;
     }
     return 0;
 }
Exemplo n.º 33
0
Arquivo: Utils.cs Projeto: harig/cobra
 private AutomationElement InternalGetWindowHandle(String windowName,
     ControlType[] type = null, bool waitTillGuiNotExist = false)
 {
     String s;
     int index;
     String actualString;
     AutomationElement element;
     CurrentObjInfo currObjInfo;
     AutomationElementCollection c;
     ObjInfo objInfo = new ObjInfo(false);
     ArrayList objectList = new ArrayList();
     // Trying to mimic python fnmatch.translate
     String tmp = Regex.Replace(windowName, @"\*", @".*");
     tmp = Regex.Replace(tmp, @"\?", @".");
     tmp = Regex.Replace(tmp, @"\\", @"\\");
     tmp = Regex.Replace(tmp, "( |\r|\n)", "");
     tmp = @"\A(?ms)" + tmp + @"\Z(?ms)";
     //tmp += @"\Z(?ms)";
     Regex rx = new Regex(tmp, RegexOptions.Compiled |
         RegexOptions.IgnorePatternWhitespace | RegexOptions.Multiline |
         RegexOptions.CultureInvariant);
     List<AutomationElement> windowTmpList = new List<AutomationElement>();
     InternalTreeWalker w;
     Condition condition;
     try
     {
         foreach (AutomationElement e in windowList)
         {
             try
             {
                 Rect rect = e.Current.BoundingRectangle;
                 if (rect.Width == 0 && rect.Height == 0)
                 {
                     // Window no longer exist
                     windowTmpList.Add(e);
                     continue;
                 }
                 currObjInfo = objInfo.GetObjectType(e);
                 s = e.Current.Name;
                 if (s != null)
                     s = Regex.Replace(s, "( |\r|\n)", "");
                 if (String.IsNullOrEmpty(s))
                 {
                     // txt0, txt1
                     actualString = currObjInfo.objType +
                         currObjInfo.objCount;
                 }
                 else
                 {
                     // txtName, txtPassword
                     actualString = currObjInfo.objType + s;
                     index = 1;
                     while (true)
                     {
                         if (objectList.IndexOf(actualString) < 0)
                         {
                             // Object doesn't exist, assume this is the first
                             // element with the name and type
                             break;
                         }
                         actualString = currObjInfo.objType + s + index;
                         index++;
                     }
                 }
                 LogMessage("Window: " + actualString + " : " + tmp);
                 objectList.Add(actualString);
                 // FIXME: Handle dlg0 as in Linux
                 if ((s != null && rx.Match(s).Success) ||
                     rx.Match(actualString).Success)
                 {
                     if (type == null)
                     {
                         LogMessage(windowName + " - Window found");
                         rx = null;
                         objectList = null;
                         return e;
                     }
                     else
                     {
                         foreach (ControlType t in type)
                         {
                             if (debug || writeToFile != null)
                                 LogMessage((t == e.Current.ControlType) +
                                     " : " + e.Current.ControlType.ProgrammaticName);
                             if (t == e.Current.ControlType)
                             {
                                 rx = null;
                                 objectList = null;
                                 return e;
                             }
                         }
                         LogMessage("type doesn't match !!!!!!!!!!!!!!");
                     }
                 }
             }
             catch (ElementNotAvailableException ex)
             {
                 // Don't alter the current list, remove it later
                 windowTmpList.Add(e);
                 LogMessage(ex);
             }
             catch (Exception ex)
             {
                 // Don't alter the current list, remove it later
                 windowTmpList.Add(e);
                 LogMessage(ex);
             }
         }
     }
     catch (Exception ex)
     {
         LogMessage(ex);
     }
     finally
     {
         try
         {
             foreach (AutomationElement e in windowTmpList)
                 try
                 {
                     // Remove element from the list
                     windowList.Remove(e);
                 }
                 catch (Exception ex)
                 {
                     LogMessage(ex);
                 }
             windowTmpList = null;
         }
         catch (Exception ex)
         {
             LogMessage(ex);
         }
     }
     if (waitTillGuiNotExist)
         // If window doesn't exist for waitTillGuiNotExist
         // return here as we don't need to find the window handle
         return null;
     condition = new PropertyCondition(
         AutomationElement.ControlTypeProperty,
         ControlType.Window);
     w = new InternalTreeWalker();
     windowTmpList = null;
     objectList.Clear();
     objInfo = new ObjInfo(false);
     element = w.walker.GetFirstChild(AutomationElement.RootElement);
     try
     {
         while (null != element)
         {
             try
             {
                 if (windowList.IndexOf(element) == -1)
                     // Add parent window handle,
                     // if it doesn't exist
                     windowList.Add(element);
             }
             catch (System.UnauthorizedAccessException ex)
             {
                 // https://bugzilla.gnome.org/show_bug.cgi?id=706992
                 // Cobra looses all objects after steps specified inside
                 LogMessage(ex);
                 InternalWait(2);
                 element = w.walker.GetFirstChild(AutomationElement.RootElement);
                 windowList.Clear();
                 continue;
             }
             if (!String.IsNullOrEmpty(appUnderTest))
             {
                 // If app under test doesn't match
                 // continue searching next app
                 Process process;
                 // Get a process using the process id.
                 try
                 {
                     process = Process.GetProcessById(element.Current.ProcessId);
                     if (process.ProcessName != appUnderTest)
                     {
                         // app name doesn't match
                         element = w.walker.GetNextSibling(element);
                         continue;
                     }
                 }
                 catch
                 {
                     // Something went wrong, since app name
                     // is provided, search for next app
                     element = w.walker.GetNextSibling(element);
                     continue;
                 }
             }
             c = element.FindAll(TreeScope.Subtree, condition);
             foreach (AutomationElement e in c)
             {
                 if (windowList.IndexOf(e) == -1)
                     // Add sub window handle, if it doesn't
                     // exist
                     windowList.Add(e);
                 currObjInfo = objInfo.GetObjectType(e);
                 s = e.Current.Name;
                 if (s != null)
                     s = Regex.Replace(s, "( |\r|\n)", "");
                 if (s == null || s == "")
                 {
                     // txt0, txt1
                     actualString = currObjInfo.objType +
                         currObjInfo.objCount;
                 }
                 else
                 {
                     // txtName, txtPassword
                     actualString = currObjInfo.objType + s;
                     index = 1;
                     while (true)
                     {
                         if (objectList.IndexOf(actualString) < 0)
                         {
                             // Object doesn't exist, assume this is the first
                             // element with the name and type
                             break;
                         }
                         actualString = currObjInfo.objType + s + index;
                         index++;
                     }
                 }
                 LogMessage("Window dynamic: " + actualString + " : " + tmp);
                 objectList.Add(actualString);
                 // FIXME: Handle dlg0 as in Linux
                 if ((s != null && rx.Match(s).Success) ||
                     rx.Match(actualString).Success)
                 {
                     if (type == null)
                     {
                         LogMessage(windowName + " - Window found");
                         return e;
                     }
                     else
                     {
                         foreach (ControlType t in type)
                         {
                             if (debug || writeToFile != null)
                                 LogMessage((t == e.Current.ControlType) +
                                     " : " + e.Current.ControlType.ProgrammaticName);
                             if (t == e.Current.ControlType)
                             {
                                 return e;
                             }
                         }
                         LogMessage("type doesn't match !!!!!!!!!!!!!!");
                     }
                 }
             }
             // Get next parent window handle in the list
             element = w.walker.GetNextSibling(element);
         }
     }
     catch (Exception ex)
     {
         LogMessage(ex);
     }
     finally
     {
         c = null;
         w = null;
         rx = null;
         condition = null;
         objectList = null;
     }
     // Unable to find window
     return null;
 }
Exemplo n.º 34
0
        private int InternalMenuHandler(String windowName, String objName,
            ref ArrayList menuList, String actionType = "Select")
        {
            if (String.IsNullOrEmpty(windowName) ||
                String.IsNullOrEmpty(objName))
            {
                throw new XmlRpcFaultException(123, "Argument cannot be empty.");
            }
            Object pattern = null;
            String currObjName = null;
            AutomationElementCollection c = null;
            ControlType[] type = new ControlType[3] { ControlType.Menu,
                ControlType.MenuBar, ControlType.MenuItem };
            ControlType[] controlType = new ControlType[3] { ControlType.Menu,
                ControlType.MenuItem, ControlType.MenuBar };
            AutomationElement tmpContextHandle = null;
            AutomationElement windowHandle, childHandle;
            AutomationElement prevObjHandle = null, firstObjHandle = null;

            InternalTreeWalker w = new InternalTreeWalker();
            try
            {
                windowHandle = utils.GetWindowHandle(windowName);
                if (windowHandle == null)
                {
                    throw new XmlRpcFaultException(123,
                        "Unable to find window: " + windowName);
                }
                processId = windowHandle.Current.ProcessId;
                windowHandle.SetFocus();
                LogMessage("Window name: " + windowHandle + " : " +
                    windowHandle.Current.Name +
                    " : " + windowHandle.Current.ControlType.ProgrammaticName);
                childHandle = windowHandle;
                /*
                // element is an AutomationElement.
                AutomationPattern[] patterns = childHandle.GetSupportedPatterns();
                foreach (AutomationPattern pattern1 in patterns)
                {
                    Console.WriteLine("ProgrammaticName: " + pattern1.ProgrammaticName);
                    Console.WriteLine("PatternName: " + Automation.PatternName(pattern1));
                }
                /**/
                while (true)
                {
                    if (objName.Contains(";"))
                    {
                        int index = objName.IndexOf(";",
                            StringComparison.CurrentCulture);
                        currObjName = objName.Substring(0, index);
                        objName = objName.Substring(index + 1);
                    }
                    else
                    {
                        currObjName = objName;
                    }
                    LogMessage("childHandle: " + childHandle.Current.Name +
                        " : " + currObjName + " : " +
                        childHandle.Current.ControlType.ProgrammaticName);
                    childHandle = utils.GetObjectHandle(childHandle,
                        currObjName, type, false);
                    if (childHandle == null)
                    {
                        if (currObjName == objName)
                        {
                            throw new XmlRpcFaultException(123,
                                "Unable to find Object: " + objName);
                        }
                        else
                        {
                            throw new XmlRpcFaultException(123,
                                "Unable to find Object: " + currObjName);
                        }
                    }
                    // Store previous handle for later use
                    prevObjHandle = childHandle;
                    if (firstObjHandle == null)
                    {
                        // Save it for later use
                        firstObjHandle = childHandle;
                    }
                    if ((actionType == "Select" || actionType == "SubMenu" ||
                        actionType == "Check" || actionType == "UnCheck" ||
                        actionType == "VerifyCheck" || actionType == "Window") &&
                        !utils.IsEnabled(childHandle, false))
                    {
                        throw new XmlRpcFaultException(123,
                            "Object state is disabled");
                    }
                    try
                    {
                        if (actionType == "Window")
                        {
                            utils.InternalClick(childHandle);
                        }
                        else
                        {
                            // SetFocus() fails on Windows Explorer
                            childHandle.SetFocus();
                        }
                    }
                    catch (Exception ex)
                    {
                        LogMessage(ex);
                    }
                    if (childHandle.TryGetCurrentPattern(InvokePattern.Pattern,
                        out pattern) || childHandle.TryGetCurrentPattern(
                        ExpandCollapsePattern.Pattern, out pattern))
                    {
                        if (actionType == "Select" || currObjName != objName ||
                             actionType == "SubMenu" || actionType == "VerifyCheck" ||
                             actionType == "Window")
                        {
                            try
                            {
                                LogMessage("Invoking menu item: " + currObjName +
                                    " : " + objName + " : " +
                                    childHandle.Current.ControlType.ProgrammaticName +
                                    " : " + childHandle.Current.Name);
                            }
                            catch (Exception ex)
                            {
                                // Noticed with closewindow() to close Notepad
                                //    System.UnauthorizedAccessException: Access is denied
                                //       Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)
                                LogMessage(ex);
                            }
                            if (actionType != "Window")
                            {
                                try
                                {
                                    // SetFocus() fails on Windows Explorer
                                    childHandle.SetFocus();
                                }
                                catch (Exception ex)
                                {
                                    LogMessage(ex);
                                }
                            }
                            if (!(actionType == "VerifyCheck" && currObjName == objName) &&
                                (actionType != "Window"))
                            {
                                utils.InternalClick(childHandle);
                            }
                            try
                            {
                                // Invoke doesn't work for VMware Workstation
                                // But they work for Notepad
                                // MoveToAndClick works for VMware Workstation
                                // But not for Notepad (on first time)
                                // Requires 2 clicks !
                                //((InvokePattern)pattern).Invoke();
                                utils.InternalWait(1);
                                c = childHandle.FindAll(TreeScope.Children,
                                    Condition.TrueCondition);
                            }
                            catch (System.NotImplementedException ex)
                            {
                                // Noticed with VMware Workstation
                                //    System.Runtime.InteropServices.COMException (0x80040200):
                                //       Exception from HRESULT: 0x80040200
                                LogMessage("NotImplementedException");
                                LogMessage(ex);
                            }
                            catch (System.Windows.Automation.ElementNotEnabledException ex)
                            {
                                // Noticed with VMware Workstation
                                //    System.Runtime.InteropServices.COMException (0x80040200):
                                //       Exception from HRESULT: 0x80040200
                                LogMessage("Element not enabled");
                                LogMessage(ex);
                            }
                            catch (Exception ex)
                            {
                                LogMessage(ex);
                            }
                        }
                    }
                    if (currObjName == objName && actionType != "SubMenu")
                    {
                        int state;
                        switch (actionType)
                        {
                            case "Select":
                            case "Window":
                                // No child menu item to be processed
                                return 1;
                            case "Check":
                            case "UnCheck":
                                state = IsMenuChecked(childHandle);
                                LogMessage("IsMenuChecked(childHandle): " +
                                    childHandle.Current.ControlType.ProgrammaticName);
                                LogMessage("actionType: " + actionType);
                                // Don't process the last item
                                if (actionType == "Check")
                                {
                                    if (state == 1)
                                        // Already checked, just click back the main menu
                                        utils.InternalClick(firstObjHandle);
                                    else
                                        // Check menu
                                        utils.InternalClick(childHandle);
                                    return 1;
                                }
                                else if (actionType == "UnCheck")
                                {
                                    if (state == 0)
                                        // Already unchecked, just click back the main menu
                                        utils.InternalClick(firstObjHandle);
                                    else
                                        // Uncheck menu
                                        utils.InternalClick(childHandle);
                                    return 1;
                                }
                                break;
                            case "Exist":
                            case "Enabled":
                                state = utils.IsEnabled(childHandle) == true ? 1 : 0;
                                LogMessage("IsEnabled(childHandle): " +
                                    childHandle.Current.Name + " : " + state);
                                LogMessage("IsEnabled(childHandle): " +
                                    childHandle.Current.ControlType.ProgrammaticName);
                                // Set it back to old state, else the menu selection left there
                                utils.InternalClick(firstObjHandle);
                                // Don't process the last item
                                if (actionType == "Enabled")
                                    return state;
                                else if (actionType == "Exist")
                                    return 1;
                                break;
                            case "SubMenu":
                                int status = HandleSubMenu(w.walker.GetFirstChild(childHandle),
                                    firstObjHandle, ref menuList);
                                if (status == 1)
                                    return 1;
                                break;
                            case "VerifyCheck":
                                state = IsMenuChecked(childHandle);
                                utils.InternalClick(firstObjHandle);
                                return state;
                            default:
                                break;
                        }
                    }
                    else if ((tmpContextHandle = utils.InternalWaitTillControlTypeExist(
                        ControlType.Menu, processId, 3)) != null)
                    {
                        LogMessage("InternalWaitTillControlTypeExist");
                        // Find object from current handle, rather than navigating
                        // the complete window
                        childHandle = tmpContextHandle;
                        if (actionType != "SubMenu")
                            continue;
                        else if (currObjName == objName)
                        {
                            switch (actionType)
                            {
                                case "SubMenu":
                                    int status = HandleSubMenu(w.walker.GetFirstChild(childHandle),
                                        firstObjHandle, ref menuList);
                                    if (status == 1)
                                        return 1;
                                    break;
                            }
                        }
                    }
                    else if (c != null && c.Count > 0)
                    {
                        if (currObjName == objName)
                        {
                            switch (actionType)
                            {
                                case "SubMenu":
                                    int status = HandleSubMenu(w.walker.GetFirstChild(childHandle),
                                        firstObjHandle, ref menuList);
                                    if (status == 1)
                                        return 1;
                                    break;
                            }
                        }
                        LogMessage("c != null && c.Count > 0");
                        childHandle = windowHandle;
                        continue;
                    }
                    // Required for Notepad like app
                    if ((c == null || c.Count == 0))
                    {
                        LogMessage("Work around for Windows application");
                        LogMessage(windowHandle.Current.Name + " : " + objName);
                        AutomationElement tmpChildHandle = utils.GetObjectHandle(
                            windowHandle, objName,
                            type, false);
                        // Work around for Notepad, as it doesn't find the menuitem
                        // on clicking any menu
                        if (tmpChildHandle != null)
                        {
                            LogMessage("Work around: tmpChildHandle != null");
                            if (actionType == "SubMenu" && currObjName == objName)
                                // Work around for Notepad like app
                                childHandle = tmpChildHandle;
                            else
                                // Work around for Notepad like app,
                                // but for actionType other than SubMenu
                                childHandle = windowHandle;
                        }
                    }
                    if (currObjName == objName)
                    {
                        switch (actionType)
                        {
                            case "SubMenu":
                                int status = HandleSubMenu(w.walker.GetFirstChild(childHandle),
                                    firstObjHandle, ref menuList);
                                if (status == 1)
                                    return 1;
                                break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogMessage(ex);
                if (firstObjHandle != null && actionType != "Window")
                {
                    // Set it back to old state, else the menu selection left there
                    utils.InternalClick(firstObjHandle);
                }
                if (((ex is ElementNotAvailableException) ||
                    (ex is UnauthorizedAccessException)) &&
                    actionType == "Window")
                {
                    // API closewindow() can close Windows Explorer on XP, but:
                    // -----------------------------------------------------------
                    // if (childHandle.TryGetCurrentPattern(InvokePattern.Pattern,
                    //     out pattern) || childHandle.TryGetCurrentPattern(
                    //     ExpandCollapsePattern.Pattern, out pattern))
                    // -----------------------------------------------------------
                    // Sometimes above code will throw exception, sometimes not:
                    //    System.Runtime.InteropServices.COMException (0x80040201):
                    //       Exception from HRESULT: 0x80040201
                    //    System.UnauthorizedAccessException, Access is denied:
                    //       Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
                    // So use this if block as workaround
                    return 1;
                }
                if (ex is XmlRpcFaultException)
                    throw;
                else
                    throw new XmlRpcFaultException(123,
                        "Unhandled exception: " + ex.Message);
            }
            finally
            {
                c = null;
                w = null;
                pattern = null;
                windowHandle = childHandle = null;
                prevObjHandle = firstObjHandle = null;
            }
        }
Exemplo n.º 35
0
Arquivo: Utils.cs Projeto: harig/cobra
        internal bool InternalGetObjectList(AutomationElement windowHandle,
            ref ArrayList objectList, ref Hashtable objectHT,
            ref string matchedKey, bool needAll = false,
            string objName = null, string parentName = null,
            ControlType type = null, ObjInfo objInfo = null)
        {
            if (windowHandle == null)
            {
                LogMessage("Invalid window handle");
                return false;
            }
            int index;
            Regex rx = null;
            String s = null;
            if (objInfo == null)
                objInfo = new ObjInfo(false);
            string objIndex;
            // Trying to mimic python fnmatch.translate
            string tmp = null;
            string utf8 = null;
            Hashtable propertyHT;
            bool isObjIndex = false;
            byte[] utf8Bytes = null;
            String actualString = null;
            String automationId = null;
            CurrentObjInfo currObjInfo;
            bool isAutomationId = false;

            InternalTreeWalker w = new InternalTreeWalker();
            if (objName != null)
            {
                if (Regex.IsMatch(objName, @"^#"))
                {
                    isAutomationId = true;
                    // Object id format: #AutomationId
                    automationId = objName.Split(new Char[] { '#' })[1];
                }
                else if (Regex.IsMatch(objName, @"#"))
                {
                    isObjIndex = true;
                }
                tmp = Regex.Replace(objName, @"( |:|\.|_|\r|\n|<|>)", "");
                tmp = Regex.Replace(tmp, @"\*", @".*");
                tmp = Regex.Replace(tmp, @"\?", @".");
                tmp = Regex.Replace(tmp, @"\\", @"\\");
                tmp = Regex.Replace(tmp, @"\(", @"\(");
                tmp = Regex.Replace(tmp, @"\)", @"\)");
                tmp = Regex.Replace(tmp, @"\+", @"\+");
                tmp = Regex.Replace(tmp, @"\#", @"\#");
                tmp = @"\A(?ms)" + tmp + @"\Z(?ms)";
                rx = new Regex(tmp, RegexOptions.Compiled |
                       RegexOptions.IgnorePatternWhitespace | RegexOptions.Multiline |
                       RegexOptions.CultureInvariant);
            }
            try
            {
                AutomationElement element = windowHandle;
                while (null != element)
                {
                    objIndex = null;
                    s = element.Current.Name;
                    currObjInfo = objInfo.GetObjectType(element);
                    if (s == null)
                    {
                        LogMessage("Current object Name is null");
                    }
                    else
                    {
                        s = s.ToString();
                        if (debug || writeToFile != null)
                            LogMessage("Obj name: " + s + " : " +
                                element.Current.ControlType.ProgrammaticName);
                    }
                    actualString = null;
                    if (s != null)
                        s = Regex.Replace(s, @"( |\t|:|\.|_|\r|\n|<|>)", "");
                    if (String.IsNullOrEmpty(s))
                    {
                        // txt0, txt1
                        actualString = currObjInfo.objType + currObjInfo.objCount;
                    }
                    else
                    {
                        // txtName, txtPassword
                        actualString = currObjInfo.objType + s;
                        index = 1;
                        while (true)
                        {
                            if (objectList.IndexOf(actualString) < 0)
                            {
                                // Object doesn't exist, assume this is the first
                                // element with the name and type
                                break;
                            }
                            actualString = currObjInfo.objType + s + index;
                            index++;
                        }
                    }
                    // Convert utf16 to utf8
                    // VMW bug#1054336
                    utf8Bytes = Encoding.UTF8.GetBytes(actualString);
                    utf8 = Encoding.UTF8.GetString(utf8Bytes);
                    if (type == null || type == element.Current.ControlType)
                    {
                        // Add if ControlType is null
                        // or only matching type, if available
                        objectList.Add(utf8);
                    }
                    if (objName != null || needAll)
                    {
                        //needAll - Required for GetChild
                        propertyHT = new Hashtable();
                        propertyHT.Add("key", utf8);
                        try
                        {
                            LogMessage(element.Current.LocalizedControlType);
                            string className = element.Current.LocalizedControlType;
                            if (element.Current.ControlType == ControlType.Button)
                                // For LDTP compatibility
                                className = "push_button";
                            propertyHT.Add("class",
                                Regex.Replace(className, " ", "_"));
                        }
                        catch (Exception ex)
                        {
                            LogMessage(ex);
                            propertyHT.Add("class",
                                element.Current.ControlType.ProgrammaticName);
                        }
                        propertyHT.Add("parent", parentName);
                        //propertyHT.Add("child_index", element.Current);
                        ArrayList childrenList = new ArrayList();
                        propertyHT.Add("children", childrenList);
                        // Check if parent exists
                        if (parentName != null && parentName.Length > 0 &&
                            objectHT[parentName] != null)
                        {
                            LogMessage("parentName NOT NULL");
                            // Add current child to the parent
                            ((ArrayList)((Hashtable)objectHT[parentName])["children"]).Add(utf8);
                        }
                        else
                            LogMessage("parentName NULL");
                        propertyHT.Add("obj_index",
                            currObjInfo.objType + "#" + currObjInfo.objCount);
                        if (s != null)
                        {
                            // Convert utf16 to utf8
                            // VMW bug#1054336
                            byte[] labelUtf8Bytes = Encoding.UTF8.GetBytes(element.Current.Name);
                            string label = Encoding.UTF8.GetString(labelUtf8Bytes);
                            propertyHT.Add("label", label);
                        }
                        // Following 2 properties exist in Linux
                        //propertyHT.Add("label_by", s == null ? "" : s);
                        //propertyHT.Add("description", element.Current.DescribedBy);
                        if (element.Current.AcceleratorKey != "")
                            propertyHT.Add("key_binding",
                                element.Current.AcceleratorKey);
                        if (!String.IsNullOrEmpty(element.Current.AutomationId))
                            propertyHT.Add("window_id",
                                element.Current.AutomationId);
                        // Add individual property to object property
                        objectHT.Add(Encoding.UTF8.GetString(utf8Bytes), propertyHT);
                        if (isObjIndex)
                            objIndex = currObjInfo.objType + "#" + currObjInfo.objCount;
                        if ((debug || writeToFile != null) && rx != null)
                        {
                            LogMessage(objName + " : " + utf8 + " : " + s
                                + " : " + tmp);
                            LogMessage((s != null && rx.Match(s).Success) + " : " +
                                (utf8 != null && rx.Match(utf8).Success));
                        }
                        if ((isAutomationId && !String.IsNullOrEmpty(automationId) &&
                            automationId == element.Current.AutomationId) ||
                            (isObjIndex && !String.IsNullOrEmpty(objIndex) && rx.Match(objIndex).Success) ||
                            (!isAutomationId && !isObjIndex &&
                            (s != null && rx != null && rx.Match(s).Success) ||
                            (utf8 != null && rx != null && rx.Match(utf8).Success)))
                        {
                            matchedKey = utf8;
                            LogMessage("String matched: " + needAll);
                            if (!needAll)
                                return true;
                        }
                    }

                    // If any subchild exist for the current element navigate to it
                    if (InternalGetObjectList(w.walker.GetFirstChild(element),
                        ref objectList, ref objectHT, ref matchedKey,
                        needAll, objName, utf8, type, objInfo))
                        return true;
                    element = w.walker.GetNextSibling(element);
                }
            }
            catch (ElementNotAvailableException ex)
            {
                LogMessage("Exception: " + ex);
            }
            catch (Exception ex)
            {
                LogMessage("Exception: " + ex);
            }
            finally
            {
                w = null;
                rx = null;
                propertyHT = null;
            }
            return false;
        }
Exemplo n.º 36
0
        internal bool InternalGetObjectList(AutomationElement windowHandle,
            ref ArrayList objectList, ref Hashtable objectHT,
            ref string matchedKey, bool needAll = false,
            string objName = null, string parentName = null,
            ControlType type = null)
        {
            if (windowHandle == null)
            {
                LogMessage("Invalid window handle");
                return false;
            }
            int index;
            Regex rx = null;
            String s = null;
            ObjInfo objInfo = new ObjInfo(false);
            String actualString = null;
            CurrentObjInfo currObjInfo;
            Hashtable propertyHT;
            // Trying to mimic python fnmatch.translate
            String tmp = null;

            InternalTreeWalker w = new InternalTreeWalker();
            if (objName != null)
            {
                tmp = Regex.Replace(objName, @"\*", @".*");
                tmp = Regex.Replace(tmp, @"( |:|\.|_|\r|\n|<|>)", "");
                tmp = Regex.Replace(tmp, @"\(", @"\(");
                tmp = Regex.Replace(tmp, @"\)", @"\)");
                //tmp += @"\Z(?ms)";
                rx = new Regex(tmp, RegexOptions.Compiled |
                    RegexOptions.IgnorePatternWhitespace | RegexOptions.Multiline |
                    RegexOptions.CultureInvariant);
            }
            try
            {
                AutomationElement element = windowHandle;
                while (null != element)
                {
                    s = element.Current.Name;
                    currObjInfo = objInfo.GetObjectType(element);
                    if (s == null)
                    {
                        LogMessage("Current object Name is null");
                    }
                    else
                    {
                        s = s.ToString();
                        if (debug)
                            LogMessage("Obj name: " + s + " : " +
                                element.Current.ControlType.ProgrammaticName);
                    }
                    actualString = null;
                    if (s != null)
                        s = Regex.Replace(s, @"( |\t|:|\.|_|\r|\n|<|>)", "");
                    if (s == null || s.Length == 0)
                    {
                        // txt0, txt1
                        actualString = currObjInfo.objType + currObjInfo.objCount;
                    }
                    else
                    {
                        // txtName, txtPassword
                        actualString = currObjInfo.objType + s;
                        index = 1;
                        while (true)
                        {
                            if (objectList.IndexOf(actualString) < 0)
                            {
                                // Object doesn't exist, assume this is the first
                                // element with the name and type
                                break;
                            }
                            actualString = currObjInfo.objType + s + index;
                            index++;
                        }
                    }
                    if (type == null || type == element.Current.ControlType)
                        // Add if ControlType is null
                        // or only matching type, if available
                        objectList.Add(actualString);
                    if (objName != null || needAll)
                    {
                        //needAll - Required for GetChild
                        propertyHT = new Hashtable();
                        propertyHT.Add("key", actualString);
                        try
                        {
                            LogMessage(element.Current.LocalizedControlType);
                            string className = element.Current.LocalizedControlType;
                            if (element.Current.ControlType == ControlType.Button)
                                // For LDTP compatibility
                                className = "push_button";
                            propertyHT.Add("class",
                                Regex.Replace(className, " ", "_"));
                        }
                        catch (Exception ex)
                        {
                            LogMessage(ex);
                            propertyHT.Add("class",
                                element.Current.ControlType.ProgrammaticName);
                        }
                        propertyHT.Add("parent", parentName);
                        //propertyHT.Add("child_index", element.Current);
                        ArrayList childrenList = new ArrayList();
                        propertyHT.Add("children", childrenList);
                        // Check if parent exists
                        if (parentName != null && parentName.Length > 0 &&
                            objectHT[parentName] != null)
                        {
                            LogMessage("parentName NOT NULL");
                            // Add current child to the parent
                            ((ArrayList)((Hashtable)objectHT[parentName])["children"]).Add(actualString);
                        }
                        else
                            LogMessage("parentName NULL");
                        propertyHT.Add("obj_index",
                            currObjInfo.objType + "#" + currObjInfo.objCount);
                        if (s != null)
                            propertyHT.Add("label", element.Current.Name);
                        // Following 2 properties exist in Linux
                        //propertyHT.Add("label_by", s == null ? "" : s);
                        //propertyHT.Add("description", element.Current.DescribedBy);
                        if (element.Current.AcceleratorKey != "")
                            propertyHT.Add("key_binding",
                                element.Current.AcceleratorKey);
                        // Add individual property to object property
                        objectHT.Add(actualString, propertyHT);
                        if (debug && rx != null)
                        {
                            LogMessage(objName + " : " + actualString + " : " + s
                                + " : " + tmp);
                            LogMessage((s != null && rx.Match(s).Success) + " : " +
                                (actualString != null && rx.Match(actualString).Success));
                        }
                        if ((s != null && rx != null && rx.Match(s).Success) ||
                        (actualString != null && rx != null && rx.Match(actualString).Success))
                        {
                            matchedKey = actualString;
                            LogMessage("String matched: " + needAll);
                            if (!needAll)
                                return true;
                        }
                    }

                    // If any subchild exist for the current element navigate to it
                    if (InternalGetObjectList(w.walker.GetFirstChild(element),
                        ref objectList, ref objectHT, ref matchedKey,
                        needAll, objName, actualString))
                        return true;
                    element = w.walker.GetNextSibling(element);
                }
            }
            catch (ElementNotAvailableException ex)
            {
                LogMessage("Exception: " + ex);
            }
            catch (Exception ex)
            {
                LogMessage("Exception: " + ex);
            }
            finally
            {
                w = null;
                rx = null;
                propertyHT = null;
            }
            return false;
        }
Exemplo n.º 37
0
Arquivo: Utils.cs Projeto: harig/cobra
 internal AutomationElement InternalWaitTillControlTypeExist(ControlType type,
     int processId, int guiTimeOut = 30)
 {
     InternalTreeWalker w;
     AutomationElementCollection c;
     Condition condition = new PropertyCondition(
         AutomationElement.ControlTypeProperty,
         ControlType.Menu);
     w = new InternalTreeWalker();
     try
     {
         int waitTime = 0;
         while (waitTime < guiTimeOut)
         {
             AutomationElement element = w.walker.GetFirstChild(AutomationElement.RootElement);
             while (null != element)
             {
                 if (windowList.IndexOf(element) == -1)
                     // Add parent window handle,
                     // if it doesn't exist
                     windowList.Add(element);
                 try
                 {
                     if (element.Current.ProcessId != processId)
                     {
                         // app name doesn't match
                         element = w.walker.GetNextSibling(element);
                         continue;
                     }
                     if (element.Current.ControlType == type)
                         return element;
                 }
                 catch
                 {
                     // Something went wrong, since app name
                     // is provided, search for next app
                     element = w.walker.GetNextSibling(element);
                     continue;
                 }
                 c = element.FindAll(TreeScope.Subtree, condition);
                 foreach (AutomationElement e in c)
                 {
                     try
                     {
                         if (e.Current.ControlType == type)
                             return e;
                     }
                     catch
                     {
                         // Something went wrong, since app name
                         // is provided, search for next app
                         element = w.walker.GetNextSibling(element);
                         continue;
                     }
                 }
                 element = w.walker.GetNextSibling(element);
                 waitTime++;
                 //InternalWait(1);
             }
         }
     }
     catch (Exception ex)
     {
         LogMessage(ex);
     }
     return null;
 }
Exemplo n.º 38
0
 private AutomationElement InternalGetWindowHandle(String windowName,
     ControlType[] type = null)
 {
     String s;
     int index;
     String actualString;
     AutomationElement element;
     CurrentObjInfo currObjInfo;
     ObjInfo objInfo = new ObjInfo(false);
     ArrayList objectList = new ArrayList();
     // Trying to mimic python fnmatch.translate
     String tmp = Regex.Replace(windowName, @"\*", @".*");
     tmp = Regex.Replace(tmp, @"\\", @"\\");
     tmp = Regex.Replace(tmp, "( |\r|\n)", "");
     tmp = @"\A(?ms)" + tmp + @"\Z(?ms)";
     //tmp += @"\Z(?ms)";
     Regex rx = new Regex(tmp, RegexOptions.Compiled |
         RegexOptions.IgnorePatternWhitespace | RegexOptions.Multiline |
         RegexOptions.CultureInvariant);
     List<AutomationElement> windowTmpList = new List<AutomationElement>();
     InternalTreeWalker w = new InternalTreeWalker();
     try
     {
         foreach (AutomationElement e in windowList)
         {
             try
             {
                 currObjInfo = objInfo.GetObjectType(e);
                 s = e.Current.Name;
                 if (s != null)
                     s = Regex.Replace(s, "( |\r|\n)", "");
                 if (s == null || s.Length == 0)
                 {
                     // txt0, txt1
                     actualString = currObjInfo.objType +
                         currObjInfo.objCount;
                 }
                 else
                 {
                     // txtName, txtPassword
                     actualString = currObjInfo.objType + s;
                     index = 1;
                     while (true)
                     {
                         if (objectList.IndexOf(actualString) < 0)
                         {
                             // Object doesn't exist, assume this is the first
                             // element with the name and type
                             break;
                         }
                         actualString = currObjInfo.objType + s + index;
                         index++;
                     }
                 }
                 LogMessage("Window: " + actualString + " : " + tmp);
                 objectList.Add(actualString);
                 // FIXME: Handle dlg0 as in Linux
                 if ((s != null && rx.Match(s).Success) ||
                     rx.Match(actualString).Success)
                 {
                     if (type == null)
                     {
                         LogMessage(windowName + " - Window found");
                         w = null;
                         rx = null;
                         objectList = null;
                         windowTmpList = null;
                         return e;
                     }
                     else
                     {
                         foreach (ControlType t in type)
                         {
                             if (debug)
                                 LogMessage((t == e.Current.ControlType) +
                                     " : " + e.Current.ControlType.ProgrammaticName);
                             if (t == e.Current.ControlType)
                             {
                                 w = null;
                                 rx = null;
                                 objectList = null;
                                 windowTmpList = null;
                                 return e;
                             }
                         }
                         LogMessage("type doesn't match !!!!!!!!!!!!!!");
                     }
                 }
             }
             catch (ElementNotAvailableException ex)
             {
                 // Don't alter the current list, remove it later
                 windowTmpList.Add(e);
                 LogMessage(ex);
             }
             catch (Exception ex)
             {
                 // Don't alter the current list, remove it later
                 windowTmpList.Add(e);
                 LogMessage(ex);
             }
         }
     }
     catch (Exception ex)
     {
         LogMessage(ex);
     }
     try
     {
         foreach (AutomationElement e in windowTmpList)
             try
             {
                 // Remove element from the list
                 windowList.Remove(e);
             }
             catch (Exception ex)
             {
                 LogMessage(ex);
             }
     }
     catch (Exception ex)
     {
         LogMessage(ex);
     }
     windowTmpList = null;
     objectList.Clear();
     objInfo = new ObjInfo(false);
     element = w.walker.GetFirstChild(AutomationElement.RootElement);
     try
     {
         while (null != element)
         {
             currObjInfo = objInfo.GetObjectType(element);
             s = element.Current.Name;
             if (s != null)
                 s = Regex.Replace(s, "( |\r|\n)", "");
             if (s == null || s == "")
             {
                 // txt0, txt1
                 actualString = currObjInfo.objType +
                     currObjInfo.objCount;
             }
             else
             {
                 // txtName, txtPassword
                 actualString = currObjInfo.objType + s;
                 index = 1;
                 while (true)
                 {
                     if (objectList.IndexOf(actualString) < 0)
                     {
                         // Object doesn't exist, assume this is the first
                         // element with the name and type
                         break;
                     }
                     actualString = currObjInfo.objType + s + index;
                     index++;
                 }
             }
             LogMessage("Window: " + actualString + " : " + tmp);
             objectList.Add(actualString);
             // FIXME: Handle dlg0 as in Linux
             if ((s != null && rx.Match(s).Success) ||
                 rx.Match(actualString).Success)
             {
                 if (type == null)
                 {
                     LogMessage(windowName + " - Window found");
                     return element;
                 }
                 else
                 {
                     foreach (ControlType t in type)
                     {
                         if (debug)
                             LogMessage((t == element.Current.ControlType) +
                                 " : " + element.Current.ControlType.ProgrammaticName);
                         if (t == element.Current.ControlType)
                         {
                             return element;
                         }
                     }
                     LogMessage("type doesn't match !!!!!!!!!!!!!!");
                 }
             }
             element = w.walker.GetNextSibling(element);
         }
         element = w.walker.GetFirstChild(
             AutomationElement.RootElement);
         // Reset object info
         objInfo = new ObjInfo(false);
         objectList.Clear();
         AutomationElement subChild;
         while (null != element)
         {
             subChild = w.walker.GetFirstChild(
                 element);
             while (subChild != null)
             {
                 if (subChild.Current.Name != null)
                 {
                     currObjInfo = objInfo.GetObjectType(subChild);
                     s = subChild.Current.Name;
                     if (s != null)
                         s = Regex.Replace(s, "( |\r|\n)", "");
                     if (s == null || s == "")
                     {
                         // txt0, txt1
                         actualString = currObjInfo.objType +
                             currObjInfo.objCount;
                     }
                     else
                     {
                         // txtName, txtPassword
                         actualString = currObjInfo.objType + s;
                         index = 1;
                         while (true)
                         {
                             if (objectList.IndexOf(actualString) < 0)
                             {
                                 // Object doesn't exist, assume this is the first
                                 // element with the name and type
                                 break;
                             }
                             actualString = currObjInfo.objType + s + index;
                             index++;
                         }
                     }
                     LogMessage("SubWindow: " + actualString + " : " + tmp);
                     if ((s != null && rx.Match(s).Success) ||
                         rx.Match(actualString).Success)
                     {
                         if (type == null)
                         {
                             LogMessage(windowName + " - Window found");
                             return subChild;
                         }
                         else
                         {
                             foreach (ControlType t in type)
                             {
                                 if (debug)
                                     LogMessage((t == subChild.Current.ControlType) +
                                         " : " + subChild.Current.ControlType.ProgrammaticName);
                                 if (t == subChild.Current.ControlType)
                                 {
                                     LogMessage(windowName + " - Window found");
                                     return subChild;
                                 }
                             }
                             LogMessage("type doesn't match !!!!!!!!!!!!!!");
                         }
                     }
                 }
                 subChild = w.walker.GetNextSibling(subChild);
             }
             element = w.walker.GetNextSibling(element);
         }
     }
     catch (Exception ex)
     {
         LogMessage(ex);
     }
     finally
     {
         w = null;
         rx = null;
         objectList = null;
     }
     // Unable to find window
     return null;
 }
Exemplo n.º 39
0
        public String[] GetWindowList()
        {
            int index;
            String s, actualString;
            ArrayList windowArrayList = new ArrayList();
            CurrentObjInfo currObjInfo;
            ObjInfo objInfo = new ObjInfo(false);
            AutomationElement element;
            InternalTreeWalker w = new InternalTreeWalker();
            element = w.walker.GetFirstChild(AutomationElement.RootElement);
            Condition condition = new PropertyCondition(
                AutomationElement.ControlTypeProperty,
                ControlType.Window);
            try
            {
                AutomationElementCollection c;
                // FIXME: Check whether resetting the ObjInfo is appropriate here
                objInfo = new ObjInfo(false);
                while (null != element)
                {
                    index = -1;

                    // In case of an exception, the program adds the element as
                    // new element to the windowList. Now I see my missing Qt window.
                    try
                    {
                        index = utils.windowList.IndexOf(element);
                    }
                    catch (Exception ex)
                    {
                        LogMessage(ex);
                    }

                    if (index == -1)
                        utils.windowList.Add(element);

                    s = element.Current.Name;
                    if (!String.IsNullOrEmpty(s))
                    {
                        LogMessage("Window name: " + s);
                    }
                    currObjInfo = objInfo.GetObjectType(element);
                    if (String.IsNullOrEmpty(s))
                        actualString = currObjInfo.objType + currObjInfo.objCount;
                    else
                        actualString = currObjInfo.objType + s;
                    index = 1;
                    while (true)
                    {
                        if (windowArrayList.IndexOf(actualString) < 0)
                            break;
                        if (String.IsNullOrEmpty(s)) // handling of an empty string like above
                            actualString = currObjInfo.objType + currObjInfo.objCount + index;
                        else
                            actualString = currObjInfo.objType + s + index;
                        index++;
                    }
                    windowArrayList.Add(actualString);
                    try
                    {
                        c = element.FindAll(TreeScope.Subtree, condition);
                        foreach (AutomationElement e in c)
                        {
                            index = -1;

                            // In case of an exception, the program adds the element as
                            // new element to the windowList. Now I see my missing Qt window.
                            try
                            {
                                index = utils.windowList.IndexOf(element);
                            }
                            catch (Exception ex)
                            {
                                LogMessage(ex);
                            }

                            if (index == -1)
                                utils.windowList.Add(element);

                            s = e.Current.Name;
                            currObjInfo = objInfo.GetObjectType(e);
                            if (String.IsNullOrEmpty(s))
                                actualString = currObjInfo.objType + currObjInfo.objCount;
                            else
                            {
                                actualString = currObjInfo.objType + s;
                                index = 1;
                                while (true)
                                {
                                    if (windowArrayList.IndexOf(actualString) < 0)
                                        break;
                                    actualString = currObjInfo.objType + s + index;
                                    index++;
                                }
                            }
                            windowArrayList.Add(actualString);
                        }
                    }
                    catch (Exception ex)
                    {
                        LogMessage(ex);
                    }
                    element = w.walker.GetNextSibling(element);
                }
                return windowArrayList.ToArray(typeof(string)) as string[];
            }
            catch (Exception ex)
            {
                LogMessage(ex);
            }

            try
            {
                if (windowArrayList.Count > 0)
                {
                   // Sometimes the command "getwindowlist" delivered an empty string, but 
                   // within the array "windowArrayList" were data. Now the included window names are returned.
                   return windowArrayList.ToArray(typeof(string)) as string[];
                }
            }
            catch (Exception ex)
            {
                LogMessage(ex);
            }

            finally
            {
                w = null;
                windowArrayList = null;
                condition = null;
            }
            // Unable to find window
            return null;
        }