/// <summary>
        /// Attempts to add a command to the repository of commands known to this executor.
        /// </summary>
        /// <param name="commandName">The name of the command to attempt to add.</param>
        /// <param name="info">The <see cref="CommandInfo"/> describing the commnd to add.</param>
        /// <returns><see langword="true"/> if the new command has been added successfully; otherwise, <see langword="false"/>.</returns>
        public bool TryAddCommand(string commandName, CommandInfo info)
        {
            HttpCommandInfo commandInfo = info as HttpCommandInfo;

            if (commandInfo == null)
            {
                return(false);
            }

            return(this.commandInfoRepository.TryAddCommand(commandName, commandInfo));
        }
        /// <summary>
        /// Executes a command
        /// </summary>
        /// <param name="commandToExecute">The command you wish to execute</param>
        /// <returns>A response from the browser</returns>
        public virtual Response Execute(Command commandToExecute)
        {
            if (commandToExecute == null)
            {
                throw new ArgumentNullException(nameof(commandToExecute), "commandToExecute cannot be null");
            }

            HttpCommandInfo info = this.commandInfoRepository.GetCommandInfo <HttpCommandInfo>(commandToExecute.Name);

            if (info == null)
            {
                throw new NotImplementedException(string.Format("The command you are attempting to execute, {0}, does not exist in the protocol dialect used by the remote end.", commandToExecute.Name));
            }

            if (this.client == null)
            {
                this.CreateHttpClient();
            }

            HttpRequestInfo  requestInfo  = new HttpRequestInfo(this.remoteServerUri, commandToExecute, info);
            HttpResponseInfo responseInfo = null;

            try
            {
                // Use TaskFactory to avoid deadlock in multithreaded implementations.
                responseInfo = new TaskFactory(CancellationToken.None,
                                               TaskCreationOptions.None,
                                               TaskContinuationOptions.None,
                                               TaskScheduler.Default)
                               .StartNew(() => this.MakeHttpRequest(requestInfo))
                               .Unwrap()
                               .GetAwaiter()
                               .GetResult();
            }
            catch (HttpRequestException ex)
            {
                string unknownErrorMessage = "An unknown exception was encountered sending an HTTP request to the remote WebDriver server for URL {0}. The exception message was: {1}";
                throw new WebDriverException(string.Format(CultureInfo.InvariantCulture, unknownErrorMessage, requestInfo.FullUri.AbsoluteUri, ex.Message), ex);
            }
            catch (TaskCanceledException ex)
            {
                string timeoutMessage = "The HTTP request to the remote WebDriver server for URL {0} timed out after {1} seconds.";
                throw new WebDriverException(string.Format(CultureInfo.InvariantCulture, timeoutMessage, requestInfo.FullUri.AbsoluteUri, this.serverResponseTimeout.TotalSeconds), ex);
            }

            Response toReturn = this.CreateResponse(responseInfo);

            return(toReturn);
        }
 public HttpRequestInfo(Uri serverUri, Command commandToExecute, HttpCommandInfo commandInfo)
 {
     this.FullUri     = commandInfo.CreateCommandUri(serverUri, commandToExecute);
     this.HttpMethod  = commandInfo.Method;
     this.RequestBody = commandToExecute.ParametersAsJsonString;
 }
Beispiel #4
0
        private void AddCustomFirefoxCommand(string commandName, string method, string resourcePath)
        {
            HttpCommandInfo commandInfoToAdd = new HttpCommandInfo(method, resourcePath);

            this.CommandExecutor.TryAddCommand(commandName, commandInfoToAdd);
        }