Project

General

Profile

Download (4.59 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> tableRows = gallery.findElements(By.cssSelector("tr.media-row"));
69
        logger.debug("GalleryImages - total rows " + tableRows.size());
70
        // loop table rows
71
        for(int rowId = 0; rowId * 2 < tableRows.size() && tableRows.size() > 0; rowId++ ){
72
            logger.debug("GalleryImages - gallery row " + rowId );
73
            List<WebElement> imageCells = tableRows.get(rowId * 2).findElements(By.tagName("td"));
74
            logger.debug("GalleryImages - number of image cells: " + imageCells.size());
75
            List<WebElement> captionCells = null;
76
            if(tableRows.size() > rowId * 2 + 1){
77
                captionCells = tableRows.get(rowId * 2 + 1).findElements(By.cssSelector("tr.caption-row"));
78
                logger.debug("GalleryImages - number of caption cells: " + captionCells.size());
79
            }
80

    
81
            galleryImageRows.add(new ArrayList<GalleryImage>());
82

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

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

    
101
        }
102

    
103
        return galleryImageRows;
104
    }
105

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

    
121

    
122
}
(9-9/13)