Provides a way to send commands to the remote server
        /// <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)
        {
            if (commandToExecute == null)
            {
                throw new ArgumentNullException("commandToExecute", "Command to execute cannot be null");
            }

            Response toReturn = null;
            if (commandToExecute.Name == DriverCommand.NewSession)
            {
                this.service.Start();
            }

            // 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.service.Dispose();
                }
            }

            return toReturn;
        }
Beispiel #2
0
        /// <summary>
        /// Creates a web request for your command
        /// </summary>
        /// <param name="baseUri">Uri that will have the command run against</param>
        /// <param name="commandToExecute">Command to execute</param>
        /// <returns>A web request of what has been run</returns>
        public HttpWebRequest CreateWebRequest(Uri baseUri, Command commandToExecute)
        {
            HttpWebRequest request = null;
            string[] urlParts = this.resourcePath.Split(new string[] { "/" }, StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < urlParts.Length; i++)
            {
                string urlPart = urlParts[i];
                if (urlPart.StartsWith("{", StringComparison.OrdinalIgnoreCase) && urlPart.EndsWith("}", StringComparison.OrdinalIgnoreCase))
                {
                    urlParts[i] = GetCommandPropertyValue(urlPart, commandToExecute);
                }
            }

            Uri fullUri;
            string relativeUrl = string.Join("/", urlParts);
            bool uriCreateSucceeded = Uri.TryCreate(baseUri, relativeUrl, out fullUri);
            if (uriCreateSucceeded)
            {
                request = HttpWebRequest.Create(fullUri) as HttpWebRequest;
                request.Method = this.method;
            }
            else
            {
                throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Unable to create URI from base {0} and relative path {1}", baseUri == null ? string.Empty : baseUri.ToString(), relativeUrl));
            }

            return request;
        }
        public Response Execute(Command commandToExecute)
        {
            Response result = null;
            if (commandToExecute.Name == DriverCommand.NewSession && this.Service != null)
            {
                this.Service.Start();
            }

            try
            {
                result = RealExecutor.Execute(commandToExecute);
                return result;
            }
            catch (Exception e)
            {
                if ((commandToExecute.Name == DriverCommand.NewSession) && (this.Service != null))
                {
                    this.Service.Dispose();
                }
                throw e;
            }
            finally
            {
                if (result != null && result.Status != WebDriverResult.Success &&
                    commandToExecute.Name == DriverCommand.NewSession && this.Service != null)
                {
                    this.Service.Dispose();
                }

                if (commandToExecute.Name == DriverCommand.Quit && this.Service != null)
                {
                    this.Service.Dispose();
                }
            }
        }
 /// <summary>
 /// Sends a command to the SafariDriver and waits for a response.
 /// </summary>
 /// <param name="command">The <see cref="Command"/> to send to the driver.</param>
 /// <returns>The <see cref="Response"/> from the command.</returns>
 public Response Send(Command command)
 {
     SafariCommand wrappedCommand = new SafariCommand(command);
     this.commandQueue.Enqueue(wrappedCommand);
     string commandJson = JsonConvert.SerializeObject(wrappedCommand);
     this.connection.Send(commandJson);
     return this.WaitForResponse(TimeSpan.FromSeconds(30));
 }
Beispiel #5
0
        private static Dictionary<string, object> ExtractParameters(Command command)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command", "command must not be null");
            }

            return command.Parameters;
        }
Beispiel #6
0
        private static string ExtractName(Command command)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command", "command must not be null");
            }

            return command.Name;
        }
Beispiel #7
0
        private static SessionId ExtractSessionId(Command command)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command", "command must not be null");
            }

            return command.SessionId;
        }
Beispiel #8
0
        public string ForwardCommand(Command commandToForward, bool verbose = true, int timeout = 0)
        {
            var serializedCommand = JsonConvert.SerializeObject(commandToForward);

            var response = this.SendRequest(serializedCommand, verbose, timeout);
            if (response.Key == HttpStatusCode.OK)
            {
                return response.Value;
            }

            throw new InnerDriverRequestException(response.Value, response.Key);
        }
        /// <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)
        {
            CommandInfo info = CommandInfoRepository.Instance.GetCommandInfo(commandToExecute.Name);
            HttpWebRequest request = info.CreateWebRequest(this.remoteServerUri, commandToExecute);
            request.Timeout = (int)this.serverResponseTimeout.TotalMilliseconds;
            request.Accept = RequestAcceptHeader;
            if (request.Method == CommandInfo.PostCommand)
            {
                string payload = commandToExecute.ParametersAsJsonString;
                byte[] data = Encoding.UTF8.GetBytes(payload);
                request.ContentType = JsonMimeType;
                System.IO.Stream requestStream = request.GetRequestStream();
                requestStream.Write(data, 0, data.Length);
                requestStream.Close();
            }

            return CreateResponse(request);
        }
        protected override string DoImpl()
        {
            // It is easier to reparse desired capabilities as JSON instead of re-mapping keys to attributes and calling type conversions, 
            // so we will take possible one time performance hit by serializing Dictionary and deserializing it as Capabilities object
            var serializedCapability =
                JsonConvert.SerializeObject(this.ExecutedCommand.Parameters["desiredCapabilities"]);
            this.Automator.ActualCapabilities = Capabilities.CapabilitiesFromJsonString(serializedCapability);

            var innerIp = this.InitializeApplication(this.Automator.ActualCapabilities.DebugConnectToRunningApp);

            this.Automator.CommandForwarder = new Requester(innerIp, this.Automator.ActualCapabilities.InnerPort);

            long timeout = this.Automator.ActualCapabilities.LaunchTimeout;

            const int PingStep = 500;
            var stopWatch = new Stopwatch();
            while (timeout > 0)
            {
                stopWatch.Restart();

                Logger.Trace("Ping inner driver");
                var pingCommand = new Command(null, "ping", null);
                var responseBody = this.Automator.CommandForwarder.ForwardCommand(pingCommand, false, 2000);
                if (responseBody.StartsWith("<pong>", StringComparison.Ordinal))
                {
                    break;
                }

                Thread.Sleep(PingStep);
                stopWatch.Stop();
                timeout -= stopWatch.ElapsedMilliseconds;
            }

            // TODO throw AutomationException with SessionNotCreatedException if timeout and uninstall the app
            Console.WriteLine();

            // Gives sometime to load visuals (needed only in case of slow emulation)
            Thread.Sleep(this.Automator.ActualCapabilities.LaunchDelay);

            var jsonResponse = this.JsonResponse(ResponseStatus.Success, this.Automator.ActualCapabilities);

            return jsonResponse;
        }
        /// <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)
        {
            CommandInfo info = CommandInfoRepository.Instance.GetCommandInfo(commandToExecute.Name);
            HttpWebRequest.DefaultMaximumErrorResponseLength = -1;
            HttpWebRequest request = info.CreateWebRequest(remoteServerUri, commandToExecute);
            request.Timeout = 15000;
            request.Accept = "application/json, image/png";
            if (request.Method == CommandInfo.PostCommand)
            {
                string payload = commandToExecute.ParametersAsJsonString;
                byte[] data = Encoding.UTF8.GetBytes(payload);
                request.ContentType = "application/json";
                System.IO.Stream requestStream = request.GetRequestStream();
                requestStream.Write(data, 0, data.Length);
                requestStream.Close();
            }

            return CreateResponse(request);
        }
Beispiel #12
0
        /// <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)
        {
            CommandInfo info = commandDictionary[commandToExecute.Name];
            HttpWebRequest.DefaultMaximumErrorResponseLength = -1;
            HttpWebRequest request = info.CreateWebRequest(remoteServerUri, commandToExecute);
            request.Accept = "application/json, image/png";
            string payload = JsonConvert.SerializeObject(commandToExecute.Parameters, new JsonConverter[] { new PlatformJsonConverter(), new CookieJsonConverter(), new CharArrayJsonConverter() });
            if (request.Method == CommandInfo.PostCommand)
            {
                byte[] data = Encoding.UTF8.GetBytes(payload);
                request.ContentType = "application/json";
                request.KeepAlive = false;
                System.IO.Stream requestStream = request.GetRequestStream();
                requestStream.Write(data, 0, data.Length);
                requestStream.Close();
            }

            return CreateResponse(request);
        }
        private void ConnectToApp()
        {
            long timeout = this.Automator.ActualCapabilities.LaunchTimeout;

            const int PingStep = 500;
            var stopWatch = new Stopwatch();
            while (timeout > 0)
            {
                stopWatch.Restart();

                Logger.Trace("Ping inner driver");
                var pingCommand = new Command(null, "ping", null);
                var responseBody = this.Automator.CommandForwarder.ForwardCommand(pingCommand, false, 2000);
                if (responseBody.StartsWith("<pong>", StringComparison.Ordinal))
                {
                    break;
                }

                Thread.Sleep(PingStep);
                stopWatch.Stop();
                timeout -= stopWatch.ElapsedMilliseconds;
            }
        }
        public Response Execute(Command commandToExecute)
        {
            if (commandToExecute == null)
            {
                throw new ArgumentNullException("commandToExecute", "Command may not be null");
            }

            if (commandToExecute.Name == DriverCommand.NewSession)
            {
                this.service.Start();
            }

            try
            {
                return this.internalExecutor.Execute(commandToExecute);
            }
            finally
            {
                if (commandToExecute.Name == DriverCommand.Quit)
                {
                    this.service.Dispose();
                }
            }
        }
        /// <summary>
        /// Executes a command with the ChromeDriver.
        /// </summary>
        /// <param name="commandToExecute">The command you wish to execute</param>
        /// <returns>A response from the browser</returns>
        public override Response Execute(Command commandToExecute)
        {
            Response toReturn = null;
            if (commandToExecute.Name == DriverCommand.NewSession)
            {
                this.service.Start();
            }

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

            return toReturn;
        }
 /// <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)
 {
     return this.connection.Send(commandToExecute);
 }
        /// <summary>
        /// Executes a command with this driver .
        /// </summary>
        /// <param name="driverCommandToExecute">A <see cref="DriverCommand"/> value representing the command to execute.</param>
        /// <param name="parameters">A <see cref="Dictionary{K, V}"/> containing the names and values of the parameters of the command.</param>
        /// <returns>A <see cref="Response"/> containing information about the success or failure of the command and any data returned by the command.</returns>
        protected virtual Response Execute(string driverCommandToExecute, Dictionary<string, object> parameters)
        {
            Command commandToExecute = new Command(this.sessionId, driverCommandToExecute, parameters);

            Response commandResponse = new Response();

            try
            {
                commandResponse = this.executor.Execute(commandToExecute);
            }
            catch (System.Net.WebException e)
            {
                commandResponse.Status = WebDriverResult.UnhandledError;
                commandResponse.Value = e;
            }

            if (commandResponse.Status != WebDriverResult.Success)
            {
                UnpackAndThrowOnError(commandResponse);
            }

            return commandResponse;
        }
 /// <summary>
 /// Sends a command to the server.
 /// </summary>
 /// <param name="commandToSend">The <see cref="Command"/> to send.</param>
 /// <returns>The command <see cref="Response"/>.</returns>
 public Response SendCommand(Command commandToSend)
 {
     return this.connection.Send(commandToSend);
 }
Beispiel #19
0
        /// <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("commandToExecute", "commandToExecute cannot be null");
            }

            CommandInfo info = CommandInfoRepository.Instance.GetCommandInfo(commandToExecute.Name);
            HttpWebRequest request = info.CreateWebRequest(this.remoteServerUri, commandToExecute);
            request.Timeout = (int)this.serverResponseTimeout.TotalMilliseconds;
            request.Accept = RequestAcceptHeader;
            request.KeepAlive = this.enableKeepAlive;
            request.ServicePoint.ConnectionLimit = 2000;
            if (request.Method == CommandInfo.PostCommand)
            {
                string payload = commandToExecute.ParametersAsJsonString;
                byte[] data = Encoding.UTF8.GetBytes(payload);
                request.ContentType = ContentTypeHeader;
                Stream requestStream = request.GetRequestStream();
                requestStream.Write(data, 0, data.Length);
                requestStream.Close();
            }

            return this.CreateResponse(request);
        }
Beispiel #20
0
        private void SendMessage(Command commandToExecute)
        {
            // Wait for a POST request to be pending from the Chrome extension.
            // When one is received, get the packet from the queue.
            bool signalReceived = postRequestReceived.WaitOne(TimeSpan.FromSeconds(ExtensionTimeoutInSeconds));

            if (pendingRequestQueue.Count == 0)
            {
                throw new FatalChromeException("No pending requests from the extension in the queue");
            }

            // ExtensionRequestPacket packet = pendingRequestQueue.Dequeue();
            ExtensionRequestPacket packet = pendingRequestQueue[0];
            pendingRequestQueue.RemoveAt(0);

            // Get the parameter names to correctly serialize the command to JSON.
            commandToExecute.Parameters.Add("request", commandNameMap[commandToExecute.Name]);
            string commandStringToSend = commandToExecute.ParametersAsJsonString;

            // Send the response to the Chrome extension.
            Send(packet, commandStringToSend, "application/json; charset=UTF-8");
        }
Beispiel #21
0
        /// <summary>
        /// Executes the Command in the browser
        /// </summary>
        /// <param name="commandToExecute">Command to execute</param>
        /// <returns>The response from the Browser</returns>
        public Response Execute(Command commandToExecute)
        {
            Response commandResponse = new Response(new SessionId("[No Session ID]"));
            if (commandToExecute.Name == DriverCommand.NewSession)
            {
                DesiredCapabilities capabilities = DesiredCapabilities.Chrome();
                Dictionary<string, object> capabilitiesMap = new Dictionary<string, object>();
                capabilitiesMap.Add("browserName", capabilities.BrowserName);
                capabilitiesMap.Add("version", capabilities.Version);
                capabilitiesMap.Add("platform", capabilities.Platform.Type.ToString());
                capabilitiesMap.Add("javascriptEnabled", true);
                commandResponse.Value = capabilitiesMap;
            }
            else
            {
                SendMessage(commandToExecute);
                commandResponse = HandleResponse();
            }

            return commandResponse;
        }
Beispiel #22
0
        private static string GetCommandPropertyValue(string propertyName, Command commandToExecute)
        {
            string propertyValue = string.Empty;

            // Strip the curly braces
            propertyName = propertyName.Substring(1, propertyName.Length - 2);

            if (propertyName == SessionIdPropertyName)
            {
                if (commandToExecute.SessionId != null)
                {
                    propertyValue = commandToExecute.SessionId.ToString();
                }
            }
            else if (commandToExecute.Parameters != null && commandToExecute.Parameters.Count > 0)
            {
                // Extract the URL parameter, and remove it from the parameters dictionary
                // so it doesn't get transmitted as a JSON parameter.
                if (commandToExecute.Parameters.ContainsKey(propertyName))
                {
                    if (commandToExecute.Parameters[propertyName] != null)
                    {
                        propertyValue = commandToExecute.Parameters[propertyName].ToString();
                        commandToExecute.Parameters.Remove(propertyName);
                    }
                }
            }

            return propertyValue;
        }
Beispiel #23
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SafariCommand"/> class.
 /// </summary>
 /// <param name="command">The <see cref="Command"/> object used as a base for this <see cref="SafariCommand"/>.</param>
 public SafariCommand(Command command)
     : base(ExtractSessionId(command), ExtractName(command), ExtractParameters(command))
 {
     this.id = Guid.NewGuid();
 }
Beispiel #24
0
        private CommandResponse ProcessCommand(Command command)
        {
            Logger.Info("COMMAND {0}\r\n{1}", command.Name, command.ParametersAsJsonString);
            var executor = this.executorDispatcher.GetExecutor(command.Name);
            executor.ExecutedCommand = command;
            var response = executor.Do();
            Logger.Debug("RESPONSE:\r\n{0}", response);

            return response;
        }
Beispiel #25
0
        private string HandleRequest(HttpRequest acceptedRequest)
        {
            if (string.IsNullOrEmpty(acceptedRequest.StartingLine))
            {
                return HttpResponseHelper.ResponseString(
                    HttpStatusCode.BadRequest,
                    "Bad request URL");
            }

            var firstHeaderTokens = acceptedRequest.StartingLine.Split(' ');
            var method = firstHeaderTokens[0];
            var resourcePath = firstHeaderTokens[1];

            var uriToMatch = new Uri(this.Prefix, resourcePath);
            var matched = this.dispatcher.Match(method, uriToMatch);

            if (matched == null)
            {
                Logger.Warn("Unknown command recived: {0}", uriToMatch);
                return HttpResponseHelper.ResponseString(HttpStatusCode.NotFound, "Unknown command " + uriToMatch);
            }

            var commandName = matched.Data.ToString();
            var commandToExecute = new Command(commandName, acceptedRequest.MessageBody);
            foreach (string variableName in matched.BoundVariables.Keys)
            {
                commandToExecute.Parameters[variableName] = matched.BoundVariables[variableName];
            }

            var commandResponse = this.ProcessCommand(commandToExecute);
            return HttpResponseHelper.ResponseString(commandResponse.HttpStatusCode, commandResponse.Content);
        }
Beispiel #26
0
        /// <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("commandToExecute", "commandToExecute cannot be null");
            }

            CommandInfo info = this.commandInfoRepository.GetCommandInfo(commandToExecute.Name);
            HttpWebRequest request = info.CreateWebRequest(this.remoteServerUri, commandToExecute);
            request.Timeout = (int)this.serverResponseTimeout.TotalMilliseconds;
            request.Accept = RequestAcceptHeader;
            request.KeepAlive = this.enableKeepAlive;
            request.ServicePoint.ConnectionLimit = 2000;
            if (request.Method == CommandInfo.PostCommand)
            {
                string payload = commandToExecute.ParametersAsJsonString;
                byte[] data = Encoding.UTF8.GetBytes(payload);
                request.ContentType = ContentTypeHeader;
                Stream requestStream = request.GetRequestStream();
                requestStream.Write(data, 0, data.Length);
                requestStream.Close();
            }

            Response toReturn = this.CreateResponse(request);
            if (commandToExecute.Name == DriverCommand.NewSession)
            {
                // If we are creating a new session, sniff the response to determine
                // what protocol level we are using. If the response contains a
                // capability called "specificationLevel" that's an integer value
                // and that's greater than 0, that means we're using the W3C protocol
                // dialect.
                // TODO(jimevans): Reverse this test to make it the default path when
                // most remote ends speak W3C, then remove it entirely when legacy
                // protocol is phased out.
                Dictionary<string, object> capabilities = toReturn.Value as Dictionary<string, object>;
                if (capabilities != null)
                {
                    if (capabilities.ContainsKey("specificationLevel"))
                    {
                        int returnedSpecLevel = Convert.ToInt32(capabilities["specificationLevel"]);
                        if (returnedSpecLevel > 0)
                        {
                            this.commandInfoRepository = new W3CWireProtocolCommandInfoRepository();
                        }
                    }
                }
            }

            return toReturn;
        }
Beispiel #27
0
            private static string GetCommandPropertyValue(string propertyName, Command commandToExecute)
            {
                string propertyValue = string.Empty;

                // Strip the leading colon
                propertyName = propertyName.Substring(1);

                if (propertyName == "sessionId")
                {
                    if (commandToExecute.SessionId != null)
                    {
                        propertyValue = commandToExecute.SessionId.ToString();
                    }
                }
                else if (propertyName == "context")
                {
                    if (commandToExecute.Context != null)
                    {
                        propertyValue = commandToExecute.Context.ToString();
                    }
                }
                else if (commandToExecute.Parameters != null && commandToExecute.Parameters.Length > 0)
                {
                    Dictionary<string, object> parameterDictionary = commandToExecute.Parameters[0] as Dictionary<string, object>;
                    if (parameterDictionary != null && parameterDictionary.ContainsKey(propertyName))
                    {
                        if (parameterDictionary[propertyName] != null)
                        {
                            propertyValue = parameterDictionary[propertyName].ToString();
                        }
                    }
                }

                return propertyValue;
            }
Beispiel #28
0
 /// <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)
 {
     return executor.Execute(commandToExecute);
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="SafariCommand"/> class.
 /// </summary>
 /// <param name="command">The <see cref="Command"/> object used as a base for this <see cref="SafariCommand"/>.</param>
 public SafariCommand(Command command)
     : base(command.SessionId, command.Name, command.Parameters)
 {
     this.id = Guid.NewGuid();
 }
        /// <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("commandToExecute", "commandToExecute cannot be null");
            }

            CommandInfo info = this.commandInfoRepository.GetCommandInfo(commandToExecute.Name);
            HttpWebRequest request = info.CreateWebRequest(this.remoteServerUri, commandToExecute);
            request.Timeout = (int)this.serverResponseTimeout.TotalMilliseconds;
            request.Accept = RequestAcceptHeader;
            request.KeepAlive = this.enableKeepAlive;
            request.ServicePoint.ConnectionLimit = 2000;
            if (request.Method == CommandInfo.PostCommand)
            {
                string payload = commandToExecute.ParametersAsJsonString;
                byte[] data = Encoding.UTF8.GetBytes(payload);
                request.ContentType = ContentTypeHeader;
                Stream requestStream = request.GetRequestStream();
                requestStream.Write(data, 0, data.Length);
                requestStream.Close();
            }

            Response toReturn = this.CreateResponse(request);
            if (commandToExecute.Name == DriverCommand.NewSession && toReturn.IsSpecificationCompliant)
            {
                // If we are creating a new session, sniff the response to determine
                // what protocol level we are using. If the response contains a
                // field called "status", it's not a spec-compliant response.
                // Each response is polled for this, and sets a property describing
                // whether it's using the W3C protocol dialect.
                // TODO(jimevans): Reverse this test to make it the default path when
                // most remote ends speak W3C, then remove it entirely when legacy
                // protocol is phased out.
                this.commandInfoRepository = new W3CWireProtocolCommandInfoRepository();
            }

            return toReturn;
        }