Search in sources :

Example 1 with JsoupImage

use of com.nixmash.blog.jsoup.base.JsoupImage in project nixmash-blog by mintster.

the class JsoupTests method parseImages.

@Test
public void parseImages() {
    // # of images in content area one less than full testdto.html page
    assertEquals(testDTO.getTestImagesInContentArea().size(), 2);
    assertEquals(testDTO.getTestImagesInPage().size(), 3);
    // attributes of first image in List<JsoupImage>
    JsoupImage img = testDTO.getTestImagesInContentArea().get(0);
    assertEquals("http://example.com/one.png", img.getSrc());
    assertEquals("one", img.getAlt());
    assertTrue(img.getHeight().equals(40));
    // empty image height-width attributes are null
    assertTrue(testDTO.getTestImagesInContentArea().get(1).getHeight() == null);
    // retrieving image with class=myimage
    assertEquals(testDTO.getTestImage().getSrc(), "http://example.com/two.png");
}
Also used : JsoupImage(com.nixmash.blog.jsoup.base.JsoupImage) Test(org.junit.Test)

Example 2 with JsoupImage

use of com.nixmash.blog.jsoup.base.JsoupImage in project nixmash-blog by mintster.

the class JsoupUI method getImages.

private List<JsoupImage> getImages(Document doc) {
    List<JsoupImage> images = new ArrayList<>();
    Elements elements;
    elements = doc.select("[src]");
    for (Element media : elements) {
        if (media.tagName().equals("img")) {
            JsoupImage img = new JsoupImage();
            img.setSrc(media.attr("abs:src"));
            img.setAlt(trim(media.attr("alt"), 60));
            img.setHeight(attrIntToNull(media.attr("height")));
            img.setWidth(attrIntToNull(media.attr("width")));
            images.add(img);
        }
    }
    return images;
}
Also used : JsoupImage(com.nixmash.blog.jsoup.base.JsoupImage) Element(org.jsoup.nodes.Element) ArrayList(java.util.ArrayList) Elements(org.jsoup.select.Elements)

Example 3 with JsoupImage

use of com.nixmash.blog.jsoup.base.JsoupImage in project nixmash-blog by mintster.

the class JsoupPostMetaTests method firstContentImageExtracted.

@Test
public void firstContentImageExtracted() throws IOException {
    // First Src URL in  /html/testPostBody.html = http://nixmash.com/x/blog/2017/dd0120a.png
    List<JsoupImage> images = jsoupPostDTOWithImages.getImagesInContent();
    assertNotNull(images);
    assertTrue(jsoupPostDTOWithImages.hasImages());
    JsoupImage img = jsoupPostDTOWithImages.getImagesInContent().get(0);
    assertThat(img.getSrc()).isEqualTo("http://nixmash.com/x/blog/2017/dd0120a.png");
}
Also used : JsoupImage(com.nixmash.blog.jsoup.base.JsoupImage) Test(org.junit.Test)

Aggregations

JsoupImage (com.nixmash.blog.jsoup.base.JsoupImage)3 Test (org.junit.Test)2 ArrayList (java.util.ArrayList)1 Element (org.jsoup.nodes.Element)1 Elements (org.jsoup.select.Elements)1