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(); }
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>" }); }
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(); }
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" }); }
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>" })); }
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" }); }
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(); }
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") } }); }
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(); }
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(); }
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(); }
public void GetOuterHtmlWithAttributes() { // Given string input = @"<html> <head> <title>Foobar</title> </head> <body> <h1>Title</h1> <p foo=""bar"">This is some Foobar text</p> <p foo=""baz"" foo=""bat"" a=""A"">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") .GetOuterHtml("Key"); // 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>("Key", @"<p foo=""bar"">This is some Foobar text</p>") }))); document.Received().Clone( Arg.Is<IEnumerable<KeyValuePair<string, object>>>(x => x.SequenceEqual(new List<KeyValuePair<string, object>> { new KeyValuePair<string, object>("Key", @"<p foo=""baz"" a=""A"">This is some other text</p>") }))); stream.Dispose(); }
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(); }
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(); }