Provides a way to store errors from a repsonse
Пример #1
0
        private static void UnpackAndThrowOnError(Response errorResponse)
        {
            // Check the status code of the error, and only handle if not success.
            if (errorResponse.Status != WebDriverResult.Success)
            {
                Dictionary<string, object> errorAsDictionary = errorResponse.Value as Dictionary<string, object>;
                if (errorAsDictionary != null)
                {
                    ErrorResponse errorResponseObject = new ErrorResponse(errorAsDictionary);
                    string errorMessage = errorResponseObject.Message;
                    switch (errorResponse.Status)
                    {
                        case WebDriverResult.NoSuchElement:
                            throw new NoSuchElementException(errorMessage);

                        case WebDriverResult.NoSuchFrame:
                            throw new NoSuchFrameException(errorMessage);

                        case WebDriverResult.UnknownCommand:
                            throw new NotImplementedException(errorMessage);

                        case WebDriverResult.ObsoleteElement:
                            throw new StaleElementReferenceException(errorMessage);

                        case WebDriverResult.ElementNotDisplayed:
                            throw new ElementNotVisibleException(errorMessage);

                        case WebDriverResult.InvalidElementState:
                        case WebDriverResult.ElementNotSelectable:
                            throw new InvalidElementStateException(errorMessage);

                        case WebDriverResult.UnhandledError:
                            throw new InvalidOperationException(errorMessage);

                        case WebDriverResult.NoSuchDocument:
                            throw new NoSuchElementException(errorMessage);

                        case WebDriverResult.Timeout:
                            throw new TimeoutException("The driver reported that the command timed out. There may "
                                                       + "be several reasons for this. Check that the destination "
                                                       + "site is in IE's 'Trusted Sites' (accessed from Tools->"
                                                       + "Internet Options in the 'Security' tab) If it is a "
                                                       + "trusted site, then the request may have taken more than "
                                                       + "a minute to finish.");

                        case WebDriverResult.NoSuchWindow:
                            throw new NoSuchWindowException(errorMessage);

                        case WebDriverResult.InvalidCookieDomain:
                        case WebDriverResult.UnableToSetCookie:
                            throw new WebDriverException(errorMessage);

                        case WebDriverResult.AsyncScriptTimeout:
                            throw new TimeoutException(errorMessage);

                        case WebDriverResult.NoAlertPresent:
                            throw new NoAlertPresentException(errorMessage);

                        case WebDriverResult.InvalidSelector:
                            throw new InvalidSelectorException(errorMessage);

                        default:
                            throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "{0} ({1})", errorMessage, errorResponse.Status));
                    }
                }
                else
                {
                    throw new WebDriverException("Unexpected error. " + errorResponse.Value.ToString());
                }
            }
        }
Пример #2
0
        private static void UnpackAndThrowOnError(Response errorResponse)
        {
            // Check the status code of the error, and only handle if not success.
            if (errorResponse.Status != WebDriverResult.Success)
            {
                Dictionary<string, object> errorAsDictionary = errorResponse.Value as Dictionary<string, object>;
                if (errorAsDictionary != null)
                {
                    ErrorResponse errorResponseObject = new ErrorResponse(errorAsDictionary);
                    string errorMessage = errorResponseObject.Message;
                    switch (errorResponse.Status)
                    {
                        case WebDriverResult.NoSuchElement:
                            throw new NoSuchElementException(errorMessage);

                        case WebDriverResult.NoSuchFrame:
                            throw new NoSuchFrameException(errorMessage);

                        case WebDriverResult.UnknownCommand:
                            throw new NotImplementedException(errorMessage);

                        case WebDriverResult.ObsoleteElement:
                            throw new StaleElementReferenceException(errorMessage);

                        case WebDriverResult.ElementNotDisplayed:
                            throw new ElementNotVisibleException(errorMessage);

                        case WebDriverResult.InvalidElementState:
                        case WebDriverResult.ElementNotSelectable:
                            throw new InvalidElementStateException(errorMessage);

                        case WebDriverResult.UnhandledError:
                            throw new InvalidOperationException(errorMessage);

                        case WebDriverResult.NoSuchDocument:
                            throw new NoSuchElementException(errorMessage);

                        case WebDriverResult.Timeout:
                            throw new WebDriverTimeoutException(errorMessage);

                        case WebDriverResult.NoSuchWindow:
                            throw new NoSuchWindowException(errorMessage);

                        case WebDriverResult.InvalidCookieDomain:
                        case WebDriverResult.UnableToSetCookie:
                            throw new WebDriverException(errorMessage);

                        case WebDriverResult.AsyncScriptTimeout:
                            throw new WebDriverTimeoutException(errorMessage);

                        case WebDriverResult.UnexpectedAlertOpen:
                            // TODO(JimEvans): Handle the case where the unexpected alert setting
                            // has been set to "ignore", so there is still a valid alert to be
                            // handled.
                            string alertText = string.Empty;
                            if (errorAsDictionary.ContainsKey("alert"))
                            {
                                Dictionary<string, object> alertDescription = errorAsDictionary["alert"] as Dictionary<string, object>;
                                if (alertDescription != null && alertDescription.ContainsKey("text"))
                                {
                                    alertText = alertDescription["text"].ToString();
                                }
                            }

                            throw new UnhandledAlertException(errorMessage, alertText);

                        case WebDriverResult.NoAlertPresent:
                            throw new NoAlertPresentException(errorMessage);

                        case WebDriverResult.InvalidSelector:
                            throw new InvalidSelectorException(errorMessage);

                        default:
                            throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "{0} ({1})", errorMessage, errorResponse.Status));
                    }
                }
                else
                {
                    throw new WebDriverException("Unexpected error. " + errorResponse.Value.ToString());
                }
            }
        }
Пример #3
0
        private static void UnpackAndThrowOnError(object error)
        {
            // The exception object is wrapped so it appears as a JSON string. Parse
            // the JSON string into an object first, then we can assemble the correct
            // exception.
            string errorString = error.ToString();
            ErrorResponse errorResponseObject;
            if (errorString.StartsWith("{", StringComparison.OrdinalIgnoreCase))
            {
                errorResponseObject = JsonConvert.DeserializeObject<ErrorResponse>(errorString);
            }
            else
            {
                errorResponseObject = new ErrorResponse { Message = errorString, ClassName = "." };
            }

            if (errorResponseObject != null)
            {
                // TODO: I don't like this approach overmuch. It's too dependent on
                // class name, and only supports the Java server. Will need to be
                // refactored to support other remote server implementations.
                // Assume we have a class member to the Java exception
                string errorMessage = errorResponseObject.Message;
                string errorClass = errorResponseObject.ClassName;
                string[] classNameParts = errorClass.Split(new string[] { "." }, StringSplitOptions.None);
                string className = classNameParts[classNameParts.Length - 1];
                if (className == "NoSuchElementException")
                {
                    throw new NoSuchElementException(errorMessage);
                }
                else if (className == "NoSuchFrameException")
                {
                    throw new NoSuchFrameException(errorMessage);
                }
                else if (className == "StaleElementReferenceException")
                {
                    throw new StaleElementReferenceException(errorMessage);
                }
                else if (className == "ElementNotVisibleException")
                {
                    throw new ElementNotVisibleException(errorMessage);
                }
                else if (className == "UnsupportedOperationException")
                {
                    if (errorMessage.Contains("toggle"))
                    {
                        throw new NotImplementedException(errorMessage);
                    }

                    throw new NotSupportedException(errorMessage);
                }
                else if (className == "WebDriverException")
                {
                    if (errorMessage.Contains("switch to frame"))
                    {
                        throw new InvalidOperationException(errorMessage);
                    }

                    throw new WebDriverException(errorMessage);
                }
                else if (className == "UnexpectedJavascriptExecutionException")
                {
                    throw new InvalidOperationException(errorMessage);
                }
                else if (className == "TimedOutException")
                {
                    throw new TimeoutException(errorMessage);
                }
                else if (className == "NoSuchWindowException")
                {
                    throw new NoSuchWindowException(errorMessage);
                }
                else
                {
                    throw new InvalidOperationException(errorMessage);
                }
            }
            else
            {
                throw new WebDriverException("Unexpected error. " + errorString);
            }
        }