Project

General

Profile

Download (4.61 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2011 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9
package eu.etaxonomy.dataportal;
10

    
11
import java.util.ArrayList;
12
import java.util.List;
13

    
14
import org.apache.log4j.Logger;
15
import org.openqa.selenium.By;
16
import org.openqa.selenium.WebElement;
17
import org.openqa.selenium.support.ui.WebDriverWait;
18

    
19
import eu.etaxonomy.dataportal.elements.BaseElement;
20
import eu.etaxonomy.dataportal.elements.GalleryImage;
21
import eu.etaxonomy.dataportal.elements.LinkElement;
22
import eu.etaxonomy.dataportal.selenium.AllTrue;
23
import eu.etaxonomy.dataportal.selenium.ChildElementVisible;
24

    
25
/**
26
 * @author andreas
27
 * @since Sep 16, 2011
28
 *
29
 */
30
public class ElementUtils {
31

    
32

    
33
    public static final Logger logger = Logger.getLogger(ElementUtils.class);
34

    
35

    
36
    public static List<BaseElement> baseElementsFromFootNoteListElements(List<WebElement> fnListElements) {
37
        List<BaseElement> footNotes = new ArrayList<BaseElement>();
38
        for(WebElement fn : fnListElements) {
39
            footNotes.add(new BaseElement(fn));
40
        }
41
        return footNotes;
42
    }
43

    
44

    
45
    public static List<LinkElement> linkElementsFromFootNoteKeyListElements(List<WebElement> fnkListElements) {
46
        List<LinkElement> footNoteKeys = new ArrayList<LinkElement>();
47
        for(WebElement fnk : fnkListElements) {
48
            footNoteKeys.add(new LinkElement(fnk));
49
        }
50
        return footNoteKeys;
51
    }
52

    
53
    /**
54
     * @param webElement the element containing the media gallery. The gallery must have the class attribute <code>media_gallery</code>.
55
     * @return a two dimensional array representing the media items in the gallery, or null if no gallery exists.
56
     */
57
    public static List<List<GalleryImage>> getGalleryImages(WebElement webElement, WebDriverWait wait) {
58

    
59

    
60
        WebElement gallery = webElement.findElement(By.className("media_gallery"));
61

    
62
        if( gallery == null){
63
            return null;
64
        }
65

    
66
        ArrayList<List<GalleryImage>> galleryImageRows = new ArrayList<List<GalleryImage>>();
67

    
68
        List<WebElement> mediaRows = gallery.findElements(By.cssSelector("tr.media-row"));
69
        List<WebElement> captionRows = gallery.findElements(By.cssSelector("tr.caption-row"));
70
        logger.debug("GalleryImages - media rows: " + mediaRows.size() + " caption rows: " + captionRows.size());
71
        // loop table rows
72
        for(int rowId = 0; rowId < mediaRows.size(); rowId++ ){
73
            logger.debug("GalleryImages - gallery row " + rowId );
74
            List<WebElement> imageCells = mediaRows.get(rowId).findElements(By.tagName("td"));
75
            List<WebElement> captionCells = null;
76
            if(rowId < captionRows.size()) {
77
                captionCells = captionRows.get(rowId).findElements(By.tagName("td"));
78
                logger.debug("GalleryImages - image cells: " + imageCells.size() + " caption cells "+ captionCells.size());
79
            }
80
            galleryImageRows.add(new ArrayList<GalleryImage>());
81

    
82
            // loop table cells in row
83
            for(int cellId = 0; cellId < imageCells.size(); cellId++) {
84
                logger.debug("cellId:" + cellId);
85
                WebElement imageCell = imageCells.get(cellId);
86
                WebElement captionCell = null;
87
                if(captionCells != null && captionCells.size() > cellId){
88
                    captionCell = captionCells.get(cellId);
89
                    wait.until(new AllTrue(
90
                            new ChildElementVisible(imageCell, By.tagName("img")),
91
                            new ChildElementVisible(captionCell, By.tagName("dl"))
92
                    ));
93

    
94
                } else {
95
                    wait.until(new ChildElementVisible(imageCell, By.tagName("img")));
96
                }
97
                galleryImageRows.get(rowId).add(new GalleryImage(imageCell, captionCell));
98
            }
99

    
100
        }
101

    
102
        return galleryImageRows;
103
    }
104

    
105
    /**
106
     * Expected DOM:
107
     *
108
     * <ul class="footnotes">
109
     *   <li class="footnotes footnotes-taxon_relationships "><span class="footnote footnote-1">...</span></li>
110
     *   <li class="footnotes footnotes-taxon_relationships-annotations ">...</li>
111
     * </ul>
112
     */
113
    public static List<BaseElement> findFootNotes(WebElement element){
114
        List<WebElement> fnListElements = element.findElements(
115
                By.xpath("./ul[contains(@class,'footnotes')]/li[contains(@class, 'footnotes')]/span[contains(@class, 'footnote')]")
116
        );
117
        return ElementUtils.baseElementsFromFootNoteListElements(fnListElements);
118
    }
119

    
120

    
121
}
(10-10/14)