示例#1
0
            public void SetInnerHtmlContent()
            {
                // Given
                string            input    = @"<html>
                        <head>
                            <title>Foobar</title>
                        </head>
                        <body>
                            <h1>Title</h1>
                            <p>This is some Foobar text</p>
                            <p>This is some other text</p>
                        </body>
                    </html>";
                IDocument         document = Substitute.For <IDocument>();
                IExecutionContext context  = Substitute.For <IExecutionContext>();
                MemoryStream      stream   = new MemoryStream(Encoding.UTF8.GetBytes(input));

                document.GetStream().Returns(stream);
                HtmlQuery query = new HtmlQuery("p")
                                  .SetContent(false);

                // When
                query.Execute(new[] { document }, context).ToList();  // Make sure to materialize the result list

                // Then
                context.Received(2).GetDocument(Arg.Any <IDocument>(), Arg.Any <string>());
                context.Received().GetDocument(document, "This is some Foobar text");
                context.Received().GetDocument(document, "This is some other text");
                stream.Dispose();
            }
示例#2
0
            public void SetInnerHtmlContent()
            {
                // Given
                const string         input    = @"<html>
                        <head>
                            <title>Foobar</title>
                        </head>
                        <body>
                            <h1>Title</h1>
                            <p>This is some Foobar text</p>
                            <p>This is some other text</p>
                        </body>
                    </html>";
                TestDocument         document = new TestDocument(input);
                TestExecutionContext context  = new TestExecutionContext();
                HtmlQuery            query    = new HtmlQuery("p")
                                                .SetContent(false);

                // When
                List <IDocument> results = query.Execute(new[] { document }, context).ToList();  // Make sure to materialize the result list

                // Then
                results.Select(x => x.Content).ShouldBe(new[]
                {
                    "This is some Foobar text",
                    "This is some other text"
                });
            }
示例#3
0
            public void GetOuterHtmlForFirst()
            {
                // Given
                string            input    = @"<html>
                        <head>
                            <title>Foobar</title>
                        </head>
                        <body>
                            <h1>Title</h1>
                            <p>This is some Foobar text</p>
                            <p>This is some other text</p>
                        </body>
                    </html>";
                IDocument         document = Substitute.For <IDocument>();
                IExecutionContext context  = Substitute.For <IExecutionContext>();
                MemoryStream      stream   = new MemoryStream(Encoding.UTF8.GetBytes(input));

                document.GetStream().Returns(stream);
                HtmlQuery query = new HtmlQuery("p")
                                  .GetOuterHtml("Key")
                                  .First();

                // When
                query.Execute(new[] { document }, context).ToList();  // Make sure to materialize the result list

                // Then
                context.Received(1).GetDocument(document, Arg.Any <IEnumerable <KeyValuePair <string, object> > >());
                context.Received().GetDocument(document,
                                               Arg.Is <IEnumerable <KeyValuePair <string, object> > >(x => x.SequenceEqual(new List <KeyValuePair <string, object> >
                {
                    new KeyValuePair <string, object>("Key", "<p>This is some Foobar text</p>")
                })));
                stream.Dispose();
            }
示例#4
0
            public void GetOuterHtmlForFirst()
            {
                // Given
                const string         input    = @"<html>
                        <head>
                            <title>Foobar</title>
                        </head>
                        <body>
                            <h1>Title</h1>
                            <p>This is some Foobar text</p>
                            <p>This is some other text</p>
                        </body>
                    </html>";
                TestDocument         document = new TestDocument(input);
                TestExecutionContext context  = new TestExecutionContext();
                HtmlQuery            query    = new HtmlQuery("p")
                                                .GetOuterHtml("Key")
                                                .First();

                // When
                IList <IDocument> results = query.Execute(new[] { document }, context).ToList();  // Make sure to materialize the result list

                // Then
                results.Select(x => x["Key"].ToString()).ShouldBe(new[]
                {
                    "<p>This is some Foobar text</p>"
                });
            }
示例#5
0
            public void GetAttributeValueWithMoreThanOneMatch()
            {
                // Given
                const string         input    = @"<html>
                        <head>
                            <title>Foobar</title>
                        </head>
                        <body>
                            <h1>Title</h1>
                            <p foo=""bar"" foo=""bat"">This is some <b>Foobar</b> text</p>
                            <p foo=""baz"">This is some other text</p>
                        </body>
                    </html>";
                TestDocument         document = new TestDocument(input);
                TestExecutionContext context  = new TestExecutionContext();
                HtmlQuery            query    = new HtmlQuery("p")
                                                .GetAttributeValue("foo");

                // When
                IList <IDocument> results = query.Execute(new[] { document }, context).ToList();  // Make sure to materialize the result list

                // Then
                results.Select(x => x.String("foo")).ShouldBe(new[]
                {
                    "bar",
                    "baz"
                });
            }
示例#6
0
            public void SetOuterHtmlContent()
            {
                // Given
                string               input    = @"<html>
                        <head>
                            <title>Foobar</title>
                        </head>
                        <body>
                            <h1>Title</h1>
                            <p>This is some Foobar text</p>
                            <p>This is some other text</p>
                        </body>
                    </html>";
                TestDocument         document = new TestDocument(input);
                TestExecutionContext context  = new TestExecutionContext();
                HtmlQuery            query    = new HtmlQuery("p")
                                                .SetContent();

                // When
                List <IDocument> results = query.Execute(new[] { document }, context).ToList();  // Make sure to materialize the result list

                // Then
                Assert.That(results, Has.Count.EqualTo(2));
                Assert.That(results.Select(x => x.Content), Is.EquivalentTo(new[]
                {
                    "<p>This is some Foobar text</p>",
                    "<p>This is some other text</p>"
                }));
            }
示例#7
0
            public void GetAll()
            {
                // Given
                string            input    = @"<html>
                        <head>
                            <title>Foobar</title>
                        </head>
                        <body>
                            <h1>Title</h1>
                            <p foo=""bar"" foo=""bat"" a=""A"" b=""B"">This is some <b>Foobar</b> text</p>
                            <p foo=""baz"" x=""X"">This is some other text</p>
                        </body>
                    </html>";
                IDocument         document = Substitute.For <IDocument>();
                IExecutionContext context  = Substitute.For <IExecutionContext>();
                MemoryStream      stream   = new MemoryStream(Encoding.UTF8.GetBytes(input));

                document.GetStream().Returns(stream);
                HtmlQuery query = new HtmlQuery("p")
                                  .GetAll();

                // When
                query.Execute(new[] { document }, context).ToList();  // Make sure to materialize the result list

                // Then
                context.Received(2).GetDocument(Arg.Any <IDocument>(), Arg.Any <IEnumerable <KeyValuePair <string, object> > >());
                context.Received().GetDocument(
                    document,
                    Arg.Is <IEnumerable <KeyValuePair <string, object> > >(x => x.SequenceEqual(new List <KeyValuePair <string, object> >
                {
                    new KeyValuePair <string, object>("OuterHtml", @"<p foo=""bar"" a=""A"" b=""B"">This is some <b>Foobar</b> text</p>"),
                    new KeyValuePair <string, object>("InnerHtml", "This is some <b>Foobar</b> text"),
                    new KeyValuePair <string, object>("TextContent", "This is some Foobar text"),
                    new KeyValuePair <string, object>("foo", "bar"),
                    new KeyValuePair <string, object>("a", "A"),
                    new KeyValuePair <string, object>("b", "B")
                })));
                context.Received().GetDocument(
                    document,
                    Arg.Is <IEnumerable <KeyValuePair <string, object> > >(x => x.SequenceEqual(new List <KeyValuePair <string, object> >
                {
                    new KeyValuePair <string, object>("OuterHtml", @"<p foo=""baz"" x=""X"">This is some other text</p>"),
                    new KeyValuePair <string, object>("InnerHtml", "This is some other text"),
                    new KeyValuePair <string, object>("TextContent", "This is some other text"),
                    new KeyValuePair <string, object>("foo", "baz"),
                    new KeyValuePair <string, object>("x", "X")
                })));
                stream.Dispose();
            }
示例#8
0
            public void GetAll()
            {
                // Given
                const string         input    = @"<html>
                        <head>
                            <title>Foobar</title>
                        </head>
                        <body>
                            <h1>Title</h1>
                            <p foo=""bar"" foo=""bat"" a=""A"" b=""B"">This is some <b>Foobar</b> text</p>
                            <p foo=""baz"" x=""X"">This is some other text</p>
                        </body>
                    </html>";
                TestDocument         document = new TestDocument(input);
                TestExecutionContext context  = new TestExecutionContext();
                HtmlQuery            query    = new HtmlQuery("p")
                                                .GetAll();

                // When
                IList <IDocument> results = query.Execute(new[] { document }, context).ToList();  // Make sure to materialize the result list

                // Then
                results
                .Select(x => x.OrderBy(y => y.Key, StringComparer.OrdinalIgnoreCase))
                .Cast <IEnumerable <KeyValuePair <string, object> > >()
                .ShouldBe(new[]
                {
                    new List <KeyValuePair <string, object> >
                    {
                        new KeyValuePair <string, object>("a", "A"),
                        new KeyValuePair <string, object>("b", "B"),
                        new KeyValuePair <string, object>("foo", "bar"),
                        new KeyValuePair <string, object>("InnerHtml", "This is some <b>Foobar</b> text"),
                        new KeyValuePair <string, object>("OuterHtml", @"<p foo=""bar"" a=""A"" b=""B"">This is some <b>Foobar</b> text</p>"),
                        new KeyValuePair <string, object>("TextContent", "This is some Foobar text")
                    },
                    new List <KeyValuePair <string, object> >
                    {
                        new KeyValuePair <string, object>("foo", "baz"),
                        new KeyValuePair <string, object>("InnerHtml", "This is some other text"),
                        new KeyValuePair <string, object>("OuterHtml", @"<p foo=""baz"" x=""X"">This is some other text</p>"),
                        new KeyValuePair <string, object>("TextContent", "This is some other text"),
                        new KeyValuePair <string, object>("x", "X")
                    }
                });
            }
示例#9
0
            public void GetAll()
            {
                // Given
                string input = @"<html>
                        <head>
                            <title>Foobar</title>
                        </head>
                        <body>
                            <h1>Title</h1>
                            <p foo=""bar"" foo=""bat"" a=""A"" b=""B"">This is some <b>Foobar</b> text</p>
                            <p foo=""baz"" x=""X"">This is some other text</p>
                        </body>
                    </html>";
                IDocument document = Substitute.For<IDocument>();
                IExecutionContext context = Substitute.For<IExecutionContext>();
                MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(input));
                document.GetStream().Returns(stream);
                HtmlQuery query = new HtmlQuery("p")
                    .GetAll();

                // When
                query.Execute(new[] { document }, context).ToList();  // Make sure to materialize the result list

                // Then
                context.Received(2).GetDocument(Arg.Any<IDocument>(), Arg.Any<IEnumerable<KeyValuePair<string, object>>>());
                context.Received().GetDocument(document,
                    Arg.Is<IEnumerable<KeyValuePair<string, object>>>(x => x.SequenceEqual(new List<KeyValuePair<string, object>>
                    {
                        new KeyValuePair<string, object>("OuterHtml", @"<p foo=""bar"" a=""A"" b=""B"">This is some <b>Foobar</b> text</p>"),
                        new KeyValuePair<string, object>("InnerHtml", "This is some <b>Foobar</b> text"),
                        new KeyValuePair<string, object>("TextContent", "This is some Foobar text"),
                        new KeyValuePair<string, object>("foo", "bar"),
                        new KeyValuePair<string, object>("a", "A"),
                        new KeyValuePair<string, object>("b", "B")
                    })));
                context.Received().GetDocument(document,
                    Arg.Is<IEnumerable<KeyValuePair<string, object>>>(x => x.SequenceEqual(new List<KeyValuePair<string, object>>
                    {
                        new KeyValuePair<string, object>("OuterHtml", @"<p foo=""baz"" x=""X"">This is some other text</p>"),
                        new KeyValuePair<string, object>("InnerHtml", "This is some other text"),
                        new KeyValuePair<string, object>("TextContent", "This is some other text"),
                        new KeyValuePair<string, object>("foo", "baz"),
                        new KeyValuePair<string, object>("x", "X")
                    })));
                stream.Dispose();
            }
示例#10
0
        public void SetOuterHtmlContentWithMetadata()
        {
            // Given
            string       input    = @"<html>
                    <head>
                        <title>Foobar</title>
                    </head>
                    <body>
                        <h1>Title</h1>
                        <p>This is some Foobar text</p>
                        <p>This is some other text</p>
                    </body>
                </html>";
            IDocument    document = Substitute.For <IDocument>();
            MemoryStream stream   = new MemoryStream(Encoding.UTF8.GetBytes(input));

            document.GetStream().Returns(stream);
            HtmlQuery query = new HtmlQuery("p")
                              .SetContent()
                              .GetInnerHtml("InnerHtmlKey")
                              .GetOuterHtml("OuterHtmlKey");

            // When
            query.Execute(new[] { document }, null).ToList();  // Make sure to materialize the result list

            // Then
            document.Received(2).Clone(Arg.Any <string>(), Arg.Any <IEnumerable <KeyValuePair <string, object> > >());
            document.Received().Clone("<p>This is some Foobar text</p>",
                                      Arg.Is <IEnumerable <KeyValuePair <string, object> > >(x => x.SequenceEqual(new List <KeyValuePair <string, object> >
            {
                new KeyValuePair <string, object>("InnerHtmlKey", "This is some Foobar text"),
                new KeyValuePair <string, object>("OuterHtmlKey", "<p>This is some Foobar text</p>")
            })));
            document.Received().Clone("<p>This is some other text</p>",
                                      Arg.Is <IEnumerable <KeyValuePair <string, object> > >(x => x.SequenceEqual(new List <KeyValuePair <string, object> >
            {
                new KeyValuePair <string, object>("InnerHtmlKey", "This is some other text"),
                new KeyValuePair <string, object>("OuterHtmlKey", "<p>This is some other text</p>")
            })));
            stream.Dispose();
        }
示例#11
0
        public void GetAttributeValueWithMoreThanOneMatch()
        {
            // Given
            string       input    = @"<html>
                    <head>
                        <title>Foobar</title>
                    </head>
                    <body>
                        <h1>Title</h1>
                        <p foo=""bar"" foo=""bat"">This is some <b>Foobar</b> text</p>
                        <p foo=""baz"">This is some other text</p>
                    </body>
                </html>";
            IDocument    document = Substitute.For <IDocument>();
            MemoryStream stream   = new MemoryStream(Encoding.UTF8.GetBytes(input));

            document.GetStream().Returns(stream);
            HtmlQuery query = new HtmlQuery("p")
                              .GetAttributeValue("foo");

            // When
            query.Execute(new[] { document }, null).ToList();  // Make sure to materialize the result list

            // Then
            document.Received(2).Clone(Arg.Any <IEnumerable <KeyValuePair <string, object> > >());
            document.Received().Clone(
                Arg.Is <IEnumerable <KeyValuePair <string, object> > >(x => x.SequenceEqual(new List <KeyValuePair <string, object> >
            {
                new KeyValuePair <string, object>("foo", "bar")
            })));
            document.Received().Clone(
                Arg.Is <IEnumerable <KeyValuePair <string, object> > >(x => x.SequenceEqual(new List <KeyValuePair <string, object> >
            {
                new KeyValuePair <string, object>("foo", "baz")
            })));
            stream.Dispose();
        }
示例#12
0
        public void GetAttributeValue()
        {
            // Given
            string input = @"<html>
                    <head>
                        <title>Foobar</title>
                    </head>
                    <body>
                        <h1>Title</h1>
                        <p foo=""bar"">This is some <b>Foobar</b> text</p>
                        <p foo=""baz"">This is some other text</p>
                    </body>
                </html>";
            IDocument document = Substitute.For<IDocument>();
            MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(input));
            document.GetStream().Returns(stream);
            HtmlQuery query = new HtmlQuery("p")
                .GetAttributeValue("foo", "Foo");

            // When
            query.Execute(new[] { document }, null).ToList();  // Make sure to materialize the result list

            // Then
            document.Received(2).Clone(Arg.Any<IEnumerable<KeyValuePair<string, object>>>());
            document.Received().Clone(
                Arg.Is<IEnumerable<KeyValuePair<string, object>>>(x => x.SequenceEqual(new List<KeyValuePair<string, object>>
                {
                    new KeyValuePair<string, object>("Foo", "bar")
                })));
            document.Received().Clone(
                Arg.Is<IEnumerable<KeyValuePair<string, object>>>(x => x.SequenceEqual(new List<KeyValuePair<string, object>>
                {
                    new KeyValuePair<string, object>("Foo", "baz")
                })));
            stream.Dispose();
        }
示例#13
0
            public void SetOuterHtmlContentWithMetadata()
            {
                // Given
                string input = @"<html>
                        <head>
                            <title>Foobar</title>
                        </head>
                        <body>
                            <h1>Title</h1>
                            <p>This is some Foobar text</p>
                            <p>This is some other text</p>
                        </body>
                    </html>";
                IDocument document = Substitute.For<IDocument>();
                IExecutionContext context = Substitute.For<IExecutionContext>();
                MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(input));
                document.GetStream().Returns(stream);
                HtmlQuery query = new HtmlQuery("p")
                    .SetContent()
                    .GetInnerHtml("InnerHtmlKey")
                    .GetOuterHtml("OuterHtmlKey");

                // When
                query.Execute(new[] { document }, context).ToList();  // Make sure to materialize the result list

                // Then
                context.Received(2).GetDocument(Arg.Any<IDocument>(), Arg.Any<string>(), Arg.Any<IEnumerable<KeyValuePair<string, object>>>());
                context.Received().GetDocument(document,"<p>This is some Foobar text</p>",
                    Arg.Is<IEnumerable<KeyValuePair<string, object>>>(x => x.SequenceEqual(new List<KeyValuePair<string, object>>
                    {
                        new KeyValuePair<string, object>("InnerHtmlKey", "This is some Foobar text"),
                        new KeyValuePair<string, object>("OuterHtmlKey", "<p>This is some Foobar text</p>")
                    })));
                context.Received().GetDocument(document,"<p>This is some other text</p>",
                    Arg.Is<IEnumerable<KeyValuePair<string, object>>>(x => x.SequenceEqual(new List<KeyValuePair<string, object>>
                    {
                        new KeyValuePair<string, object>("InnerHtmlKey", "This is some other text"),
                        new KeyValuePair<string, object>("OuterHtmlKey", "<p>This is some other text</p>")
                    })));
                stream.Dispose();
            }
示例#14
0
            public void SetInnerHtmlContent()
            {
                // Given
                string input = @"<html>
                        <head>
                            <title>Foobar</title>
                        </head>
                        <body>
                            <h1>Title</h1>
                            <p>This is some Foobar text</p>
                            <p>This is some other text</p>
                        </body>
                    </html>";
                IDocument document = Substitute.For<IDocument>();
                IExecutionContext context = Substitute.For<IExecutionContext>();
                MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(input));
                document.GetStream().Returns(stream);
                HtmlQuery query = new HtmlQuery("p")
                    .SetContent(false);

                // When
                query.Execute(new[] { document }, context).ToList();  // Make sure to materialize the result list

                // Then
                context.Received(2).GetDocument(Arg.Any<IDocument>(), Arg.Any<string>());
                context.Received().GetDocument(document,"This is some Foobar text");
                context.Received().GetDocument(document,"This is some other text");
                stream.Dispose();
            }