Project

General

Profile

Download (5.38 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.commons.lang3.StringUtils;
15
import org.apache.log4j.Logger;
16
import org.openqa.selenium.By;
17
import org.openqa.selenium.WebElement;
18
import org.openqa.selenium.support.ui.WebDriverWait;
19

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

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

    
33

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

    
36

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

    
45

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

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

    
60

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

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

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

    
69
        List<WebElement> mediaRows = gallery.findElements(By.cssSelector("tr.media-row"));
70
        List<WebElement> captionRows = gallery.findElements(By.cssSelector("tr.caption-row"));
71
        logger.debug("GalleryImages - media rows: " + mediaRows.size() + " caption rows: " + captionRows.size());
72
        // loop table rows
73
        for(int rowId = 0; rowId < mediaRows.size(); rowId++ ){
74
            logger.debug("GalleryImages - gallery row " + rowId );
75
            List<WebElement> imageCells = mediaRows.get(rowId).findElements(By.tagName("td"));
76
            List<WebElement> captionCells = null;
77
            if(rowId < captionRows.size()) {
78
                captionCells = captionRows.get(rowId).findElements(By.tagName("td"));
79
                logger.debug("GalleryImages - image cells: " + imageCells.size() + " caption cells "+ captionCells.size());
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
     * Intended for loggin purposes.
123
     * <p>
124
     * Creates incomplete markup of the WebElement with the attributes id and class like:
125
     * {@code <tagName id="the-id" class="">
126
     *
127
     * @param we the WebElement
128
     * @return the markup
129
     */
130
    public static String webElementTagToMarkup(WebElement we) {
131
        String markup = "<" + we.getTagName();
132
        String idAttr = we.getAttribute("id");
133
        String classAttr = we.getAttribute("class");
134
        if(StringUtils.isNotEmpty(idAttr)) {
135
            markup += " id=\""+ classAttr + "\"";
136
        }
137
        if(StringUtils.isNotEmpty(classAttr)) {
138
            markup += " class=\""+ classAttr + "\"";
139
        }
140
        markup += " >";
141
        return markup;
142
    }
143

    
144

    
145
}
(10-10/14)