示例#1
0
        public LifecycleWatcher(Frame frame, NavigationOptions options)
        {
            _options = options != null && options is NavigationOptions ? (WaitForNavigationOptions)options : new WaitForNavigationOptions();
            _frame   = frame;

            _expectedLifecycle = (_options.WaitUntil ?? _defaultWaitUntil).Select(w =>
            {
                string protocolEvent = _protocolLifecycle.GetValueOrDefault(w);

                if (protocolEvent == null)
                {
                    throw new PlaywrightSharpException($"Unknown value for options.waitUntil: {w}");
                }

                return(protocolEvent);
            });

            _timeout = frame.Page.DefaultNavigationTimeout;
            _frame   = frame;
            _sameDocumentNavigationTaskWrapper = new TaskCompletionSource <bool>(TaskCreationOptions.RunContinuationsAsynchronously);
            _lifecycleTaskWrapper             = new TaskCompletionSource <bool>(TaskCreationOptions.RunContinuationsAsynchronously);
            _newDocumentNavigationTaskWrapper = new TaskCompletionSource <bool>(TaskCreationOptions.RunContinuationsAsynchronously);
            _terminationTaskWrapper           = new TaskCompletionSource <bool>(TaskCreationOptions.RunContinuationsAsynchronously);

            _frame.Page.FrameManager.LifecycleWatchers.Add(this);

            CheckLifecycleComplete();
        }
示例#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);
            }
        }
        public LifecycleWatcher(Frame frame, NavigationOptions options, CancellationToken token = default)
        {
            _options           = options != null ? options as WaitForNavigationOptions ?? new WaitForNavigationOptions(options) : new WaitForNavigationOptions();
            _expectedLifecycle = _options.WaitUntil ?? _defaultWaitUntil;
            _timeout           = _options?.Timeout ?? frame.Page.DefaultNavigationTimeout;
            _frame             = frame;
            _sameDocumentNavigationTaskWrapper = new TaskCompletionSource <bool>(TaskCreationOptions.RunContinuationsAsynchronously);
            _lifecycleTaskWrapper             = new TaskCompletionSource <bool>(TaskCreationOptions.RunContinuationsAsynchronously);
            _newDocumentNavigationTaskWrapper = new TaskCompletionSource <bool>(TaskCreationOptions.RunContinuationsAsynchronously);
            _terminationTaskWrapper           = new TaskCompletionSource <bool>(TaskCreationOptions.RunContinuationsAsynchronously);

            _frame.Page.FrameManager.LifecycleWatchers.Add(this);
            _token = token;
            _frame.Page.ClientDisconnected += (sender, args) => _terminationTaskWrapper.TrySetException(new PlaywrightSharpException("Client disconnected"));
            CheckLifecycleComplete();
        }
示例#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);
            }
        }
示例#5
0
 internal WaitForNavigationOptions(NavigationOptions options = null)
 {
     Timeout   = options?.Timeout;
     WaitUntil = options?.WaitUntil;
 }
示例#6
0
 /// <inheritdoc />
 public Task SetContentAsync(string html, NavigationOptions options = null) => throw new NotImplementedException();
示例#7
0
 /// <inheritdoc />
 public Task WaitForLoadStateAsync(NavigationOptions options = null) => throw new NotImplementedException();
示例#8
0
 /// <inheritdoc />
 public Task <IResponse> ReloadAsync(NavigationOptions options = null) => throw new NotImplementedException();
示例#9
0
 /// <inheritdoc cref="IPage.GoForwardAsync(NavigationOptions)"/>
 public Task <IResponse> GoForwardAsync(NavigationOptions options = null)
 {
     throw new NotImplementedException();
 }