/// <summary> /// Waits until the document is fully available. This includes /// that the document is completed and that a stable point has /// been reached. /// </summary> /// <param name="document">The document to await.</param> /// <param name="cancellation">The timeout cancellation, if any.</param> /// <returns>A task that is finished when the document is available.</returns> public static async Task WaitUntilAvailable(this IDocument document, CancellationToken cancellation = default) { if (document.ReadyState != DocumentReadyState.Complete) { var abort = new TaskCompletionSource <object>(); cancellation.Register(() => abort.TrySetCanceled()); await Task.WhenAny(document.AwaitEventAsync("load"), abort.Task).ConfigureAwait(false); } await document.WhenStable().ConfigureAwait(false); }
public void EventsAwaitedTriggered() { var evName = "click"; var element = document.QuerySelector("img"); var ev = document.CreateEvent("event"); ev.Init(evName, true, true); var task = document.AwaitEventAsync(evName); Assert.IsFalse(task.IsCompleted); document.Dispatch(ev); Assert.IsTrue(task.IsCompleted); Assert.IsFalse(task.IsFaulted); Assert.AreEqual(evName, task.Result.Type); }