示例#1
0
        public static async Task PuppeteerWaitForResponse(BotData data, string url, int timeoutMilliseconds = 60000)
        {
            data.Logger.LogHeader();

            var page    = GetPage(data);
            var options = new WaitForOptions
            {
                Timeout = timeoutMilliseconds
            };

            var response = await page.WaitForResponseAsync(url, options);

            data.ADDRESS      = response.Url;
            data.RESPONSECODE = (int)response.Status;
            data.HEADERS      = response.Headers;

            // On 3xx puppeteer returns a body missing exception
            if (((int)response.Status) / 100 != 3)
            {
                data.SOURCE = await response.TextAsync();

                data.RAWSOURCE = await response.BufferAsync();
            }

            data.Logger.Log($"Address: {data.ADDRESS}", LogColors.DodgerBlue);
            data.Logger.Log($"Response code: {data.RESPONSECODE}", LogColors.Citrine);

            data.Logger.Log("Received Headers:", LogColors.MediumPurple);
            data.Logger.Log(data.HEADERS.Select(h => $"{h.Key}: {h.Value}"), LogColors.Violet);

            data.Logger.Log("Received Payload:", LogColors.ForestGreen);
            data.Logger.Log(data.SOURCE, LogColors.GreenYellow, true);
        }
        /// <inheritdoc cref="IBrowser.WaitForTargetAsync(Func{ITarget, bool}, WaitForOptions)"/>
        public Task <ITarget> WaitForTargetAsync(Func <ITarget, bool> predicate, WaitForOptions options = null)
        {
            int timeout        = options?.Timeout ?? DefaultWaitForTimeout;
            var existingTarget = GetAllTargets().FirstOrDefault(predicate);

            if (existingTarget != null)
            {
                return(Task.FromResult(existingTarget));
            }

            var tsc = new TaskCompletionSource <ITarget>();

            void TargetChangedHandler(object sender, TargetChangedArgs e)
            {
                tsc.TrySetResult(e.Target);
                TargetChanged -= TargetChangedHandler;
            }

            TargetChanged += TargetChangedHandler;
            return(tsc.Task.WithTimeout(timeout));
        }
        /// <inheritdoc cref="IBrowser.WaitForTargetAsync"/>
        public async Task <ITarget> WaitForTargetAsync(Func <ITarget, bool> predicate, WaitForOptions options = null)
        {
            int timeout        = options?.Timeout ?? DefaultWaitForTimeout;
            var existingTarget = GetAllTargets().FirstOrDefault(predicate);

            if (existingTarget != null)
            {
                return(existingTarget);
            }

            var targetCompletionSource = new TaskCompletionSource <ITarget>(TaskCreationOptions.RunContinuationsAsynchronously);

            void TargetHandler(object sender, TargetChangedArgs e)
            {
                if (predicate(e.Target))
                {
                    targetCompletionSource.TrySetResult(e.Target);
                }
            }

            try
            {
                TargetCreated += TargetHandler;
                TargetChanged += TargetHandler;

                return(await targetCompletionSource.Task.WithTimeout(timeout).ConfigureAwait(false));
            }
            finally
            {
                TargetCreated -= TargetHandler;
                TargetChanged -= TargetHandler;
            }
        }
示例#4
0
 public Task <Target> WaitForTargetAsync(Func <Target, bool> predicate, WaitForOptions options = null)
 {
     return(_browser.WaitForTargetAsync(predicate, options));
 }