Example #1
0
        /// <summary>
        /// Executes the command.
        /// </summary>
        /// <param name="environment">The <see cref="CommandEnvironment"/> to use in executing the command.</param>
        /// <param name="parameters">The <see cref="Dictionary{string, object}"/> containing the command parameters.</param>
        /// <returns>The JSON serialized string representing the command response.</returns>
        public override Response Execute(CommandEnvironment environment, Dictionary <string, object> parameters)
        {
            var windows = new BreadthFirstSearch().Find(environment.Cache.AutomationElement, ControlType.Window, environment.GetCancellationToken())
                          .Where(w => w.Current.ControlType == ControlType.Window)
                          .ToList();

            object[] handles = windows.Select(w => w.Current.AutomationId).ToArray();
            return(Response.CreateSuccessResponse(handles));
        }
Example #2
0
        /// <summary>
        /// Executes the command.
        /// </summary>
        /// <param name="environment">The <see cref="CommandEnvironment"/> to use in executing the command.</param>
        /// <param name="parameters">The <see cref="Dictionary{string, object}"/> containing the command parameters.</param>
        /// <returns>The JSON serialized string representing the command response.</returns>
        public override Response Execute(CommandEnvironment environment, Dictionary <string, object> parameters)
        {
            if (!parameters.TryGetValue("handle", out object handle))
            {
                return(Response.CreateMissingParametersResponse("handle"));
            }

            var windowIdentifier = handle?.ToString();

            if (windowIdentifier != CommandEnvironment.GlobalWindowHandle)
            {
                var windows = new BreadthFirstSearch().Find(environment.Cache.AutomationElement, ControlType.Window, environment.GetCancellationToken())
                              .Where(w => w.Current.ControlType == ControlType.Window)
                              .ToList();

                var matchingWindow = windows.SingleOrDefault(w => w.Current.AutomationId == windowIdentifier);
                if (matchingWindow != null)
                {
                    // if any of those windows is modal and it is not the window that user is switching to, return an error
                    var modalWindow = windows.FirstOrDefault(w => (bool)w.GetAutomationElementPropertyValue(WindowPattern.IsModalProperty) == true);
                    if (modalWindow != null && modalWindow != matchingWindow)
                    {
                        return(Response.CreateErrorResponse(WebDriverStatusCode.UnhandledError, "Window is not enabled."));
                    }

                    environment.SwitchToWindow(matchingWindow);
                    return(Response.CreateSuccessResponse());
                }

                return(Response.CreateErrorResponse(WebDriverStatusCode.NoSuchWindow, "No window found"));
            }

            return(Response.CreateSuccessResponse());
        }
        protected override Response GetResponse(AutomationElement automationElement, CommandEnvironment environment, Dictionary <string, object> parameters)
        {
            if (!parameters.TryGetValue("using", out var mechanism))
            {
                return(Response.CreateMissingParametersResponse("using"));
            }

            if (!parameters.TryGetValue("value", out var criteria))
            {
                return(Response.CreateMissingParametersResponse("value"));
            }

            var token = environment.GetCancellationToken();

            try
            {
                var element = environment.Cache.FindElements(automationElement, mechanism.ToString(), criteria.ToString(), token).FirstOrDefault();
                if (element == null)
                {
                    throw new NoSuchElementException();
                }

                environment.Cache.AddToCache(element);

                var response = new Response
                {
                    Status    = WebDriverStatusCode.Success,
                    SessionId = environment.SessionId,
                    Value     = new Dictionary <string, object>
                    {
                        { CommandEnvironment.ElementObjectKey, element.Item1 },
                        { string.Empty, element.Item2.Current.AutomationId }
                    }
                };

                if (response.Status == WebDriverStatusCode.Success)
                {
                    // Return early for success
                    var foundElement = response.Value as Dictionary <string, object>;
                    if (foundElement != null && foundElement.ContainsKey(CommandEnvironment.ElementObjectKey))
                    {
                        return(response);
                    }
                }
                else if (response.Status != WebDriverStatusCode.NoSuchElement)
                {
                    if (mechanism.ToString().ToUpperInvariant() != "XPATH" && response.Status == WebDriverStatusCode.InvalidSelector)
                    {
                        //continue;
                    }

                    // Also return early for response of not NoSuchElement.
                    return(response);
                }

                string errorMessage = string.Format(CultureInfo.InvariantCulture, "No element found for {0} == '{1}'", mechanism.ToString(), criteria.ToString());
                response = Response.CreateErrorResponse(WebDriverStatusCode.NoSuchElement, errorMessage);
                return(response);
            }
            catch (Exception e)
            {
                throw;
            }
        }