コード例 #1
0
        public static void WriteResponse(ChromeResponse response)
        {
            string value = "null";
            if (response.Value != null)
                value = response.Value.ToString();
            string formattedMessage = string.Format("({0}) Response: Status Code: {1} - Value: ( {2} )\n", 
                DateTime.Now.ToString(), response.StatusCode.ToString(), value);
            
#if DEBUG
            //Console.WriteLine(formattedMessage);
#endif

            Debug.WriteLine(formattedMessage);
        }
コード例 #2
0
        public ChromeResponse HandleResponse(ChromeCommand command, bool printConsoleMessages)
        {
            // Wait for a POST request to be pending from the Chrome extension.
            // Note that we need to leave the packet in the queue for the next
            // send message.

            DateTime timeout = DateTime.Now.AddSeconds(PacketTimeoutSeconds);
            browser.DialogInterrupted = false;

            using (DialogWatcher watcher = new DialogWatcher(browser))
            {
                while (DateTime.Now < timeout)
                {
                    lock (responseLock)
                    {
                        if (watcher.FoundDialog)
                        {
                            browser.DialogInterrupted = true;
                            break;
                        }

                        if (receivedResponse)
                            break;
                    }
                }
            }

            ChromeResponse response = new ChromeResponse();

            if (browser.DialogInterrupted)
            {
                response.StatusCode = StatusCode.SUCCESS;
                response.Value = "A JSDialog caused a timeout.";
                return response;
            }

            if (!receivedResponse)
            {
                throw new ChromeCommandTimedOutException(command.Command, PacketTimeoutSeconds);
            }

            // ExtensionRequestPacket packet = pendingRequestQueue.Peek();
            // If the page cannot be found, this gets index out of bounds
            ExtensionRequestPacket packet = pendingRequestQueue[0];

            // Parse the packet content, and deserialize from a JSON object.
            string responseString = ParseResponse(packet.Content);

            if (!string.IsNullOrEmpty(responseString))
            {
                response = JsonConvert.DeserializeObject<ChromeResponse>(responseString);

                if (response == null)
                    throw new ChromeException("No response was returned by the extension.");

                response.Value = response.Value ?? "";
            }

            string valueAsString = response.Value as string;
            if (valueAsString != null)
            {
                // First, collapse all \r\n pairs to \n, then replace all \n with
                // System.Environment.NewLine. This ensures the consistency of 
                // the values.
                response.Value = valueAsString.Replace("\r\n", "\n").Replace("\n", System.Environment.NewLine);
            }
            //Utilities.WriteToConsole(string.Format("Response was {0}", response.ToString()));

            if (printConsoleMessages)
            {
                Utilities.WriteResponse(response);
            }

            switch (response.StatusCode)
            {
                case StatusCode.BADCOMMAND:
                case StatusCode.BADJAVASCRIPT:
                case StatusCode.CONTENTSCRIPTCONNECTFAIL:
                case StatusCode.CONTENTSCRIPTERROR:
                case StatusCode.ELEMENTNOTVISIBLE:
                case StatusCode.INVALIDELEMENTSTATE:
                case StatusCode.NOSUCHFRAME:
                case StatusCode.STALEELEMENTREFERENCE:
                case StatusCode.UNHANDLEDERROR:
                    throw new ChromeException(response.Value.ToString());
            }

            return response;
        }