Пример #1
0
        /// <inheritdoc cref="IFrame.GoToAsync(string, GoToOptions)"/>
        public async Task <IResponse> GoToAsync(string url, GoToOptions options = null)
        {
            Page.PageState.ExtraHTTPHeaders.TryGetValue("referer", out string referer);

            if (options?.Referer != null)
            {
                if (referer != null && referer != options.Referer)
                {
                    throw new ArgumentException("\"referer\" is already specified as extra HTTP header");
                }

                referer = options.Referer;
            }

            using var watcher = new LifecycleWatcher(this, options);

            try
            {
                var navigateTask = Page.Delegate.NavigateFrameAsync(this, url, referer);
                var task         = await Task.WhenAny(
                    watcher.TimeoutOrTerminationTask,
                    navigateTask).ConfigureAwait(false);

                await task.ConfigureAwait(false);

                var tasks = new List <Task>()
                {
                    watcher.TimeoutOrTerminationTask
                };
                if (!string.IsNullOrEmpty(navigateTask.Result.NewDocumentId))
                {
                    watcher.SetExpectedDocumentId(navigateTask.Result.NewDocumentId, url);
                    tasks.Add(watcher.NewDocumentNavigationTask);
                }
                else if (navigateTask.Result.IsSameDocument)
                {
                    tasks.Add(watcher.SameDocumentNavigationTask);
                }
                else
                {
                    tasks.AddRange(new[] { watcher.SameDocumentNavigationTask, watcher.NewDocumentNavigationTask });
                }

                task = await Task.WhenAny(tasks).ConfigureAwait(false);

                await task.ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                throw new NavigationException(ex.Message, ex);
            }

            return(watcher.NavigationResponse);
        }
Пример #2
0
        /// <inheritdoc cref="IFrame.WaitForLoadStateAsync(NavigationOptions)"/>
        public async Task WaitForLoadStateAsync(NavigationOptions options = null)
        {
            using var watcher = new LifecycleWatcher(this, options);
            var errorTask = watcher.TimeoutOrTerminationTask;

            await Task.WhenAny(
                errorTask,
                watcher.LifecycleTask).ConfigureAwait(false);

            if (errorTask.IsCompleted)
            {
                await errorTask.ConfigureAwait(false);
            }
        }
Пример #3
0
        /// <inheritdoc cref="IFrame.WaitForNavigationAsync(WaitForNavigationOptions, CancellationToken)"/>
        public async Task <IResponse> WaitForNavigationAsync(WaitForNavigationOptions options = null, CancellationToken token = default)
        {
            using var watcher = new LifecycleWatcher(this, options);
            var errorTask = watcher.TimeoutOrTerminationTask;

            await Task.WhenAny(
                errorTask,
                watcher.SameDocumentNavigationTask,
                watcher.NewDocumentNavigationTask).ConfigureAwait(false);

            if (errorTask.IsCompleted)
            {
                await errorTask.ConfigureAwait(false);
            }

            return(await watcher.NavigationResponseTask.ConfigureAwait(false));
        }
Пример #4
0
        /// <inheritdoc cref="IFrame.SetContentAsync(string, NavigationOptions)"/>
        public async Task SetContentAsync(string html, NavigationOptions options = null)
        {
            string tag     = $"--playwright--set--content--{Id}--{++_setContentCounter}--";
            var    context = await GetUtilityContextAsync().ConfigureAwait(false);

            LifecycleWatcher watcher = null;

            Page.FrameManager.ConsoleMessageTags.TryAdd(tag, () =>
            {
                // Clear lifecycle right after document.open() - see 'tag' below.
                Page.FrameManager.ClearFrameLifecycle(this);
                watcher = new LifecycleWatcher(this, options);
            });

            await context.EvaluateAsync(
                @"(html, tag) => {
                    window.stop();
                    document.open();
                    console.debug(tag);
                    document.write(html);
                    document.close();
                }",
                html,
                tag).ConfigureAwait(false);

            if (watcher == null)
            {
                throw new PlaywrightSharpException("Was not able to clear lifecycle in SetContentAsync");
            }

            var timeoutTask = watcher.TimeoutOrTerminationTask;
            await Task.WhenAny(
                timeoutTask,
                watcher.LifecycleTask).ConfigureAwait(false);

            watcher.Dispose();
            if (timeoutTask.IsFaulted)
            {
                await timeoutTask.ConfigureAwait(false);
            }
        }