// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; using System.Collections; using System.Collections.Generic; using System.Collections.ObjectModel; using OpenQA.Selenium; using OpenQA.Selenium.Support.UI; using Xunit; namespace Microsoft.AspNetCore.Blazor.E2ETest.Infrastructure { // XUnit assertions, but hooked into Selenium's polling mechanism public class WaitAssert { private readonly static TimeSpan DefaultTimeout = TimeSpan.FromSeconds(1); public static void Equal(T expected, Func actual) => WaitAssertCore(() => Assert.Equal(expected, actual())); public static void True(Func actual) => WaitAssertCore(() => Assert.True(actual())); public static void False(Func actual) => WaitAssertCore(() => Assert.False(actual())); public static void Contains(string expectedSubstring, Func actualString) => WaitAssertCore(() => Assert.Contains(expectedSubstring, actualString())); public static void Collection(Func> actualValues, params Action[] elementInspectors) => WaitAssertCore(() => Assert.Collection(actualValues(), elementInspectors)); public static void Empty(Func actualValues) => WaitAssertCore(() => Assert.Empty(actualValues())); public static void Single(Func actualValues) => WaitAssertCore(() => Assert.Single(actualValues())); private static void WaitAssertCore(Action assertion, TimeSpan timeout = default) { if (timeout == default) { timeout = DefaultTimeout; } try { new WebDriverWait(BrowserTestBase.Browser, timeout).Until(_ => { try { assertion(); return true; } catch { return false; } }); } catch (WebDriverTimeoutException) { // Instead of reporting it as a timeout, report the Xunit exception assertion(); } } } }