Пример #1
0
        private Response FromException(AutomationElement element, Exception exception, CommandEnvironment environment, CancellationToken cancellationToken)
        {
            if (exception is AggregateException aggregateException)
            {
                return(FromException(element, aggregateException.InnerException, environment, cancellationToken));
            }

            if (exception is ElementNotEnabledException enee)
            {
                // maybe the interaction is not possible, because there is a modal window blocking the UI
                var parentWindow = element.GetTopLevelWindow();
                if (parentWindow.IsBlockedByModalWindow())
                {
                    var modalWindow = environment.GetModalWindow(cancellationToken);

                    return(Response.CreateErrorResponse(
                               WebDriverStatusCode.UnexpectedAlertOpen,
                               "Interaction with the element is not possible, because there is a modal window blocking the UI.",
                               payload: modalWindow?.GetWindowCaption(),
                               error: "Invalid element state",
                               sessionId: environment.SessionId));
                }

                return(Response.CreateErrorResponse(WebDriverStatusCode.InvalidElementState, enee.Message));
            }

            ExceptionDispatchInfo.Capture(exception).Throw();

            return(null);
        }
        public override Response Execute(CommandEnvironment environment, Dictionary <string, object> parameters, System.Threading.CancellationToken cancellationToken)
        {
            var modalWindow = environment.GetModalWindow(cancellationToken);

            if (modalWindow == null)
            {
                return(Response.CreateErrorResponse(WebDriverStatusCode.NoAlertPresent, string.Empty));
            }

            var buttons = new DescendantIterator(modalWindow, false, cancellationToken)
                          .Cast <AutomationElement>()
                          .Where(el => el.Current.ControlType == ControlType.Button)
                          .ToList();

            var captions = (environment.GetDesiredCapabilityValue(_capabilityName) + ";" + _defaultCaptions)
                           .ToString()
                           .Split(new[] { ';' }, System.StringSplitOptions.RemoveEmptyEntries);

            // button accepting/dismissing the dialog is identified by its caption
            var button = buttons.FirstOrDefault(b =>
            {
                var text = b.GetText();
                return(captions.Any(c => text.Equals(c, System.StringComparison.CurrentCultureIgnoreCase)));
            });

            if (button != null)
            {
                return(new ClickElementCommandHandler().GetResponse(button, environment));
            }

            return(Response.CreateErrorResponse(WebDriverStatusCode.UnhandledError,
                                                $"Modal dialog cannot be {_verb}ed, button with one of default captions ({string.Join(", ", _defaultCaptions.Split(';'))}) was not found. You have to {_verb} the modal dialog yourself or set the driver capability '{_capabilityName}' with semicolon delimited list of button captions for the {_verb} command to succeed.",
                                                $"Modal dialog cannot be {_verb}ed",
                                                sessionId: environment.SessionId));
        }
Пример #3
0
        public override Response Execute(CommandEnvironment environment, Dictionary <string, object> parameters, System.Threading.CancellationToken cancellationToken)
        {
            var modalWindow = environment.GetModalWindow(cancellationToken);

            if (modalWindow == null)
            {
                return(Response.CreateErrorResponse(WebDriverStatusCode.NoAlertPresent, string.Empty));
            }

            var enumerable = new DescendantIterator(modalWindow, false, cancellationToken).Cast <AutomationElement>()
                             .Where(el => el.Current.ControlType == ControlType.Text || el.Current.ControlType == ControlType.Edit)
                             .OrderBy(el => el.Current.BoundingRectangle.TopLeft, new PointComparer());

            var alertText = string.Join(Environment.NewLine, enumerable.Select(el => el.GetText()));

            return(Response.CreateSuccessResponse(alertText));
        }