|
2 | 2 |
|
3 | 3 | import java.io.IOException; |
4 | 4 | import org.jsoup.HttpStatusException; |
| 5 | +import org.jsoup.Jsoup; |
| 6 | +import org.jsoup.nodes.Document; |
| 7 | +import org.jsoup.nodes.Element; |
| 8 | +import org.jsoup.parser.Tag; |
| 9 | +import org.jsoup.select.Elements; |
5 | 10 | import static org.junit.Assert.assertEquals; |
6 | 11 | import static org.junit.Assert.assertTrue; |
7 | 12 | import org.junit.Before; |
8 | 13 | import org.junit.Test; |
9 | 14 |
|
10 | 15 | public class JsoupParserTest { |
11 | 16 |
|
12 | | - JsoupParser jsoupParser; |
| 17 | + Document doc; |
13 | 18 |
|
14 | 19 | @Before |
15 | | - public void setUp() { |
16 | | - jsoupParser = new JsoupParser(); |
| 20 | + public void setUp() throws IOException { |
| 21 | + doc = Jsoup.connect("https://spring.io/blog").get(); |
17 | 22 | } |
18 | 23 |
|
19 | 24 | @Test |
20 | | - public void test404() throws IOException { |
| 25 | + public void loadDocument404() throws IOException { |
21 | 26 | try { |
22 | | - jsoupParser.loadDocument("https://spring.io/will-not-be-found"); |
| 27 | + doc = Jsoup.connect("https://spring.io/will-not-be-found").get(); |
23 | 28 | } catch (HttpStatusException ex) { |
24 | 29 | assertEquals(404, ex.getStatusCode()); |
25 | 30 | } |
26 | 31 | } |
27 | 32 |
|
28 | 33 | @Test |
29 | | - public void testChange() throws IOException { |
30 | | - jsoupParser.loadDocument("http://spring.io/blog"); |
| 34 | + public void loadDocumentCustomized() throws IOException { |
| 35 | + doc = Jsoup.connect("https://spring.io/blog") |
| 36 | + .userAgent("Mozilla") |
| 37 | + .timeout(5000) |
| 38 | + .cookie("cookiename", "val234") |
| 39 | + .cookie("anothercookie", "ilovejsoup") |
| 40 | + .referrer("http://google.com") |
| 41 | + .header("headersecurity", "xyz123") |
| 42 | + .get(); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void examplesSelectors() { |
| 47 | + Elements links = doc.select("a"); |
| 48 | + Elements logo = doc.select(".spring-logo--container"); |
| 49 | + Elements pagination = doc.select("#pagination_control"); |
| 50 | + Elements divsDescendant = doc.select("header div"); |
| 51 | + Elements divsDirect = doc.select("header > div"); |
| 52 | + |
| 53 | + Element pag = doc.getElementById("pagination_control"); |
| 54 | + Elements desktopOnly = doc.getElementsByClass("desktopOnly"); |
| 55 | + |
| 56 | + Elements sections = doc.select("section"); |
| 57 | + Element firstSection = sections.first(); |
| 58 | + Elements sectionParagraphs = firstSection.select(".paragraph"); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void examplesTraversing() { |
| 63 | + Elements sections = doc.select("section"); |
| 64 | + |
| 65 | + Element firstSection = sections.first(); |
| 66 | + Element lastSection = sections.last(); |
| 67 | + Element secondSection = sections.get(2); |
| 68 | + Elements allParents = firstSection.parents(); |
| 69 | + Element parent = firstSection.parent(); |
| 70 | + Elements children = firstSection.children(); |
| 71 | + Elements siblings = firstSection.siblingElements(); |
| 72 | + |
| 73 | + sections.stream().forEach(el -> System.out.println("section: " + el)); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void examplesExtracting() { |
| 78 | + Element firstArticle = doc.select("article").first(); |
| 79 | + Element timeElement = firstArticle.select("time").first(); |
| 80 | + String dateTimeOfFirstArticle = timeElement.attr("datetime"); |
| 81 | + Element sectionDiv = firstArticle.select("section div").first(); |
| 82 | + String sectionDivText = sectionDiv.text(); |
| 83 | + String articleHtml = firstArticle.html(); |
| 84 | + String outerHtml = firstArticle.outerHtml(); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void examplesModifying() { |
| 89 | + Element firstArticle = doc.select("article").first(); |
| 90 | + Element timeElement = firstArticle.select("time").first(); |
| 91 | + Element sectionDiv = firstArticle.select("section div").first(); |
| 92 | + |
| 93 | + String dateTimeOfFirstArticle = timeElement.attr("datetime"); |
| 94 | + timeElement.attr("datetime", "2016-12-16 15:19:54.3"); |
| 95 | + sectionDiv.text("foo bar"); |
| 96 | + firstArticle.select("h2").html("<div><span></span></div>"); |
| 97 | + |
| 98 | + Element link = new Element(Tag.valueOf("a"), "") |
| 99 | + .text("Checkout this amazing website!") |
| 100 | + .attr("href", "http://baeldung.com") |
| 101 | + .attr("target", "_blank"); |
| 102 | + firstArticle.appendChild(link); |
31 | 103 |
|
32 | | - jsoupParser.examplesModifying(); |
| 104 | + doc.select("li.navbar-link").remove(); |
| 105 | + firstArticle.select("img").remove(); |
33 | 106 |
|
34 | | - assertTrue(jsoupParser.getTidyHtml().contains("http://baeldung.com")); |
| 107 | + assertTrue(doc.html().contains("http://baeldung.com")); |
35 | 108 | } |
36 | 109 | } |
0 commit comments