コード例 #1
0
        /// <summary>
        ///     Wait until the javascript window.status is returning the given <paramref name="status" />
        /// </summary>
        /// <param name="status">The case insensitive status</param>
        /// <param name="timeout">Continue after reaching the set timeout in milliseconds</param>
        /// <returns><c>true</c> when window status matched, <c>false</c> when timing out</returns>
        /// <exception cref="ChromeException">Raised when an error is returned by Chrome</exception>
        public bool WaitForWindowStatus(string status, int timeout = 60000)
        {
            var message = new Message {
                Method = "Runtime.evaluate"
            };

            message.AddParameter("expression", "window.status;");
            message.AddParameter("silent", true);
            message.AddParameter("returnByValue", true);

            var waitEvent = new ManualResetEvent(false);
            var match     = false;

            void MessageReceived(object sender, string data)
            {
                var evaluate = Evaluate.FromJson(data);

                if (evaluate.Result?.Result?.Value != status)
                {
                    return;
                }
                match = true;
                waitEvent.Set();
            }

            _pageConnection.MessageReceived += MessageReceived;

            var stopWatch = new Stopwatch();

            stopWatch.Start();

            while (!match)
            {
                _pageConnection.SendAsync(message).GetAwaiter();
                waitEvent.WaitOne(10);
                if (stopWatch.ElapsedMilliseconds >= timeout)
                {
                    break;
                }
            }

            stopWatch.Stop();
            _pageConnection.MessageReceived -= MessageReceived;

            return(match);
        }
コード例 #2
0
        /// <summary>
        /// Wait until the javascript window.status is returning the given <paramref name="status"/>
        /// </summary>
        /// <param name="status">The case insensitive status</param>
        /// <param name="timeout">Continue after reaching the set timeout in milliseconds</param>
        /// <returns><c>true</c> when window status matched, <c>false</c> when timing out</returns>
        /// <exception cref="ChromeException">Raised when an error is returned by Chrome</exception>
        public bool WaitForWindowStatus(string status, int timeout = 60000)
        {
            var message = new Message
            {
                Id     = MessageId,
                Method = "Runtime.evaluate"
            };

            message.AddParameter("expression", "window.status;");
            message.AddParameter("silent", true);
            message.AddParameter("returnByValue", true);

            var match = false;

            _webSocket.MessageReceived += (sender, args) =>
            {
                var evaluate = Evaluate.FromJson(args.Message);
                if (evaluate.Result?.Result?.Value == status)
                {
                    match = true;
                }
            };

            var stopWatch = new Stopwatch();

            stopWatch.Start();

            while (!match)
            {
                WebSocketSend(message.ToJson());
                Thread.Sleep(10);
                if (stopWatch.ElapsedMilliseconds >= timeout)
                {
                    break;
                }
            }

            stopWatch.Stop();

            return(match);
        }