Пример #1
0
 private AutomationElement GetObjectHandle(string windowName,
                                           string objName)
 {
     ControlType[] type = new ControlType[1] {
         ControlType.Tab
     };
     try
     {
         return(utils.GetObjectHandle(windowName, objName, type));
     }
     finally
     {
         type = null;
     }
 }
Пример #2
0
 private AutomationElement GetObjectHandle(string windowName,
                                           string objName)
 {
     ControlType[] type = new ControlType[3] {
         ControlType.ComboBox,
         ControlType.ListItem, ControlType.List/*, ControlType.Text */
     };
     try
     {
         return(utils.GetObjectHandle(windowName, objName, type));
     }
     finally
     {
         type = null;
     }
 }
Пример #3
0
 private AutomationElement GetObjectHandle(string windowName,
                                           string objName, ControlType[] type = null, bool waitForObj = true)
 {
     if (type == null)
     {
         type = new ControlType[2] {
             ControlType.Tree,
             ControlType.List
         }
     }
     ;
     try
     {
         return(utils.GetObjectHandle(windowName,
                                      objName, type, waitForObj));
     }
     finally
     {
         type = null;
     }
 }
Пример #4
0
 public int EnterString(string windowName, string objName = null,
                        string data = null)
 {
     if (!String.IsNullOrEmpty(objName))
     {
         AutomationElement windowHandle = utils.GetWindowHandle(windowName);
         if (windowHandle != null)
         {
             AutomationElement childHandle = utils.GetObjectHandle(
                 windowHandle, objName);
             windowHandle = null;
             try
             {
                 if (childHandle != null)
                 {
                     childHandle.SetFocus();
                 }
             }
             catch (Exception ex)
             {
                 LogMessage(ex);
                 if (ex is XmlRpcFaultException)
                 {
                     throw;
                 }
                 else
                 {
                     throw new XmlRpcFaultException(123,
                                                    "Unhandled exception: " + ex.Message);
                 }
             }
             finally
             {
                 childHandle = null;
             }
         }
     }
     else
     {
         // Hack as Linux LDTPv1/v2
         data = windowName;
     }
     if (data == null || data.Length == 0)
     {
         throw new XmlRpcFaultException(123, "Argument cannot be empty.");
     }
     return(GenerateKeyEvent(data));
 }
Пример #5
0
 private AutomationElement GetObjectHandle(string windowName,
                                           string objName)
 {
     // Pane added for a bug in QT
     // Ref: https://cobra.codeplex.com/discussions/450296
     ControlType[] type = new ControlType[4] {
         ControlType.Edit,
         ControlType.Document, ControlType.ComboBox, ControlType.Pane
     };
     try
     {
         return(utils.GetObjectHandle(windowName, objName, type));
     }
     finally
     {
         type = null;
     }
 }
Пример #6
0
        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");
        }
Пример #7
0
 private AutomationElement GetObjectHandle(string windowName,
                                           string objName, ControlType[] type)
 {
     return(utils.GetObjectHandle(windowName, objName, type));
 }
Пример #8
0
        private int InternalMenuHandler(String windowName, String objName,
                                        ref ArrayList menuList, String actionType = "Select")
        {
            if (String.IsNullOrEmpty(windowName) ||
                String.IsNullOrEmpty(objName))
            {
                throw new XmlRpcFaultException(123, "Argument cannot be empty.");
            }
            Object pattern                = null;
            String currObjName            = null;
            AutomationElementCollection c = null;

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

            InternalTreeWalker w = new InternalTreeWalker();

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

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

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

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

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

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

                        default:
                            break;
                        }
                    }
                    else if ((tmpContextHandle = utils.InternalWaitTillControlTypeExist(
                                  ControlType.Menu, processId, 3)) != null)
                    {
                        LogMessage("InternalWaitTillControlTypeExist");
                        // Find object from current handle, rather than navigating
                        // the complete window
                        childHandle = tmpContextHandle;
                        if (actionType != "SubMenu")
                        {
                            continue;
                        }
                        else if (currObjName == objName)
                        {
                            switch (actionType)
                            {
                            case "SubMenu":
                                int status = HandleSubMenu(w.walker.GetFirstChild(childHandle),
                                                           firstObjHandle, ref menuList);
                                if (status == 1)
                                {
                                    return(1);
                                }
                                break;
                            }
                        }
                    }
                    else if (c != null && c.Count > 0)
                    {
                        if (currObjName == objName)
                        {
                            switch (actionType)
                            {
                            case "SubMenu":
                                int status = HandleSubMenu(w.walker.GetFirstChild(childHandle),
                                                           firstObjHandle, ref menuList);
                                if (status == 1)
                                {
                                    return(1);
                                }
                                break;
                            }
                        }
                        LogMessage("c != null && c.Count > 0");
                        childHandle = windowHandle;
                        continue;
                    }
                    // Required for Notepad like app
                    if ((c == null || c.Count == 0))
                    {
                        LogMessage("Work around for Windows application");
                        LogMessage(windowHandle.Current.Name + " : " + objName);
                        AutomationElement tmpChildHandle = utils.GetObjectHandle(
                            windowHandle, objName,
                            type, false);
                        // Work around for Notepad, as it doesn't find the menuitem
                        // on clicking any menu
                        if (tmpChildHandle != null)
                        {
                            LogMessage("Work around: tmpChildHandle != null");
                            if (actionType == "SubMenu" && currObjName == objName)
                            {
                                // Work around for Notepad like app
                                childHandle = tmpChildHandle;
                            }
                            else
                            {
                                // Work around for Notepad like app,
                                // but for actionType other than SubMenu
                                childHandle = windowHandle;
                            }
                        }
                    }
                    if (currObjName == objName)
                    {
                        switch (actionType)
                        {
                        case "SubMenu":
                            int status = HandleSubMenu(w.walker.GetFirstChild(childHandle),
                                                       firstObjHandle, ref menuList);
                            if (status == 1)
                            {
                                return(1);
                            }
                            break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogMessage(ex);
                if (firstObjHandle != null && actionType != "Window")
                {
                    // Set it back to old state, else the menu selection left there
                    utils.InternalXYClick(firstObjHandle);
                }
                if (((ex is ElementNotAvailableException) ||
                     (ex is UnauthorizedAccessException)) &&
                    actionType == "Window")
                {
                    // API closewindow() can close Windows Explorer on XP, but:
                    // -----------------------------------------------------------
                    // if (childHandle.TryGetCurrentPattern(InvokePattern.Pattern,
                    //     out pattern) || childHandle.TryGetCurrentPattern(
                    //     ExpandCollapsePattern.Pattern, out pattern))
                    // -----------------------------------------------------------
                    // Sometimes above code will throw exception, sometimes not:
                    //    System.Runtime.InteropServices.COMException (0x80040201):
                    //       Exception from HRESULT: 0x80040201
                    //    System.UnauthorizedAccessException, Access is denied:
                    //       Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
                    // So use this if block as workaround
                    return(1);
                }
                if (ex is XmlRpcFaultException)
                {
                    throw;
                }
                else
                {
                    throw new XmlRpcFaultException(123,
                                                   "Unhandled exception: " + ex.Message);
                }
            }
            finally
            {
                c             = null;
                w             = null;
                pattern       = null;
                windowHandle  = childHandle = null;
                prevObjHandle = firstObjHandle = null;
            }
        }