/// <summary> /// Verifies that a specific element in a HTML document or fragment are equivalent to a given fragment using the provided options. /// </summary> /// <param name="expected">The expected HTML fragment element</param> /// <param name="html">The HTML document or fragment from which the element to compare should be selected from.</param> /// <param name="selector">A selector used to find the element to compare.</param> /// <param name="elementComparisonMode">Indicated if the selected element itself should be included in the comparison or if only it's content should be compared.</param> /// <param name="elementSelectionMode">Indicates how selected elements should be compared.</param> /// <param name="ignoreAdditionalAttributes">Indicates if additional attribute on any element in the candidate HTML should be ignored.</param> /// <param name="ignoreAdditionalClassNames">Indicates if additional class names on any element in the candidate HTML should be ignored.</param> /// <param name="ignoreClassNameOrder">Indicates if the order of class names in the candidate HTML should be ignored</param> /// <exception cref="HtmlException">Thrown when the HTML elements are not equivalent</exception> public static void HtmlElement(string expected, string html, string selector, ElementComparisonMode elementComparisonMode = ElementComparisonMode.InnerHtml, ElementSelectionMode elementSelectionMode = ElementSelectionMode.First, bool ignoreAdditionalAttributes = false, bool ignoreAdditionalClassNames = false, bool ignoreClassNameOrder = true) { GuardArgumentNotNull(nameof(expected), expected); GuardArgumentNotNull(nameof(selector), selector); var comparerOptions = new HtmlCompareOptions { ElementComparisonMode = elementComparisonMode, ElementSelectionMode = elementSelectionMode, IgnoreAdditionalAttributes = ignoreAdditionalAttributes, IgnoreAdditionalClassNames = ignoreAdditionalClassNames, IgnoreClassNameOrder = ignoreClassNameOrder, TreatHtmlAsFragment = true }; var result = new HtmlComparer(comparerOptions).Equals(expected, html, selector); if (!result.Matches) { throw new HtmlException(result, selector); } }
/// <summary> /// Verifies that a specific element in a HTML document returned in a <see cref="HttpResponseMessage"/> is equivalent to a given fragment using the provided options. /// </summary> /// <param name="expected">The expected HTML fragment element</param> /// <param name="response">The <see cref="HttpResponseMessage"/> containing the HTML to be compared against</param> /// <param name="selector">A selector used to find the element to compare.</param> /// <param name="elementComparisonMode">Indicated if the selected element itself should be included in the comparison or if only it's content should be compared.</param> /// <param name="elementSelectionMode">Indicates how selected elements should be compared.</param> /// <param name="ignoreAdditionalAttributes">Indicates if additional attribute on any element in the candidate HTML should be ignored.</param> /// <param name="ignoreAdditionalClassNames">Indicates if additional class names on any element in the candidate HTML should be ignored.</param> /// <param name="ignoreClassNameOrder">Indicates if the order of class names in the candidate HTML should be ignored</param> /// <exception cref="HtmlException">Thrown when the HTML elements are not equivalent</exception> /// <exception cref="HttpRequestException">Thrown when the response is unsuccessful</exception> public static async Task HtmlElement(string expected, HttpResponseMessage response, string selector, ElementComparisonMode elementComparisonMode = ElementComparisonMode.InnerHtml, ElementSelectionMode elementSelectionMode = ElementSelectionMode.First, bool ignoreAdditionalAttributes = false, bool ignoreAdditionalClassNames = false, bool ignoreClassNameOrder = true) { GuardArgumentNotNull(nameof(expected), expected); GuardArgumentNotNull(nameof(response), response); GuardArgumentNotNull(nameof(selector), selector); HtmlElement(expected, await GetHtml(response), selector, elementComparisonMode, elementSelectionMode, ignoreAdditionalAttributes, ignoreAdditionalClassNames, ignoreClassNameOrder); }