Пример #1
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);
        }
Пример #2
0
        private bool DoesWindowNameMatched(AutomationElement e,
                                           string windowName)
        {
            if (e == null || String.IsNullOrEmpty(windowName))
            {
                return(false);
            }
            String         s1, s2;
            CurrentObjInfo currObjInfo;
            // Trying to mimic python fnmatch.translate
            String tmp = Regex.Replace(windowName, @"\*", @".*");

            tmp = Regex.Replace(tmp, " ", "");
            //tmp += @"\Z(?ms)";
            Regex rx = new Regex(tmp, RegexOptions.Compiled |
                                 RegexOptions.IgnorePatternWhitespace | RegexOptions.Multiline |
                                 RegexOptions.CultureInvariant);

            s1 = e.Current.Name;
            if (s1 != null)
            {
                s1 = (new Regex(" ")).Replace(s1, "");
            }
            if (String.IsNullOrEmpty(s1))
            {
                return(false);
            }
            ObjInfo objInfo = new ObjInfo(false);

            currObjInfo = objInfo.GetObjectType(e);
            // LDTP format object name
            s2 = currObjInfo.objType + s1;
            if (rx.Match(s1).Success || rx.Match(s2).Success)
            {
                return(true);
            }
            return(false);
        }
Пример #3
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);
        }
Пример #4
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);
        }
Пример #5
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);
        }
Пример #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;
        }
Пример #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;
 }
Пример #8
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;
 }
Пример #9
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();
     ObjInfo objInfo = new ObjInfo(false);
     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)
             {
                 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);
 }
Пример #10
0
 public String[] GetObjectList(String windowName)
 {
     if (String.IsNullOrEmpty(windowName))
     {
         throw new XmlRpcFaultException(123, "Argument cannot be empty.");
     }
     string matchedKey = null;
     ObjInfo objInfo = new ObjInfo(false);
     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);
     }
     windowHandle.SetFocus();
     InternalTreeWalker walker = new InternalTreeWalker();
     utils.InternalGetObjectList(walker.walker.GetFirstChild(windowHandle),
         ref objectList, ref objectHT, ref matchedKey,
         true, null, windowHandle.Current.Name);
     if (utils.debug)
     {
         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;
     }
 }
Пример #11
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();
     ObjInfo objInfo = new ObjInfo(false);
     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);
 }
Пример #12
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.");
     }
     Hashtable ht;
     string matchedKey = "";
     Hashtable objectHT = new Hashtable();
     ObjInfo objInfo = new ObjInfo(false);
     ArrayList childList = new ArrayList();
     ArrayList objectList = new ArrayList();
     InternalTreeWalker w;
     AutomationElement childHandle;
     AutomationElement windowHandle = utils.GetWindowHandle(windowName);
     if (windowHandle == null)
     {
         throw new XmlRpcFaultException(123,
             "Unable to find window: " + windowName);
     }
     w = new InternalTreeWalker();
     try
     {
         if (childName != null && childName.Length > 0 &&
             role != null && role.Length > 0)
         {
             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)
                         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)
                     {
                         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)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[];
         }
         if (role != null && role.Length > 0)
         {
             childHandle = utils.GetObjectHandle(windowHandle,
                 childName);
             if (childHandle == null)
             {
                 throw new XmlRpcFaultException(123,
                     "Unable to find child object: " + childName);
             }
             role = Regex.Replace(role, @" ", @"_");
             utils.InternalGetObjectList(w.walker.GetFirstChild(windowHandle),
                 ref objectList, ref objectHT, ref matchedKey,
                 true, null, windowHandle.Current.Name);
             foreach (string key in objectHT.Keys)
             {
                 try
                 {
                     if (utils.debug)
                         LogMessage("Key: " + key);
                     ht = (Hashtable)objectHT[key];
                     if ((string)ht["class"] == role)
                         childList.Add(ht["key"]);
                 }
                 catch (Exception ex)
                 {
                     LogMessage(ex);
                 }
             }
             return childList.ToArray(typeof(string)) as string[];
         }
         if (childName != null && childName.Length > 0)
         {
             childHandle = utils.GetObjectHandle(windowHandle,
                 childName);
             if (childHandle == null)
             {
                 throw new XmlRpcFaultException(123,
                     "Unable to find child object: " + childName);
             }
             utils.InternalGetObjectList(w.walker.GetFirstChild(childHandle),
                 ref objectList, ref objectHT, ref matchedKey,
                 true, null, windowHandle.Current.Name);
             foreach (string key in objectHT.Keys)
             {
                 try
                 {
                     if (utils.debug)
                         LogMessage("Key: " + key);
                     ht = (Hashtable)objectHT[key];
                     childList.Add(ht["key"]);
                 }
                 catch (Exception ex)
                 {
                     LogMessage(ex);
                 }
             }
             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");
 }
Пример #13
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);
        }
Пример #14
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");
        }
Пример #15
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;
 }
Пример #16
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");
 }
Пример #17
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;
 }
Пример #18
0
 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;
 }
Пример #19
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 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;
        }
Пример #20
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;
        }
Пример #21
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);
        }
Пример #22
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;
 }
Пример #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)
                {
                    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;
        }