Example #1
0
    public WindowExecution(WindowType type, string[] commandsData, ActionKeyboard keyboard, VisualRecorder recorder, DesktopManager desktop) : base()
    {
        DesktopWindow window = null;

        if (type == WindowType.Close || type == WindowType.Handle || type == WindowType.State || type == WindowType.Keys)
        {
            _      = int.TryParse(commandsData[0], out int handle);
            window = desktop.GetWindowByHandle(handle);
            if (window != null)
            {
                if (type == WindowType.State)
                {
                    executor = new StateExecutor(window, response, commandsData[1]);
                }
                else if (type == WindowType.Keys)
                {
                    executor = new KeysExecutor(window, response, keyboard, commandsData[1]);
                }
                else if (type == WindowType.Close)
                {
                    executor = new CloseExecutor(window, response);
                }
                else if (type == WindowType.Handle)
                {
                    executor = new HandleExecutor(window, response);
                }
            }
        }
        else if (type == WindowType.ToFront)
        {
            if (commandsData.Length > 1)
            {
                _ = int.TryParse(commandsData[0], out int handle);
                _ = int.TryParse(commandsData[1], out int pid);

                window = desktop.GetWindowByHandle(handle);

                if (window == null)
                {
                    window = desktop.GetWindowIndexByPid(pid, 0);
                }

                recorder.CurrentPid = pid;
            }
            else
            {
                _ = int.TryParse(commandsData[0], out int pid);
                recorder.CurrentPid = pid;

                List <DesktopWindow> windows = desktop.GetOrderedWindowsByPid(pid);
                if (windows.Count > 0)
                {
                    window = windows[0];
                }
            }

            if (window != null)
            {
                window.ToFront();
            }
        }
        else if (type == WindowType.Url)
        {
            _      = int.TryParse(commandsData[0], out int handle);
            window = desktop.GetWindowByHandle(handle);

            if (window.isIE)
            {
                window.ToFront();

                AutomationElement win        = window.Element;
                AutomationElement addressBar = win.FindFirst(TreeScope.Descendants, win.ConditionFactory.ByControlType(ControlType.Pane).And(win.ConditionFactory.ByClassName("Address Band Root")));
                if (addressBar != null)
                {
                    addressBar.Focus();
                    AutomationElement edit = addressBar.FindFirstChild(addressBar.ConditionFactory.ByControlType(ControlType.Edit));

                    if (edit != null)
                    {
                        try
                        {
                            edit.Focus();
                            edit.Click();
                            if (edit.Patterns.Value.IsSupported)
                            {
                                edit.Patterns.Value.Pattern.SetValue(commandsData[1]);
                            }
                            Keyboard.Type(VirtualKeyShort.ENTER);
                        }
                        catch { }
                    }
                }
            }
            else
            {
                string fname = Environment.ExpandEnvironmentVariables(commandsData[1]);
                if (fname.StartsWith("file:///"))
                {
                    fname = fname.Substring(8);
                }

                try
                {
                    if (Directory.Exists(@fname))
                    {
                        window.ToFront();
                        keyboard.AddressBar(Path.GetFullPath(@fname));
                    }
                    else
                    {
                        response.SetError(UNREACHABLE_GOTO_URL, "directory not found : " + fname);
                    }
                }
                catch (Exception e)
                {
                    response.SetError(UNREACHABLE_GOTO_URL, "directory path not valid : " + fname + " (" + e.Message + ")");
                }
            }
        }
        else if (type == WindowType.Title)
        {
            if (commandsData[0].Equals("jx"))
            {
                window = desktop.GetJxWindowPid(commandsData[1]);
            }
            else
            {
                window = desktop.GetWindowByTitle(commandsData[0]);
            }
        }
        else if (type == WindowType.List)
        {
            _        = int.TryParse(commandsData[0], out int pid);
            executor = new ListExecutor(window, response, desktop.GetOrderedWindowsByPid(pid).ToArray());
        }
        else if (type == WindowType.Switch)
        {
            if (commandsData.Length > 1)
            {
                _      = int.TryParse(commandsData[0], out int pid);
                _      = int.TryParse(commandsData[1], out int index);
                window = desktop.GetWindowIndexByPid(pid, index);

                if (window == null)
                {
                    _      = int.TryParse(commandsData[2], out int handle);
                    window = desktop.GetWindowByHandle(handle);
                }
            }
            else
            {
                _      = int.TryParse(commandsData[0], out int handle);
                window = desktop.GetWindowByHandle(handle);
            }

            if (window != null)
            {
                window.ToFront();
            }
        }
        else if (type == WindowType.Move || type == WindowType.Resize)
        {
            _ = int.TryParse(commandsData[0], out int handle);
            _ = int.TryParse(commandsData[1], out int value1);
            _ = int.TryParse(commandsData[2], out int value2);

            window = desktop.GetWindowByHandle(handle);
            if (window != null)
            {
                if (type == WindowType.Move)
                {
                    executor = new MoveExecutor(window, response, value1, value2);
                }
                else
                {
                    executor = new ResizeExecutor(window, response, value1, value2);
                }
            }
        }

        if (executor == null)
        {
            executor = new EmptyExecutor(window, response);
        }
    }