OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError C# (CSharp) Method

UnpackAndThrowOnError() private static method

private static UnpackAndThrowOnError ( OpenQA.Selenium.Remote.Response errorResponse ) : void
errorResponse OpenQA.Selenium.Remote.Response
return void
        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);

                        default:
                            throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "{0} ({1})", errorMessage, errorResponse.Status));
                    }
                }
                else
                {
                    throw new WebDriverException("Unexpected error. " + errorResponse.Value.ToString());
                }
            }
        }

Usage Example

Exemplo n.º 1
0
        protected virtual Response Execute(string driverCommandToExecute, Dictionary <string, object> parameters)
        {
            Command  commandToExecute = new Command(this.sessionId, driverCommandToExecute, parameters);
            Response response         = new Response();

            try
            {
                response = this.executor.Execute(commandToExecute);
            }
            catch (WebException value)
            {
                response.Status = WebDriverResult.UnhandledError;
                response.Value  = value;
            }
            if (response.Status != WebDriverResult.Success)
            {
                RemoteWebDriver.UnpackAndThrowOnError(response);
            }
            return(response);
        }