Project

General

Profile

Download (4.43 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
     * @return a two dimensional array representing the media items in the gallery, or null if no gallery exists.
55
     */
56
    public static List<List<GalleryImage>> getGalleryImages(WebElement webElement, WebDriverWait wait) {
57

    
58

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

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

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

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