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); }
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; }
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; }
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; }
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; }
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; }
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"); }
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; }
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); }
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; }
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); }
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"); }
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); }
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; }