public ElementFactory(IRenderedFragment testTarget, TElement initialElement, string cssSelector) { _testTarget = testTarget; _cssSelector = cssSelector; _element = initialElement; testTarget.OnMarkupUpdated += FragmentsMarkupUpdated; }
/// <summary> /// Verifies that a diff is a change to a text node. /// </summary> /// <param name="actualChange">The diff to verify.</param> /// <param name="expectedChange">The rendered fragment containing the expected text change.</param> /// <param name="userMessage">A custom error message to show if the verification fails.</param> public static void ShouldBeTextChange(this IDiff actualChange, IRenderedFragment expectedChange, string?userMessage = null) { if (expectedChange is null) { throw new ArgumentNullException(nameof(expectedChange)); } ShouldBeTextChange(actualChange, expectedChange.Nodes, userMessage); }
//returns the specified control button private static IElement FindControlButton(IRenderedFragment comp, Page controlButton) { var buttons = comp.FindAll(".mud-pagination-item button"); var button = controlButton switch { Page.First => buttons[0], Page.Previous => buttons[1], Page.Next => buttons[^ 2],
/// <summary> /// Returns a refreshable collection of <see cref="IElement"/>s from the rendered fragment or component under test, /// using the provided <paramref name="cssSelector"/>, in a depth-first pre-order traversal /// of the rendered nodes. /// </summary> /// <param name="renderedFragment">The rendered fragment to search.</param> /// <param name="cssSelector">The group of selectors to use.</param> /// <param name="enableAutoRefresh">If true, the returned <see cref="IRefreshableElementCollection{IElement}"/> will automatically refresh its <see cref="IElement"/>s whenever the <paramref name="renderedFragment"/> changes.</param> /// <returns>An <see cref="IRefreshableElementCollection{IElement}"/>, that can be refreshed to execute the search again.</returns> public static IRefreshableElementCollection <IElement> FindAll(this IRenderedFragment renderedFragment, string cssSelector, bool enableAutoRefresh = false) { if (renderedFragment is null) { throw new ArgumentNullException(nameof(renderedFragment)); } return(new RefreshableElementCollection(renderedFragment, cssSelector) { EnableAutoRefresh = enableAutoRefresh }); }
public static ConversionResult Convert(IRenderedFragment fragment, VerifySettings settings) { var stream = new MemoryStream(); using var writer = stream.BuildLeaveOpenWriter(); writer.WriteLine(fragment.Markup); var instance = ComponentReader.GetInstance(fragment); return(new ConversionResult(new FragmentInfo(instance), stream)); }
/// <summary> /// Finds the first component of type <typeparamref name="TComponent"/> in the render tree of /// this <see cref="IRenderedFragment"/>. /// </summary> /// <typeparam name="TComponent">Type of component to find.</typeparam> /// <exception cref="ComponentNotFoundException">Thrown if a component of type <typeparamref name="TComponent"/> was not found in the render tree.</exception> /// <returns>The <see cref="IRenderedComponent{T}"/>.</returns> public static IRenderedComponent <TComponent> FindComponent <TComponent>(this IRenderedFragment renderedFragment) where TComponent : IComponent { if (renderedFragment is null) { throw new ArgumentNullException(nameof(renderedFragment)); } var renderer = renderedFragment.Services.GetRequiredService <ITestRenderer>(); var(id, component) = renderer.FindComponent <TComponent>(renderedFragment.ComponentId); return(new RenderedComponent <TComponent>(renderedFragment.Services, id, component)); }
/// <summary> /// Verifies that the <paramref name="actual"/> <see cref="INode"/> matches /// the rendered markup from the <paramref name="expected"/> <see cref="IRenderedFragment"/>, using the <see cref="HtmlComparer"/> /// type. /// </summary> /// <exception cref="HtmlEqualException">Thrown when the <paramref name="actual"/> markup does not match the <paramref name="expected"/> markup.</exception> /// <param name="actual">The node to verify.</param> /// <param name="expected">The expected rendered fragment.</param> /// <param name="userMessage">A custom user message to display in case the verification fails.</param> public static void MarkupMatches(this INode actual, IRenderedFragment expected, string?userMessage = null) { if (actual is null) { throw new ArgumentNullException(nameof(actual)); } if (expected is null) { throw new ArgumentNullException(nameof(expected)); } actual.MarkupMatches(expected.GetNodes(), userMessage); }
/// <summary> /// Compare the rendered markup in the <paramref name="actual"/> <see cref = "IRenderedFragmentBase" /> to /// the rendered markup in the <paramref name="expected"/> <see cref = "IRenderedFragmentBase" /> /// </summary> /// <param name="actual">Source of rendered markup to check.</param> /// <param name="expected">Source of rendered markup to compare with.</param> /// <returns>Any differences found.</returns> public static IReadOnlyList <IDiff> CompareTo(this IRenderedFragment actual, IRenderedFragment expected) { if (actual is null) { throw new ArgumentNullException(nameof(actual)); } if (expected is null) { throw new ArgumentNullException(nameof(expected)); } return(actual.Nodes.CompareTo(expected.Nodes)); }
public static ConversionResult Convert(IRenderedFragment fragment, IReadOnlyDictionary <string, object> context) { var markup = fragment.Markup.Replace("\r\n", "\n"); var instance = ComponentReader.GetInstance(fragment); var all = fragment.FindAll("*"); FragmentInfo info = new( instance, fragment.RenderCount, all.Count, markup.Length.ToString("N0")); return(new(info, "html", markup)); }
/// <summary> /// Returns the first element from the rendered fragment or component under test, /// using the provided <paramref name="cssSelector"/>, in a depth-first pre-order traversal /// of the rendered nodes. /// </summary> /// <param name="renderedFragment">The rendered fragment to search.</param> /// <param name="cssSelector">The group of selectors to use.</param> public static IElement Find(this IRenderedFragment renderedFragment, string cssSelector) { if (renderedFragment is null) { throw new ArgumentNullException(nameof(renderedFragment)); } var result = renderedFragment.Nodes.QuerySelector(cssSelector); if (result is null) { throw new ElementNotFoundException(cssSelector); } return(WrapperFactory.Create(new ElementFactory <IElement>(renderedFragment, result, cssSelector))); }
/// <summary> /// Compare the rendered markup in the <paramref name="actual"/> <see cref="IRenderedFragment"/> /// with that in the <paramref name="expected"/> markup string. /// </summary> /// <param name="actual">Source of rendered markup to check.</param> /// <param name="expected">Markup to compare with.</param> /// <returns>Any differences found.</returns> public static IReadOnlyList <IDiff> CompareTo(this IRenderedFragment actual, string expected) { if (actual is null) { throw new ArgumentNullException(nameof(actual)); } if (expected is null) { throw new ArgumentNullException(nameof(expected)); } var expectedNodes = actual.TestContext.CreateNodes(expected); return(actual.Nodes.CompareTo(expectedNodes)); }
/// <summary> /// Verifies that the rendered markup from the <paramref name="actual"/> <see cref="IRenderedFragment"/> matches /// the <paramref name="expected"/> markup, using the <see cref="HtmlComparer"/> type. /// </summary> /// <exception cref="HtmlEqualException">Thrown when the <paramref name="actual"/> markup does not match the <paramref name="expected"/> markup.</exception> /// <param name="actual">The rendered fragment to verify.</param> /// <param name="expected">The expected markup.</param> /// <param name="userMessage">A custom user message to display in case the verification fails.</param> public static void MarkupMatches(this IRenderedFragment actual, string expected, string?userMessage = null) { if (actual is null) { throw new ArgumentNullException(nameof(actual)); } if (expected is null) { throw new ArgumentNullException(nameof(expected)); } var expectedNodes = actual.TestContext.CreateNodes(expected); actual.Nodes.MarkupMatches(expectedNodes, userMessage); }
/// <summary> /// Compare the rendered markup in the <paramref name="actual"/> <see cref="IRenderedFragmentBase"/> /// with that in the <paramref name="expected"/> markup string. /// </summary> /// <param name="actual">Source of rendered markup to check.</param> /// <param name="expected">Markup to compare with.</param> /// <returns>Any differences found.</returns> public static IReadOnlyList <IDiff> CompareTo(this IRenderedFragment actual, string expected) { if (actual is null) { throw new ArgumentNullException(nameof(actual)); } if (expected is null) { throw new ArgumentNullException(nameof(expected)); } var htmlParser = actual.Services.GetRequiredService <HtmlParser>(); var expectedNodes = htmlParser.Parse(expected); return(actual.Nodes.CompareTo(expectedNodes)); }
/// <summary> /// Verifies that the rendered markup from the <paramref name="actual"/> <see cref="IRenderedFragmentBase"/> matches /// the <paramref name="expected"/> markup, using the <see cref="HtmlComparer"/> type. /// </summary> /// <exception cref="HtmlEqualException">Thrown when the <paramref name="actual"/> markup does not match the <paramref name="expected"/> markup.</exception> /// <param name="actual">The rendered fragment to verify.</param> /// <param name="expected">The expected markup.</param> /// <param name="userMessage">A custom user message to display in case the verification fails.</param> public static void MarkupMatches(this IRenderedFragment actual, string expected, string?userMessage = null) { if (actual is null) { throw new ArgumentNullException(nameof(actual)); } if (expected is null) { throw new ArgumentNullException(nameof(expected)); } var htmlParser = actual.Services.GetRequiredService <HtmlParser>(); var expectedNodes = htmlParser.Parse(expected); actual.Nodes.MarkupMatches(expectedNodes, userMessage); }
public static ConversionResult Convert(IRenderedFragment fragment, VerifySettings settings) { var stream = new MemoryStream(); using var writer = stream.BuildLeaveOpenWriter(); var markup = fragment.Markup; writer.WriteLine(markup); var instance = ComponentReader.GetInstance(fragment); var all = fragment.FindAll("*"); var info = new FragmentInfo( instance, fragment.RenderCount, all.Count, markup.Replace("\r\n", "\n").Length.ToString("N0")); return(new ConversionResult(info, new [] { new ConversionStream("html", stream) })); }
public static object?GetInstance(IRenderedFragment fragment) { var type = fragment.GetType(); if (!type.IsGenericType) { return(null); } var renderComponentInterface = type.GetInterfaces() .Where(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(IRenderedComponent <>)) .SingleOrDefault(); if (renderComponentInterface == null) { return(null); } var instanceProperty = renderComponentInterface.GetProperty("Instance"); return(instanceProperty.GetValue(fragment)); }
private IRenderedComponent <TComponent> TryCastTo <TComponent>(IRenderedFragment target, [System.Runtime.CompilerServices.CallerMemberName] string sourceMethod = "") where TComponent : class, IComponent { if (target is IRenderedComponent <TComponent> result) { return(result); } if (target is IRenderedComponent <IComponent> other) { throw new InvalidOperationException($"The generic version of {sourceMethod} has previously returned an object of type IRenderedComponent<{other.Instance.GetType().Name}>. " + $"That cannot be cast to an object of type IRenderedComponent<{typeof(TComponent).Name}>."); } if (target is IRenderedFragment) { throw new InvalidOperationException($"It is not possible to call the generic version of {sourceMethod} after " + $"the non-generic version has been called on the same test context. Change all calls to the same generic version and try again."); } else { throw new Exception($"This line should never have been reached. An unknown type was placed inside the {nameof(_renderedFragments)}."); } }
public ElementFactory(IRenderedFragment testTarget, TElement initialElement, string cssSelector) : base((testTarget ?? throw new ArgumentNullException(nameof(testTarget))).RenderEvents)
/// <summary> /// Finds all components of type <typeparamref name="TComponent"/> in the render tree of /// this <see cref="IRenderedFragment"/>. /// </summary> /// <typeparam name="TComponent">Type of components to find.</typeparam> /// <returns>The <see cref="IRenderedComponent{T}"/>s</returns> public static IReadOnlyList <IRenderedComponent <TComponent> > FindComponents <TComponent>(this IRenderedFragment renderedFragment) where TComponent : IComponent { if (renderedFragment is null) { throw new ArgumentNullException(nameof(renderedFragment)); } var renderer = renderedFragment.Services.GetRequiredService <ITestRenderer>(); var components = renderer.FindComponents <TComponent>(renderedFragment.ComponentId); var result = components.Count == 0 ? Array.Empty <IRenderedComponent <TComponent> >() : new IRenderedComponent <TComponent> [components.Count]; for (int i = 0; i < components.Count; i++) { result[i] = new RenderedComponent <TComponent>(renderedFragment.Services, components[i].ComponentId, components[i].Component); } return(result); }
/// <summary> /// Creates an instance of the <see cref="ComponentChangeEventSubscriber"/>. /// </summary> public ComponentChangeEventSubscriber(IRenderedFragment testTarget, Action <RenderEvent>?onChange = null, Action?onCompleted = null) : base((testTarget ?? throw new ArgumentNullException(nameof(testTarget))).RenderEvents, onChange, onCompleted)