public int SetTextValue(String windowName, String objName, String value) { AutomationElement childHandle = GetObjectHandle(windowName, objName); if (!utils.IsEnabled(childHandle)) { childHandle = null; throw new XmlRpcFaultException(123, "Object state is disabled"); } object valuePattern = null; try { // Reference: http://msdn.microsoft.com/en-us/library/ms750582.aspx if (!childHandle.TryGetCurrentPattern(ValuePattern.Pattern, out valuePattern)) { childHandle.SetFocus(); SendKeys.SendWait(value); } else { ((ValuePattern)valuePattern).SetValue(value); } } catch (Exception ex) { LogMessage(ex); if (ex is XmlRpcFaultException) { throw; } else { throw new XmlRpcFaultException(123, "Unhandled exception: " + ex.Message); } } finally { childHandle = null; valuePattern = null; } return(1); }
public int SetValue(String windowName, String objName, double value) { AutomationElement childHandle = GetObjectHandle(windowName, objName); object pattern = null; try { if (!utils.IsEnabled(childHandle)) { throw new XmlRpcFaultException(123, "Object state is disabled"); } childHandle.SetFocus(); if (childHandle.TryGetCurrentPattern(RangeValuePattern.Pattern, out pattern)) { if (((RangeValuePattern)pattern).Current.IsReadOnly) { throw new XmlRpcFaultException(123, "Control is read-only."); } ((RangeValuePattern)pattern).SetValue(value); return(1); } else if (childHandle.TryGetCurrentPattern(LegacyIAccessiblePattern.Pattern, out pattern)) { ((LegacyIAccessiblePattern)pattern).SetValue(Convert.ToString(value, CultureInfo.InvariantCulture)); return(1); } } catch (Exception ex) { LogMessage(ex); if (ex is XmlRpcFaultException) { throw; } else { throw new XmlRpcFaultException(123, "Unhandled exception: " + ex.Message); } } finally { pattern = null; childHandle = null; } throw new XmlRpcFaultException(123, "Unable to set value"); }
public int OneDown(String windowName, String objName, int iterations) { AutomationElement childHandle = GetObjectHandle(windowName, objName); if (!utils.IsEnabled(childHandle)) { childHandle = null; throw new XmlRpcFaultException(123, "Object state is disabled"); } object pattern = null; try { if (childHandle.TryGetCurrentPattern(RangeValuePattern.Pattern, out pattern)) { // Since we could not identify whether the object is vertical // or horizontal, let us do the same for down and right for (int i = 0; i < iterations; i++) { double value = ((RangeValuePattern)pattern).Current.Value; // Value < 3 doesn't work, tried 1, 2 if ((value + 3) < ((RangeValuePattern)pattern).Current.Maximum) { ((RangeValuePattern)pattern).SetValue(value + 3); } } return(1); } } catch (Exception ex) { LogMessage(ex); if (ex is XmlRpcFaultException) { throw; } else { throw new XmlRpcFaultException(123, "Unhandled exception: " + ex.Message); } } finally { pattern = null; } throw new XmlRpcFaultException(123, "Unable to one down"); }
public int DoesRowExist(String windowName, String objName, String text, bool partialMatch = false) { if (String.IsNullOrEmpty(text)) { LogMessage("Argument cannot be empty."); return(0); } ControlType[] type; AutomationElement childHandle; AutomationElement elementItem; try { childHandle = GetObjectHandle(windowName, objName, null, false); if (!utils.IsEnabled(childHandle)) { childHandle = null; LogMessage("Object state is disabled"); return(0); } childHandle.SetFocus(); type = new ControlType[2] { ControlType.TreeItem, ControlType.ListItem }; if (partialMatch) { text += "*"; } elementItem = utils.GetObjectHandle(childHandle, text, type, false); if (elementItem != null) { return(1); } } catch (Exception ex) { LogMessage(ex); } finally { type = null; childHandle = elementItem = null; } return(0); }
public int StateEnabled(String windowName, String objName) { AutomationElement childHandle; try { childHandle = GetObjectHandle(windowName, objName, null); if (utils.IsEnabled(childHandle)) { return(1); } } catch (Exception ex) { LogMessage(ex); } finally { childHandle = null; } return(0); }
public int SetTextValue(String windowName, String objName, String value) { AutomationElement childHandle = GetObjectHandle(windowName, objName); if (!utils.IsEnabled(childHandle)) { childHandle = null; throw new XmlRpcFaultException(123, "Object state is disabled"); } object valuePattern = null; try { if (childHandle.Current.ControlType == ControlType.ComboBox) { AutomationElement o = null; ArrayList objectList = new ArrayList(); ControlType[] type = new ControlType[1] { ControlType.Edit }; // NOTE: Using "*" for object name, which returns the first // matching Edit control type o = utils.InternalGetObjectHandle(childHandle, "*", type, ref objectList); if (o != null) { childHandle = o; } objectList = null; } // Reference: http://msdn.microsoft.com/en-us/library/ms750582.aspx if (!childHandle.TryGetCurrentPattern(ValuePattern.Pattern, out valuePattern)) { childHandle.SetFocus(); SendKeys.SendWait(value); } else { ((ValuePattern)valuePattern).SetValue(value); } } catch (Exception ex) { LogMessage(ex); if (ex is XmlRpcFaultException) { throw; } else { throw new XmlRpcFaultException(123, "Unhandled exception: " + ex.Message); } } finally { childHandle = null; valuePattern = null; } return(1); }
private int InternalComboHandler(String windowName, String objName, String item, ref String selectedItem, String actionType = "Select", ArrayList childList = null) { bool verify = actionType == "Verify" ? true : false; ControlType[] comboTtype = new ControlType[3] { ControlType.ComboBox, ControlType.ListItem, ControlType.List/*, ControlType.Text */ }; AutomationElement childHandle = utils.GetObjectHandle(windowName, objName, comboTtype, !verify); Object pattern = null; Object invokePattern = null; AutomationElement elementItem = null; ControlType[] type = new ControlType[1] { ControlType.Button }; try { LogMessage("Handle name: " + childHandle.Current.Name + " - " + childHandle.Current.ControlType.ProgrammaticName); if (!utils.IsEnabled(childHandle, !verify)) { throw new XmlRpcFaultException(123, "Object state is disabled"); } elementItem = null; /* * elementItem = utils.GetObjectHandle(childHandle, "Open", type, true); * if (elementItem != null) * { * LogMessage("elementItem: " + elementItem.Current.Name + * " - " + elementItem.Current.ControlType.ProgrammaticName); * } */ if (childHandle.TryGetCurrentPattern(ExpandCollapsePattern.Pattern, out pattern) || childHandle.TryGetCurrentPattern( InvokePattern.Pattern, out invokePattern) || (elementItem != null && elementItem.TryGetCurrentPattern( InvokePattern.Pattern, out invokePattern))) { LogMessage("ExpandCollapsePattern"); // Retry max 5 times for (int i = 0; i < 5; i++) { switch (actionType) { case "Hide": if (invokePattern != null) { ((InvokePattern)invokePattern).Invoke(); return(1); } else if (pattern != null) { ((ExpandCollapsePattern)pattern).Collapse(); // Required to wait 1 second, // before checking the state and retry collapsing utils.InternalWait(1); if (((ExpandCollapsePattern)pattern).Current.ExpandCollapseState == ExpandCollapseState.Collapsed) { // Hiding same combobox multiple time consecutively // fails. Check for the state and retry to collapse LogMessage("Collapsed"); return(1); } } break; case "Show": case "Select": case "Verify": // elementItem = utils.GetObjectHandle(childHandle, "Open", // type, !verify); elementItem = null; if (invokePattern != null || (elementItem != null && elementItem.TryGetCurrentPattern(InvokePattern.Pattern, out invokePattern))) { ((InvokePattern)invokePattern).Invoke(); } else if (pattern != null) { ((ExpandCollapsePattern)pattern).Expand(); } // Required to wait 1 second, // before checking the state and retry expanding utils.InternalWait(1); if (invokePattern != null || (pattern != null && ((ExpandCollapsePattern)pattern).Current.ExpandCollapseState == ExpandCollapseState.Expanded)) { // Selecting same combobox multiple time consecutively // fails. Check for the state and retry to expand LogMessage("Expaneded"); if (actionType == "Show") { return(1); } else { return(SelectListItem(childHandle, item, verify) ? 1 : 0); } } break; case "GetComboValue": Object selectionPattern = null; LogMessage("GetComboValue"); // elementItem = utils.GetObjectHandle(childHandle, "Open", // type, true); elementItem = null; if (invokePattern != null || (elementItem != null && elementItem.TryGetCurrentPattern(InvokePattern.Pattern, out invokePattern))) { LogMessage("InvokePattern"); childHandle.SetFocus(); utils.InternalClick(elementItem); // InvokePattern doesn't work with Virtual Network // Editor of VMware Workstation, so used the above InternalClick //((InvokePattern)invokePattern).Invoke(); } else if (pattern != null) { LogMessage("ExpandCollapsePattern"); ((ExpandCollapsePattern)pattern).Expand(); } Object valuePattern; if (childHandle.TryGetCurrentPattern(ValuePattern.Pattern, out valuePattern)) { selectedItem = ((ValuePattern)valuePattern).Current.Value; return(1); } // Required to wait 1 second, // before checking the state and retry expanding utils.InternalWait(1); LogMessage("Handle name: " + childHandle.Current.Name + " - " + childHandle.Current.ControlType.ProgrammaticName); bool typeExist = utils.InternalWaitTillChildControlTypeExist(childHandle, type); LogMessage("Control type exist: " + typeExist); AutomationElementCollection c = childHandle.FindAll(TreeScope.Subtree, Condition.TrueCondition); LogMessage("AutomationElementCollection " + c.Count); foreach (AutomationElement e in c) { LogMessage(e.Current.Name + " : " + e.Current.ControlType.ProgrammaticName); bool status = false; if (e.TryGetCurrentPattern(SelectionItemPattern.Pattern, out selectionPattern)) { status = ((SelectionItemPattern)selectionPattern).Current.IsSelected; if (status) { LogMessage("Selected: " + e.Current.Name); selectedItem = e.Current.Name; ((ExpandCollapsePattern)pattern).Collapse(); return(1); } } } LogMessage("Unable to find selected combo box value"); c = null; selectionPattern = null; if (invokePattern != null) { ((InvokePattern)invokePattern).Invoke(); } else if (pattern != null) { ((ExpandCollapsePattern)pattern).Collapse(); } return(0); case "GetAllItem": string matchedKey = null; Hashtable objectHT = new Hashtable(); ArrayList tmpChildList = new ArrayList(); InternalTreeWalker w = new InternalTreeWalker(); elementItem = utils.GetObjectHandle(childHandle, "Open", type, true); // Changes based on QT 5.0.2 if (invokePattern != null || (elementItem != null && elementItem.TryGetCurrentPattern(InvokePattern.Pattern, out invokePattern))) { ((InvokePattern)invokePattern).Invoke(); } else if (pattern != null) { ((ExpandCollapsePattern)pattern).Expand(); } // Required to wait 1 second, // before checking the state and retry expanding utils.InternalWait(1); utils.InternalGetObjectList( w.walker.GetFirstChild(childHandle), ref tmpChildList, ref objectHT, ref matchedKey, true, null, null, ControlType.ListItem); if (invokePattern != null) { ((InvokePattern)invokePattern).Invoke(); } else if (pattern != null) { ((ExpandCollapsePattern)pattern).Collapse(); } // For Linux compatibility Hashtable propertyHT; foreach (String key in objectHT.Keys) { propertyHT = (Hashtable)objectHT[key]; string className = (string)propertyHT["class"]; if (className != null && className.Contains("list_item")) { // Add only list items childList.Add(propertyHT["label"]); } } w = null; tmpChildList = null; propertyHT = objectHT = null; if (childList.Count > 0) { // Don't process the last item return(1); } else { LogMessage("childList.Count <= 0: " + childList.Count); } return(0); } } } // Handle selectitem and verifyselect on list. // Get ExpandCollapsePattern fails on list, // VM Library items are selected and // verified correctly on Player with this fix else { LogMessage("SelectListItem"); childHandle.SetFocus(); return(SelectListItem(childHandle, item, verify) ? 1 : 0); } } catch (Exception ex) { LogMessage(ex); if (ex is XmlRpcFaultException) { throw; } else { throw new XmlRpcFaultException(123, "Unhandled exception: " + ex.Message); } } finally { comboTtype = type = null; pattern = invokePattern = null; elementItem = childHandle = null; } return(0); }
private int InternalComboHandler(String windowName, String objName, String item, String actionType = "Select", ArrayList childList = null) { AutomationElement childHandle = GetObjectHandle(windowName, objName); Object pattern = null; try { LogMessage("Handle name: " + childHandle.Current.Name + " - " + childHandle.Current.ControlType.ProgrammaticName); if (!utils.IsEnabled(childHandle)) { throw new XmlRpcFaultException(123, "Object state is disabled"); } if (childHandle.TryGetCurrentPattern(ExpandCollapsePattern.Pattern, out pattern)) { LogMessage("ExpandCollapsePattern"); // Retry max 5 times for (int i = 0; i < 5; i++) { switch (actionType) { case "Hide": ((ExpandCollapsePattern)pattern).Collapse(); // Required to wait 1 second, // before checking the state and retry collapsing utils.InternalWait(1); if (((ExpandCollapsePattern)pattern).Current.ExpandCollapseState == ExpandCollapseState.Collapsed) { // Hiding same combobox multiple time consecutively // fails. Check for the state and retry to collapse LogMessage("Collapsed"); return(1); } break; case "Show": case "Select": case "Verify": ((ExpandCollapsePattern)pattern).Expand(); // Required to wait 1 second, // before checking the state and retry expanding utils.InternalWait(1); if (((ExpandCollapsePattern)pattern).Current.ExpandCollapseState == ExpandCollapseState.Expanded) { // Selecting same combobox multiple time consecutively // fails. Check for the state and retry to expand LogMessage("Expaneded"); if (actionType == "Show") { return(1); } else { childHandle.SetFocus(); bool verify = actionType == "Verify" ? true : false; return(SelectListItem(childHandle, item, verify) ? 1 : 0); } } break; case "GetAllItem": string matchedKey = null; Hashtable objectHT = new Hashtable(); ArrayList tmpChildList = new ArrayList(); InternalTreeWalker w = new InternalTreeWalker(); utils.InternalGetObjectList( w.walker.GetFirstChild(childHandle), ref tmpChildList, ref objectHT, ref matchedKey, true, null, null, ControlType.ListItem); // For Linux compatibility Hashtable propertyHT; foreach (String key in objectHT.Keys) { propertyHT = (Hashtable)objectHT[key]; string className = (string)propertyHT["class"]; if (className != null && className.Contains("list_item")) { // Add only list items childList.Add(propertyHT["label"]); } } w = null; tmpChildList = null; propertyHT = objectHT = null; if (childList.Count > 0) { // Don't process the last item return(1); } else { LogMessage("childList.Count <= 0: " + childList.Count); } return(0); } } } // Handle selectitem and verifyselect on list. // Get ExpandCollapsePattern fails on list, // VM Library items are selected and // verified correctly on Player with this fix else { childHandle.SetFocus(); bool verify = actionType == "Verify" ? true : false; return(SelectListItem(childHandle, item, verify) ? 1 : 0); } } catch (Exception ex) { LogMessage(ex); if (ex is XmlRpcFaultException) { throw; } else { throw new XmlRpcFaultException(123, "Unhandled exception: " + ex.Message); } } finally { pattern = null; childHandle = null; } return(0); }
public int SelectTab(String windowName, String objName, String tabName) { if (String.IsNullOrEmpty(tabName)) { throw new XmlRpcFaultException(123, "Argument cannot be empty."); } AutomationElement elementItem; AutomationElement childHandle = GetObjectHandle(windowName, objName); if (!utils.IsEnabled(childHandle)) { childHandle = null; throw new XmlRpcFaultException(123, "Object state is disabled"); } Object pattern; try { childHandle.SetFocus(); elementItem = utils.GetObjectHandle(childHandle, tabName); if (elementItem != null) { LogMessage(elementItem.Current.Name + " : " + elementItem.Current.ControlType.ProgrammaticName); if (elementItem.TryGetCurrentPattern(SelectionItemPattern.Pattern, out pattern)) { LogMessage("SelectionItemPattern"); //((SelectionItemPattern)pattern).Select(); // NOTE: Work around, as the above doesn't seem to work // with UIAComWrapper and UIAComWrapper is required // to Edit value in Spin control utils.InternalClick(elementItem); return(1); } else if (elementItem.TryGetCurrentPattern(ExpandCollapsePattern.Pattern, out pattern)) { LogMessage("ExpandCollapsePattern"); ((ExpandCollapsePattern)pattern).Expand(); return(1); } else { throw new XmlRpcFaultException(123, "Unsupported pattern."); } } } catch (Exception ex) { LogMessage(ex); if (ex is XmlRpcFaultException) { throw; } else { throw new XmlRpcFaultException(123, "Unhandled exception: " + ex.Message); } } finally { pattern = null; childHandle = null; } throw new XmlRpcFaultException(123, "Unable to find the item in tab list: " + tabName); }
public int MouseLeftClick(String windowName, String objName) { Object pattern = null; AutomationElement childHandle; try { childHandle = utils.GetObjectHandle(windowName, objName); if (!utils.IsEnabled(childHandle)) { throw new XmlRpcFaultException(123, "Object state is disabled"); } try { childHandle.SetFocus(); } catch (Exception ex) { // Have noticed exception with // maximize / minimize button LogMessage(ex); } if (childHandle.Current.ControlType == ControlType.Pane) { // NOTE: Work around, as the pane doesn't seem to work // with any actions. Noticed this window, when Windows // Security Warning dialog pop's up utils.InternalClick(childHandle); return(1); } else if (childHandle.TryGetCurrentPattern(InvokePattern.Pattern, out pattern)) { if (childHandle.Current.ControlType == ControlType.Menu || childHandle.Current.ControlType == ControlType.MenuBar || childHandle.Current.ControlType == ControlType.MenuItem || childHandle.Current.ControlType == ControlType.ListItem) { //((InvokePattern)invokePattern).Invoke(); // NOTE: Work around, as the above doesn't seem to work // with UIAComWrapper and UIAComWrapper is required // to Edit value in Spin control utils.InternalClick(childHandle); } else { try { ((InvokePattern)pattern).Invoke(); } catch (Exception ex) { LogMessage(ex); // Have noticed exception with // maximize / minimize button utils.InternalClick(childHandle); } } return(1); } else if (childHandle.TryGetCurrentPattern(SelectionItemPattern.Pattern, out pattern)) { ((SelectionItemPattern)pattern).Select(); return(1); } else { utils.InternalClick(childHandle); return(1); } } catch (Exception ex) { LogMessage(ex); if (ex is XmlRpcFaultException) { throw; } else { throw new XmlRpcFaultException(123, "Unhandled exception: " + ex.Message); } } finally { pattern = null; childHandle = null; } throw new XmlRpcFaultException(123, "Unable to perform action"); }
private int InternalMenuHandler(String windowName, String objName, ref ArrayList menuList, String actionType = "Select") { if (String.IsNullOrEmpty(windowName) || String.IsNullOrEmpty(objName)) { throw new XmlRpcFaultException(123, "Argument cannot be empty."); } Object pattern = null; String currObjName = null; AutomationElementCollection c = null; ControlType[] type = new ControlType[3] { ControlType.Menu, ControlType.MenuBar, ControlType.MenuItem }; ControlType[] controlType = new ControlType[3] { ControlType.Menu, ControlType.MenuItem, ControlType.MenuBar }; AutomationElement tmpContextHandle = null; AutomationElement windowHandle, childHandle; AutomationElement prevObjHandle = null, firstObjHandle = null; InternalTreeWalker w = new InternalTreeWalker(); try { windowHandle = utils.GetWindowHandle(windowName); if (windowHandle == null) { throw new XmlRpcFaultException(123, "Unable to find window: " + windowName); } processId = windowHandle.Current.ProcessId; windowHandle.SetFocus(); LogMessage("Window name: " + windowHandle + " : " + windowHandle.Current.Name + " : " + windowHandle.Current.ControlType.ProgrammaticName); childHandle = windowHandle; /* * // element is an AutomationElement. * AutomationPattern[] patterns = childHandle.GetSupportedPatterns(); * foreach (AutomationPattern pattern1 in patterns) * { * Console.WriteLine("ProgrammaticName: " + pattern1.ProgrammaticName); * Console.WriteLine("PatternName: " + Automation.PatternName(pattern1)); * } * /**/ while (true) { if (objName.Contains(";")) { int index = objName.IndexOf(";", StringComparison.CurrentCulture); currObjName = objName.Substring(0, index); objName = objName.Substring(index + 1); } else { currObjName = objName; } LogMessage("childHandle: " + childHandle.Current.Name + " : " + currObjName + " : " + childHandle.Current.ControlType.ProgrammaticName); childHandle = utils.GetObjectHandle(childHandle, currObjName, type, false); if (childHandle == null) { if (currObjName == objName) { throw new XmlRpcFaultException(123, "Unable to find Object: " + objName); } else { throw new XmlRpcFaultException(123, "Unable to find Object: " + currObjName); } } // Store previous handle for later use prevObjHandle = childHandle; if (firstObjHandle == null) { // Save it for later use firstObjHandle = childHandle; } if ((actionType == "Select" || actionType == "SubMenu" || actionType == "Check" || actionType == "UnCheck" || actionType == "VerifyCheck" || actionType == "Window") && !utils.IsEnabled(childHandle, false)) { throw new XmlRpcFaultException(123, "Object state is disabled"); } try { if (actionType == "Window") { utils.InternalXYClick(childHandle); } else { // SetFocus() fails on Windows Explorer childHandle.SetFocus(); } } catch (Exception ex) { LogMessage(ex); } if (childHandle.TryGetCurrentPattern(InvokePattern.Pattern, out pattern) || childHandle.TryGetCurrentPattern( ExpandCollapsePattern.Pattern, out pattern)) { if (actionType == "Select" || currObjName != objName || actionType == "SubMenu" || actionType == "VerifyCheck" || actionType == "Window") { try { LogMessage("Invoking menu item: " + currObjName + " : " + objName + " : " + childHandle.Current.ControlType.ProgrammaticName + " : " + childHandle.Current.Name); } catch (Exception ex) { // Noticed with closewindow() to close Notepad // System.UnauthorizedAccessException: Access is denied // Exception from HRESULT: 0x80070005 (E_ACCESSDENIED) LogMessage(ex); } if (actionType != "Window") { try { // SetFocus() fails on Windows Explorer childHandle.SetFocus(); } catch (Exception ex) { LogMessage(ex); } } if (!(actionType == "VerifyCheck" && currObjName == objName) && (actionType != "Window")) { utils.InternalClick(childHandle); } try { // Invoke doesn't work for VMware Workstation // But they work for Notepad // MoveToAndClick works for VMware Workstation // But not for Notepad (on first time) // Requires 2 clicks ! //((InvokePattern)pattern).Invoke(); utils.InternalWait(1); c = childHandle.FindAll(TreeScope.Children, Condition.TrueCondition); } catch (System.NotImplementedException ex) { // Noticed with VMware Workstation // System.Runtime.InteropServices.COMException (0x80040200): // Exception from HRESULT: 0x80040200 LogMessage("NotImplementedException"); LogMessage(ex); } catch (System.Windows.Automation.ElementNotEnabledException ex) { // Noticed with VMware Workstation // System.Runtime.InteropServices.COMException (0x80040200): // Exception from HRESULT: 0x80040200 LogMessage("Element not enabled"); LogMessage(ex); } catch (Exception ex) { LogMessage(ex); } } } if (currObjName == objName && actionType != "SubMenu") { int state; switch (actionType) { case "Select": case "Window": // No child menu item to be processed return(1); case "Check": case "UnCheck": state = IsMenuChecked(childHandle); LogMessage("IsMenuChecked(childHandle): " + childHandle.Current.ControlType.ProgrammaticName); LogMessage("actionType: " + actionType); // Don't process the last item if (actionType == "Check") { if (state == 1) { // Already checked, just click back the main menu utils.InternalClick(firstObjHandle); } else { // Check menu utils.InternalClick(childHandle); } return(1); } else if (actionType == "UnCheck") { if (state == 0) { // Already unchecked, just click back the main menu utils.InternalClick(firstObjHandle); } else { // Uncheck menu utils.InternalClick(childHandle); } return(1); } break; case "Exist": case "Enabled": state = utils.IsEnabled(childHandle) == true ? 1 : 0; LogMessage("IsEnabled(childHandle): " + childHandle.Current.Name + " : " + state); LogMessage("IsEnabled(childHandle): " + childHandle.Current.ControlType.ProgrammaticName); // Set it back to old state, else the menu selection left there utils.InternalClick(firstObjHandle); // Don't process the last item if (actionType == "Enabled") { return(state); } else if (actionType == "Exist") { return(1); } break; case "SubMenu": int status = HandleSubMenu(w.walker.GetFirstChild(childHandle), firstObjHandle, ref menuList); if (status == 1) { return(1); } break; case "VerifyCheck": state = IsMenuChecked(childHandle); utils.InternalClick(firstObjHandle); return(state); default: break; } } else if ((tmpContextHandle = utils.InternalWaitTillControlTypeExist( ControlType.Menu, processId, 3)) != null) { LogMessage("InternalWaitTillControlTypeExist"); // Find object from current handle, rather than navigating // the complete window childHandle = tmpContextHandle; if (actionType != "SubMenu") { continue; } else if (currObjName == objName) { switch (actionType) { case "SubMenu": int status = HandleSubMenu(w.walker.GetFirstChild(childHandle), firstObjHandle, ref menuList); if (status == 1) { return(1); } break; } } } else if (c != null && c.Count > 0) { if (currObjName == objName) { switch (actionType) { case "SubMenu": int status = HandleSubMenu(w.walker.GetFirstChild(childHandle), firstObjHandle, ref menuList); if (status == 1) { return(1); } break; } } LogMessage("c != null && c.Count > 0"); childHandle = windowHandle; continue; } // Required for Notepad like app if ((c == null || c.Count == 0)) { LogMessage("Work around for Windows application"); LogMessage(windowHandle.Current.Name + " : " + objName); AutomationElement tmpChildHandle = utils.GetObjectHandle( windowHandle, objName, type, false); // Work around for Notepad, as it doesn't find the menuitem // on clicking any menu if (tmpChildHandle != null) { LogMessage("Work around: tmpChildHandle != null"); if (actionType == "SubMenu" && currObjName == objName) { // Work around for Notepad like app childHandle = tmpChildHandle; } else { // Work around for Notepad like app, // but for actionType other than SubMenu childHandle = windowHandle; } } } if (currObjName == objName) { switch (actionType) { case "SubMenu": int status = HandleSubMenu(w.walker.GetFirstChild(childHandle), firstObjHandle, ref menuList); if (status == 1) { return(1); } break; } } } } catch (Exception ex) { LogMessage(ex); if (firstObjHandle != null && actionType != "Window") { // Set it back to old state, else the menu selection left there utils.InternalXYClick(firstObjHandle); } if (((ex is ElementNotAvailableException) || (ex is UnauthorizedAccessException)) && actionType == "Window") { // API closewindow() can close Windows Explorer on XP, but: // ----------------------------------------------------------- // if (childHandle.TryGetCurrentPattern(InvokePattern.Pattern, // out pattern) || childHandle.TryGetCurrentPattern( // ExpandCollapsePattern.Pattern, out pattern)) // ----------------------------------------------------------- // Sometimes above code will throw exception, sometimes not: // System.Runtime.InteropServices.COMException (0x80040201): // Exception from HRESULT: 0x80040201 // System.UnauthorizedAccessException, Access is denied: // Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)) // So use this if block as workaround return(1); } if (ex is XmlRpcFaultException) { throw; } else { throw new XmlRpcFaultException(123, "Unhandled exception: " + ex.Message); } } finally { c = null; w = null; pattern = null; windowHandle = childHandle = null; prevObjHandle = firstObjHandle = null; } }