Beispiel #1
0
        private bool SelectListItem(AutomationElement element, String itemText,
                                    bool verify = false)
        {
            if (element == null || String.IsNullOrEmpty(itemText))
            {
                throw new XmlRpcFaultException(123,
                                               "Argument cannot be null or empty.");
            }

            element.SetFocus();

            LogMessage("SelectListItem Element: " + element.Current.Name +
                       " - Type: " + element.Current.ControlType.ProgrammaticName);
            Object            pattern = null;
            AutomationElement elementItem;

            try
            {
                utils.InternalWait(1);

                String enterKeys = itemText;

                if (enterKeys.StartsWith("*"))
                {
                    enterKeys = enterKeys.Remove(0, 1);
                }

                Keyboard kb = new Keyboard(utils);
                kb.GenerateKeyEvent(enterKeys);

                elementItem = utils.GetObjectHandle(element, itemText);
                if (elementItem != null)
                {
                    LogMessage(elementItem.Current.Name + " : " +
                               elementItem.Current.ControlType.ProgrammaticName);
                    if (verify)
                    {
                        bool status = false;
                        if (elementItem.TryGetCurrentPattern(SelectionItemPattern.Pattern,
                                                             out pattern))
                        {
                            status = ((SelectionItemPattern)pattern).Current.IsSelected;
                        }
                        if (element.TryGetCurrentPattern(ExpandCollapsePattern.Pattern,
                                                         out pattern))
                        {
                            LogMessage("ExpandCollapsePattern");
                            element.SetFocus();
                            ((ExpandCollapsePattern)pattern).Collapse();
                        }
                        return(status);
                    }
                    if (elementItem.TryGetCurrentPattern(ScrollItemPattern.Pattern,
                                                         out pattern))
                    {
                        LogMessage("ScrollItemPattern");
                        ((ScrollItemPattern)pattern).ScrollIntoView();
                    }
                    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
                        return(utils.InternalClick(elementItem));
                    }
                    else if (elementItem.TryGetCurrentPattern(ExpandCollapsePattern.Pattern,
                                                              out pattern))
                    {
                        LogMessage("ExpandCollapsePattern");
                        ((ExpandCollapsePattern)pattern).Expand();
                        element.SetFocus();
                        return(true);
                    }
                    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;
                elementItem = null;
            }
            throw new XmlRpcFaultException(123,
                                           "Unable to find item in the list: " + itemText);
        }
Beispiel #2
0
        public int SimulateMouseMove(int source_x, int source_y, int dest_x, int dest_y, double delay = 0.0)
        {
            int[]   size;
            Generic generic = new Generic(utils);

            try
            {
                size = generic.GetWindowSize("paneProgramManager");
            }
            finally
            {
                generic = null;
            }
            if (source_x < size[0] || source_y <size[1] ||
                                                dest_x> size[2] || dest_y > size[3] ||
                source_x > size[2] || source_y > size[3] ||
                dest_x < size[0] || dest_y < size[1])
            {
                return(0);
            }
            bool x_flag = true; // Iterated x ?
            bool y_flag = true; // Iterated y ?

            while (true)
            {
                if (x_flag)
                {
                    if (source_x > dest_x)
                    {
                        // If source X greather than dest X
                        // then move -1 pixel
                        source_x -= 1;
                    }
                    else if (source_x < dest_x)
                    {
                        // If source X less than dest X
                        // then move +1 pixel
                        source_x += 1;
                    }
                    else
                    {
                        // If source X equal to dest X
                        // then don't process X co-ordinate
                        x_flag = false;
                    }
                }
                if (y_flag)
                {
                    if (source_y > dest_y)
                    {
                        // If source Y greather than dest Y
                        // then move -1 pixel
                        source_y -= 1;
                    }
                    else if (source_y < dest_y)
                    {
                        // If source Y less than dest Y
                        // then move +1 pixel
                        source_y += 1;
                    }
                    else
                    {
                        // If source Y equal to dest Y
                        // then don't process Y co-ordinate
                        y_flag = false;
                    }
                }
                if (delay != 0.0)
                {
                    utils.InternalWait(delay);
                }
                // Start mouse move from source_x, source_y to dest_x, dest_y
                GenerateMouseEvent(source_x, source_y, "abs");
                if (source_x == dest_x && source_y == dest_y)
                {
                    // If we have reached the dest_x and dest_y
                    // then break the loop
                    break;
                }
            }
            return(1);
        }
Beispiel #3
0
        public int HasState(String windowName, String objName,
                            String state, int guiTimeOut = 0)
        {
            Object                      pattern;
            AutomationElement           childHandle;
            AutomationElementCollection c;

            try
            {
                childHandle = GetObjectHandle(windowName, objName, null);
                c           = childHandle.FindAll(TreeScope.Children,
                                                  Condition.TrueCondition);
                if (c == null)
                {
                    LogMessage("Unable to get row count.");
                    return(0);
                }
                do
                {
                    LogMessage("State: " + state);
                    switch (state.ToLower(CultureInfo.CurrentCulture))
                    {
                    case "visible":
                    case "showing":
                        if (childHandle.Current.IsOffscreen == false)
                        {
                            return(1);
                        }
                        break;

                    case "enabled":
                        if (utils.IsEnabled(childHandle, false))
                        {
                            return(1);
                        }
                        break;

                    case "focused":
                        LogMessage("childHandle.Current.HasKeyboardFocus: " +
                                   childHandle.Current.HasKeyboardFocus);
                        if (childHandle.Current.HasKeyboardFocus)
                        {
                            return(1);
                        }
                        break;

                    case "checked":
                        if (childHandle.TryGetCurrentPattern(TogglePattern.Pattern,
                                                             out pattern))
                        {
                            if (((TogglePattern)pattern).Current.ToggleState ==
                                ToggleState.On)
                            {
                                return(1);
                            }
                        }
                        break;

                    case "selected":
                        if (childHandle.TryGetCurrentPattern(SelectionItemPattern.Pattern,
                                                             out pattern))
                        {
                            if (((SelectionItemPattern)pattern).Current.IsSelected)
                            {
                                return(1);
                            }
                        }
                        break;

                    case "selectable":
                        if (utils.IsEnabled(childHandle) &&
                            childHandle.TryGetCurrentPattern(SelectionItemPattern.Pattern,
                                                             out pattern))
                        {
                            // Assuming, if its enabled and has selection item pattern
                            // then its selectable
                            return(1);
                        }
                        break;

                    case "editable":
                        if (childHandle.TryGetCurrentPattern(ValuePattern.Pattern,
                                                             out pattern))
                        {
                            if (((ValuePattern)pattern).Current.IsReadOnly)
                            {
                                return(0);
                            }
                            else
                            {
                                return(1);
                            }
                        }
                        break;
                    }
                    if (guiTimeOut > 0)
                    {
                        // Wait a second and retry checking the state
                        utils.InternalWait(1);
                        guiTimeOut--;
                    }
                } while (guiTimeOut > 0);
            }
            catch (Exception ex)
            {
                LogMessage(ex);
            }
            finally
            {
                c           = null;
                pattern     = null;
                childHandle = null;
            }
            return(0);
        }
Beispiel #4
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);
        }
Beispiel #5
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;
            }
        }