示例#1
0
        public void FindNthChild()
        {
            var compareHtml = "<html><body><p>A</p><p>C</p></html>";
            var x = new HtmlComparer(compareHtml, "<html><p>A</p><p>B</p></body></html>");
            Assert.AreEqual("C", x.Diffs[0].compareSourceHtml);
            Assert.AreEqual(true, x.Diffs[0].isText);
            Assert.AreEqual("html > body > p:nth-child(2)", x.Diffs[0].selector);
            Assert.AreEqual(1, x.Diffs.Count);

            var useComputedSelector = ((CQ)compareHtml)["html > body > p:nth-child(2)"].Text();
            Assert.AreEqual("C", useComputedSelector);


        }
示例#2
0
        public void ExcludeSelectors()
        {
            var html1 = "<html><body><p>A</p><p class='exclude'>C</p></html>";
            var html2 = "<html><p>A</p><p class='exclude'>D</p></body></html>";
            var x = new HtmlComparer(html1, html2);
            Assert.AreEqual("C", x.Diffs[0].compareSourceHtml);
            Assert.AreEqual(true, x.Diffs[0].isText);
            Assert.AreEqual("html > body > p.exclude", x.Diffs[0].selector);

            Assert.AreEqual(1, x.Diffs.Count);

            var excludeSelectors = new[] { ".exclude" };

            var ex = new HtmlComparer(html1, html2, "",excludeSelectors);

            Assert.AreEqual(0, ex.Diffs.Count);

        }
示例#3
0
        public void FindDiff()
        {
            var compareHtml = "<html><body><p>A</p></html>";
            var x = new HtmlComparer(compareHtml, "<html><p>B</p></body></html>");
            Assert.AreEqual("A", x.Diffs[0].compareSourceHtml);
            Assert.AreEqual(true, x.Diffs[0].isText);
            Assert.AreEqual("html > body > p", x.Diffs[0].selector);

            Assert.AreEqual(1, x.Diffs.Count);

            var useComputedSelector = ((CQ)compareHtml)[x.Diffs[0].selector].Text();
            Assert.AreEqual("A", useComputedSelector);

            var useWrittenSelector = ((CQ)compareHtml)["html > body > p"].Text();
            Assert.AreEqual("A", useWrittenSelector);


        }
示例#4
0
        /// <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);
            }
        }