Provides a way of executing Commands over HTTP
Inheritance: ICommandExecutor
        /// <summary>
        /// Executes a command
        /// </summary>
        /// <param name="commandToExecute">The command you wish to execute</param>
        /// <returns>A response from the browser</returns>
        public Response Execute(Command commandToExecute)
        {
            Response toReturn = null;
            if (commandToExecute.Name == DriverCommand.NewSession)
            {
                this.server.Start();
                this.internalExecutor = new HttpCommandExecutor(this.server.ExtensionUri, this.commandTimeout);
            }

            // Use a try-catch block to catch exceptions for the Quit
            // command, so that we can get the finally block.
            try
            {
                toReturn = this.internalExecutor.Execute(commandToExecute);
            }
            finally
            {
                if (commandToExecute.Name == DriverCommand.Quit)
                {
                    this.server.Dispose();
                }
            }

            return toReturn;
        }
Exemple #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ExtensionConnection"/> class.
        /// </summary>
        /// <param name="lockObject">An <see cref="ILock"/> object used to lock the mutex port before connection.</param>
        /// <param name="binary">The <see cref="FirefoxBinary"/> on which to make the connection.</param>
        /// <param name="profile">The <see cref="FirefoxProfile"/> creating the connection.</param>
        /// <param name="host">The name of the host on which to connect to the Firefox extension (usually "localhost").</param>
        public ExtensionConnection(ILock lockObject, FirefoxBinary binary, FirefoxProfile profile, string host)
        {
            this.profile = profile;
            if (binary == null)
            {
                this.process = new FirefoxBinary();
            }
            else
            {
                this.process = binary;
            }

            lockObject.LockObject(this.process.TimeoutInMilliseconds);
            try
            {
                int portToUse = DetermineNextFreePort(host, profile.Port);

                profile.Port = portToUse;
                profile.UpdateUserPreferences();
                this.process.Clean(profile);
                this.process.StartProfile(profile, null);

                SetAddress(host, portToUse);

                // TODO (JimEvans): Get a better url algorithm.
                executor = new HttpCommandExecutor(new Uri(string.Format(CultureInfo.InvariantCulture, "http://{0}:{1}/hub/", host, portToUse)));
            }
            finally
            {
                lockObject.UnlockObject();
            }
        }
Exemple #3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="DriverServiceCommandExecutor"/> class.
 /// </summary>
 /// <param name="driverService">The <see cref="DriverService"/> that drives the browser.</param>
 /// <param name="commandTimeout">The maximum amount of time to wait for each command.</param>
 /// <param name="enableKeepAlive"><see langword="true"/> if the KeepAlive header should be sent
 /// with HTTP requests; otherwise, <see langword="false"/>.</param>
 public DriverServiceCommandExecutor(DriverService driverService, TimeSpan commandTimeout, bool enableKeepAlive)
 {
     this.service          = driverService;
     this.internalExecutor = new HttpCommandExecutor(driverService.ServiceUrl, commandTimeout, enableKeepAlive);
 }
        /// <summary>
        /// Starts the connection to the extension.
        /// </summary>
        public void Start()
        {
            using (ILock lockObject = new SocketLock(this.profile.Port - 1))
            {
                lockObject.LockObject(this.process.Timeout);
                try
                {
                    int portToUse = DetermineNextFreePort(this.host, this.profile.Port);
                    this.profile.Port = portToUse;
                    this.profile.WriteToDisk();
                    this.process.Clean(this.profile);
                    this.process.StartProfile(this.profile, new string[] { "-foreground" });

                    this.SetAddress(portToUse);

                    // TODO (JimEvans): Get a better url algorithm.
                    this.executor = new HttpCommandExecutor(new Uri(string.Format(CultureInfo.InvariantCulture, "http://{0}:{1}/hub/", this.host, portToUse)), this.timeout);
                    this.ConnectToBrowser(this.process.Timeout);
                }
                finally
                {
                    lockObject.UnlockObject();
                }
            }
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="DriverServiceCommandExecutor"/> class.
 /// </summary>
 /// <param name="driverService">The <see cref="DriverService"/> that drives the browser.</param>
 /// <param name="commandTimeout">The maximum amount of time to wait for each command.</param>
 /// <param name="enableKeepAlive"><see langword="true"/> if the KeepAlive header should be sent
 /// with HTTP requests; otherwise, <see langword="false"/>.</param>
 public DriverServiceCommandExecutor(DriverService driverService, TimeSpan commandTimeout, bool enableKeepAlive)
 {
     this.service = driverService;
     this.internalExecutor = new HttpCommandExecutor(driverService.ServiceUrl, commandTimeout, enableKeepAlive);
 }
        private Response CreateResponse(WebRequest request)
        {
            Response        response        = new Response();
            HttpWebResponse httpWebResponse = null;

            try
            {
                httpWebResponse = (request.GetResponse() as HttpWebResponse);
            }
            catch (WebException ex)
            {
                httpWebResponse = (ex.Response as HttpWebResponse);
                if (ex.Status == WebExceptionStatus.Timeout)
                {
                    string format = "The HTTP request to the remote WebDriver server for URL {0} timed out after {1} seconds.";
                    throw new WebDriverException(string.Format(CultureInfo.InvariantCulture, format, new object[]
                    {
                        request.RequestUri.AbsoluteUri,
                        this.serverResponseTimeout.TotalSeconds
                    }), ex);
                }
                if (ex.Response == null)
                {
                    string format2 = "A exception with a null response was thrown sending an HTTP request to the remote WebDriver server for URL {0}. The status of the exception was {1}, and the message was: {2}";
                    throw new WebDriverException(string.Format(CultureInfo.InvariantCulture, format2, new object[]
                    {
                        request.RequestUri.AbsoluteUri,
                        ex.Status,
                        ex.Message
                    }), ex);
                }
            }
            if (httpWebResponse == null)
            {
                throw new WebDriverException("No response from server for url " + request.RequestUri.AbsoluteUri);
            }
            string textOfWebResponse = HttpCommandExecutor.GetTextOfWebResponse(httpWebResponse);

            if (httpWebResponse.ContentType != null && httpWebResponse.ContentType.StartsWith("application/json", StringComparison.OrdinalIgnoreCase))
            {
                response = Response.FromJson(textOfWebResponse);
            }
            else
            {
                response.Value = textOfWebResponse;
            }
            if (this.commandInfoRepository.SpecificationLevel < 1 && (httpWebResponse.StatusCode < HttpStatusCode.OK || httpWebResponse.StatusCode >= HttpStatusCode.BadRequest))
            {
                if (httpWebResponse.StatusCode >= HttpStatusCode.BadRequest && httpWebResponse.StatusCode < HttpStatusCode.InternalServerError)
                {
                    response.Status = WebDriverResult.UnhandledError;
                }
                else if (httpWebResponse.StatusCode >= HttpStatusCode.InternalServerError)
                {
                    if (httpWebResponse.StatusCode == HttpStatusCode.NotImplemented)
                    {
                        response.Status = WebDriverResult.UnknownCommand;
                    }
                    else if (response.Status == WebDriverResult.Success)
                    {
                        response.Status = WebDriverResult.UnhandledError;
                    }
                }
                else
                {
                    response.Status = WebDriverResult.UnhandledError;
                }
            }
            if (response.Value is string)
            {
                response.Value = ((string)response.Value).Replace("\r\n", "\n").Replace("\n", Environment.NewLine);
            }
            httpWebResponse.Close();
            return(response);
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="DriverServiceCommandExecutor"/> class.
 /// </summary>
 /// <param name="service">The <see cref="DriverService"/> that drives the browser.</param>
 /// <param name="commandExecutor">The <see cref="HttpCommandExecutor"/> object used to execute commands,
 /// communicating with the service via HTTP.</param>
 public DriverServiceCommandExecutor(DriverService service, HttpCommandExecutor commandExecutor)
 {
     this.service          = service;
     this.internalExecutor = commandExecutor;
 }
        private static ICommandExecutor GetCommandExecutor(int port, ICapabilities capabilities, TimeSpan commandTimeout)
        {
            // This method should be completely removed when the standalone server
            // is in widespread use.
            ICommandExecutor executor = null;
            if (capabilities.HasCapability("useLegacyInternalServer"))
            {
                useLegacyServer = (bool)capabilities.GetCapability("useLegacyInternalServer");
                executor = new HttpCommandExecutor(CreateServerUri(port), commandTimeout);
            }
            else
            {
                useLegacyServer = false;
                try
                {
                    executor = new DriverServiceCommandExecutor(InternetExplorerDriverService.CreateDefaultService(), commandTimeout);
                }
                catch (DriverServiceNotFoundException ex)
                {
                    throw new WebDriverException("You will need to use add InternetExplorerDriver.UseLegacyInternalServer to the desired capabilities to use the internal native code server library. This functionality will be deprecated in favor of the standalone InternetExplorerDriver.exe server.", ex);
                }
            }

            return executor;
        }