Exemplo n.º 1
0
        public void GetStringContent_WithInlineEditingAttribute_TextElementProperlyCreated()
        {
            // Arrange
            var htmlProcessor    = new HtmlProcessor();
            var dummyWidgetModel = new DummyWidgetModel {
                EditableContent = this.dummyContent, NonEditableContent = this.dummyContent
            };

            var fieldName = "DummyWidget";
            var type      = "LongText";

            // Act
            string inlineeditingAwareContent = htmlProcessor.GetStringContent(dummyWidgetModel, "EditableContent");

            // Assert
            using (var parser = new HtmlParser(inlineeditingAwareContent))
            {
                HtmlChunk chunk = parser.ParseNext();
                Assert.IsNotNull(chunk);

                // checks if the HTML tag is of type div and if it has the required attributes
                Assert.IsTrue(chunk.TagName.Equals(this.htmlWrapperTag, StringComparison.Ordinal), "There is no wrapper div appended to the property representation.");
                Assert.IsTrue(chunk.HasAttribute(this.fieldAttribute), "The field attribute is not appended correctly.");
                Assert.IsTrue(chunk.HasAttribute(this.fieldTypeAttribute), "The field type attribute is not appended correctly.");

                // checks if the required attributes has proper values assigned to them
                Assert.AreEqual(fieldName, chunk.GetParamValue(this.fieldAttribute), "The value of the field attribute is not correct.");
                Assert.AreEqual(type, chunk.GetParamValue(this.fieldTypeAttribute), "The value of the fieldType attribute is not correct.");

                this.AssertContentAndCloseTag(parser);
            }
        }
Exemplo n.º 2
0
        public void CreateInlineEditingRegion_DummyContent_IsWrappedIntoInlineEditingRegion()
        {
            // Arrange: create dummy data which will be set to the related attributes inside the region div tag
            TextWriter writer       = new StringWriter(System.Globalization.CultureInfo.InvariantCulture);
            var        providerName = "dummyProvider";
            var        type         = "dummyType";
            Guid       id           = Guid.NewGuid();

            // Act: create the CreateInlineEditingRegion
            var htmlProcessor = new HtmlProcessor();

            using (htmlProcessor.CreateInlineEditingRegion(writer, providerName, type, id))
            {
                writer.WriteLine(this.dummyContent);
            }

            string outPut = writer.ToString();

            // Assert: Parses the generated by the htmlTransformationProxy HTML checks if the HTML content is properly wrapped into a div tag
            // which has the required by the InlineEditing attributes
            // and these attributes has a proper data assigned
            using (var parser = new HtmlParser(outPut))
            {
                HtmlChunk chunk = parser.ParseNext();
                Assert.IsNotNull(chunk);

                // checks if the HTML tag is of type div and if it has the required attributes
                Assert.IsTrue(chunk.TagName.Equals(this.htmlWrapperTag, StringComparison.Ordinal));
                Assert.IsTrue(chunk.HasAttribute(this.idAttribute), "The id of the item is not appended as attribute correctly.");
                Assert.IsTrue(chunk.HasAttribute(this.providerAttribute), "The provider is not appended as attribute correctly.");
                Assert.IsTrue(chunk.HasAttribute(this.typeAttribute), "The id type the item is not appended as attribute correctly.");

                // checks if the required attributes has proper values assigned to them
                Assert.AreEqual(providerName, chunk.GetParamValue(this.providerAttribute), "The value of the provider attribute is not correct.");
                Assert.AreEqual(type, chunk.GetParamValue(this.typeAttribute), "The value of the provider attribute is not correct.");
                Assert.AreEqual(id.ToString(), chunk.GetParamValue(this.idAttribute), "The value of the id attribute is not correct.");

                this.AssertContentAndCloseTag(parser);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Asserts that the expected tags exists on the page and that not expected tags do not exists on the page.
        /// </summary>
        /// <param name="pageContent">Content of the page.</param>
        /// <param name="expectedTags">The expected tags.</param>
        /// <param name="notExpectedTags">The not expected tags.</param>
        /// <param name="showCount">The show count.</param>
        private void AssertTagsLinks(string pageContent, List <Tag> expectedTags, List <Tag> notExpectedTags, bool showCount)
        {
            pageContent = pageContent.Replace(Environment.NewLine, string.Empty);

            using (HtmlParser parser = new HtmlParser(pageContent))
            {
                HtmlChunk chunk = null;
                parser.SetChunkHashMode(false);
                parser.AutoExtractBetweenTagsOnly  = true;
                parser.CompressWhiteSpaceBeforeTag = true;
                parser.KeepRawHTML = true;

                while ((chunk = parser.ParseNext()) != null)
                {
                    if (chunk.TagName.Equals("a") && !chunk.IsClosure)
                    {
                        var linkHref = chunk.GetParamValue("href");
                        chunk = parser.ParseNext();
                        var linkText = chunk.Html;

                        foreach (var tag in notExpectedTags)
                        {
                            if (linkHref != null)
                            {
                                Assert.IsFalse(
                                    linkHref.EndsWith(tag.Url, StringComparison.Ordinal),
                                    string.Format(CultureInfo.InvariantCulture, "The tag's url {0} is found but is not expected.", linkHref));

                                Assert.AreNotEqual(
                                    tag.Name,
                                    linkText,
                                    string.Format(CultureInfo.InvariantCulture, "The tag's name {0} is found but is not expected.", linkText));
                            }
                        }

                        var currentTag = expectedTags.First();

                        Assert.IsTrue(
                            linkHref.EndsWith(currentTag.Url, StringComparison.Ordinal),
                            string.Format(CultureInfo.InvariantCulture, "The expected tag url {0} is not found.", currentTag.Url));

                        Assert.AreEqual(
                            currentTag.Name,
                            linkText,
                            string.Format(CultureInfo.InvariantCulture, "The tag's name {0} is not correct.", linkText));

                        if (showCount)
                        {
                            parser.ParseNext();                  // gets the closing tag </a>
                            var countChunk = parser.ParseNext(); // gets the chunk that contains the tag's count

                            var count = countChunk.Html.Trim();
                            Assert.AreEqual(
                                "(" + currentTag.Count + ")",
                                count,
                                string.Format(CultureInfo.InstalledUICulture, "The tag's count {0} is not correct.", count));
                        }
                        else
                        {
                            parser.ParseNext();                 // gets the closing tag </a>
                            parser.ParseNext();                 // gets an empty line
                            var listChunk = parser.ParseNext(); // gets the closing </li>

                            // Verifies there is no count before the closing li tag
                            Assert.IsTrue(listChunk.TagName.Equals("li"));
                            Assert.IsTrue(listChunk.IsClosure);
                        }

                        expectedTags.RemoveAt(0);
                    }
                }
            }
        }
Exemplo n.º 4
0
        private void AssertLanguageLinks(string pageContent, Dictionary <CultureInfo, string> links, Dictionary <CultureInfo, string> notVisiblelinks)
        {
            using (HtmlParser parser = new HtmlParser(pageContent))
            {
                HtmlChunk chunk = null;
                parser.SetChunkHashMode(false);
                parser.AutoExtractBetweenTagsOnly  = true;
                parser.CompressWhiteSpaceBeforeTag = true;
                parser.KeepRawHTML = true;
                var initialLinks    = new Dictionary <CultureInfo, string>(links);
                var htmlTagEndIndex = 0;

                while ((chunk = parser.ParseNext()) != null)
                {
                    if (chunk.TagName.Equals("html") && chunk.IsClosure)
                    {
                        htmlTagEndIndex = chunk.ChunkOffset;
                    }
                    else if (chunk.TagName.Equals("a") && !chunk.IsClosure && chunk.GetParamValue("onclick") != null)
                    {
                        var linkOnClickAttribute = chunk.GetParamValue("onclick");
                        chunk = parser.ParseNext();
                        var linkText = chunk.Html;

                        foreach (var link in notVisiblelinks)
                        {
                            Assert.IsFalse(
                                linkOnClickAttribute.Contains(link.Key.Name),
                                string.Format(CultureInfo.InvariantCulture, "The anchor tag for culture {0} is found, but is not expected.", linkOnClickAttribute));

                            Assert.AreNotEqual(
                                link.Key.NativeName,
                                linkText,
                                string.Format(CultureInfo.InvariantCulture, "The link display name {0} is found, but is not expected.", linkText));
                        }

                        var foundlanguageCulture = new CultureInfo(string.Empty);

                        foreach (var link in links)
                        {
                            Assert.IsTrue(
                                linkOnClickAttribute.Contains(link.Key.Name),
                                string.Format(CultureInfo.InvariantCulture, "The expected link's culture {0} is not found.", link.Value));

                            Assert.AreEqual(
                                HttpUtility.HtmlEncode(link.Key.NativeName),
                                linkText,
                                string.Format(CultureInfo.InvariantCulture, "The link display name {0} is not correct.", linkText));

                            foundlanguageCulture = link.Key;
                            break;
                        }

                        Assert.IsFalse(
                            string.IsNullOrEmpty(foundlanguageCulture.Name),
                            string.Format(CultureInfo.InvariantCulture, "The anchor tag for culture {0} is not expected.", linkOnClickAttribute));

                        links.Remove(foundlanguageCulture);
                    }
                    else if (chunk.TagName.Equals("input") && chunk.GetParamValue("type") == "hidden" && chunk.GetParamValue("value") != null && (chunk.GetParamValue("value").StartsWith("http://", StringComparison.Ordinal) || chunk.GetParamValue("value").StartsWith("https://", StringComparison.Ordinal)))
                    {
                        var dataSfRole = chunk.GetParamValue("data-sf-role");
                        if (dataSfRole != null)
                        {
                            var currentCulture = new CultureInfo(dataSfRole);
                            Assert.IsTrue(initialLinks.ContainsKey(currentCulture), string.Format(CultureInfo.InvariantCulture, "The found hidden input field {0} is not expected", currentCulture));
                            var expectedLink = initialLinks[currentCulture];

                            var hiddenInputValue = chunk.GetParamValue("value");

                            Assert.IsFalse(htmlTagEndIndex > 0, "Hidden field not exist inside html tag");
                            Assert.IsTrue(hiddenInputValue.EndsWith(expectedLink, StringComparison.Ordinal));

                            initialLinks.Remove(currentCulture);
                        }
                    }
                }

                Assert.AreEqual(0, links.Count(), "Not all expected languages are found.");
                Assert.AreEqual(0, initialLinks.Count(), "Not all expected hidden fields of languages are found.");
            }
        }