/// <summary> /// Wait until the provided <paramref name="assertion"/> passes (i.e. does not throw an /// exception), or the <paramref name="timeout"/> is reached (default is one second). /// /// The <paramref name="assertion"/> is attempted initially, and then each time the <paramref name="renderedFragment"/> renders. /// </summary> /// <param name="renderedFragment">The rendered fragment to wait for renders from and assert against.</param> /// <param name="assertion">The verification or assertion to perform.</param> /// <param name="timeout">The maximum time to attempt the verification.</param> /// <exception cref="WaitForFailedException">Thrown if the timeout has been reached. See the inner exception to see the captured assertion exception.</exception> public static void WaitForAssertion(this IRenderedFragmentBase renderedFragment, Action assertion, TimeSpan?timeout = null) { using var waiter = new WaitForAssertionHelper(renderedFragment, assertion, timeout); try { waiter.WaitTask.Wait(); } catch (AggregateException e) { throw e.InnerException; } }
/// <summary> /// Wait until the provided <paramref name="statePredicate"/> action returns true, /// or the <paramref name="timeout"/> is reached (default is one second). /// /// The <paramref name="statePredicate"/> is evaluated initially, and then each time /// the <paramref name="renderedFragment"/> renders. /// </summary> /// <param name="renderedFragment">The render fragment or component to attempt to verify state against.</param> /// <param name="statePredicate">The predicate to invoke after each render, which must returns <c>true</c> when the desired state has been reached.</param> /// <param name="timeout">The maximum time to wait for the desired state.</param> /// <exception cref="WaitForFailedException">Thrown if the <paramref name="statePredicate"/> throw an exception during invocation, or if the timeout has been reached. See the inner exception for details.</exception> public static void WaitForState(this IRenderedFragmentBase renderedFragment, Func <bool> statePredicate, TimeSpan?timeout = null) { using var waiter = new WaitForStateHelper(renderedFragment, statePredicate, timeout); try { waiter.WaitTask.Wait(); } catch (AggregateException e) { throw e.InnerException; } }
/// <summary> /// Creates an instance of the <see cref="WaitForHelper"/> type. /// </summary> protected WaitForHelper(IRenderedFragmentBase renderedFragment, Func <bool> completeChecker, TimeSpan?timeout = null) { _renderedFragment = renderedFragment ?? throw new ArgumentNullException(nameof(renderedFragment)); _completeChecker = completeChecker ?? throw new ArgumentNullException(nameof(completeChecker)); _logger = renderedFragment.Services.CreateLogger <WaitForHelper>(); _completionSouce = new TaskCompletionSource <object?>(); _timer = new Timer(OnTimeout, this, Timeout.InfiniteTimeSpan, Timeout.InfiniteTimeSpan); _renderedFragment.OnAfterRender += OnAfterRender; OnAfterRender(); StartTimer(timeout); }
/// <summary> /// Creates an instance of the <see cref="WaitForStateHelper"/> type, /// which will wait until the provided <paramref name="statePredicate"/> action returns true, /// or the <paramref name="timeout"/> is reached (default is one second). /// /// The <paramref name="statePredicate"/> is evaluated initially, and then each time the <paramref name="renderedFragment"/> renders. /// </summary> /// <param name="renderedFragment">The render fragment or component to attempt to verify state against.</param> /// <param name="statePredicate">The predicate to invoke after each render, which must returns <c>true</c> when the desired state has been reached.</param> /// <param name="timeout">The maximum time to wait for the desired state.</param> /// <exception cref="WaitForFailedException">Thrown if the <paramref name="statePredicate"/> throw an exception during invocation, or if the timeout has been reached. See the inner exception for details.</exception> public WaitForStateHelper(IRenderedFragmentBase renderedFragment, Func <bool> statePredicate, TimeSpan?timeout = null) : base(renderedFragment, statePredicate, timeout) { }
/// <summary> /// Creates an instance of the <see cref="WaitForAssertionHelper"/> type, /// which will until the provided <paramref name="assertion"/> passes (i.e. does not throw an /// exception), or the <paramref name="timeout"/> is reached (default is one second). /// /// The <paramref name="assertion"/> is attempted initially, and then each time the <paramref name="renderedFragment"/> renders. /// </summary> /// <param name="renderedFragment">The rendered fragment to wait for renders from and assert against.</param> /// <param name="assertion">The verification or assertion to perform.</param> /// <param name="timeout">The maximum time to attempt the verification.</param> public WaitForAssertionHelper(IRenderedFragmentBase renderedFragment, Action assertion, TimeSpan?timeout = null) : base(renderedFragment, () => { assertion(); return(true); }, timeout) { }