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.logging.log4j.LogManager;
|
16
|
import org.apache.logging.log4j.Logger;
|
17
|
import org.openqa.selenium.By;
|
18
|
import org.openqa.selenium.WebElement;
|
19
|
import org.openqa.selenium.support.ui.WebDriverWait;
|
20
|
|
21
|
import eu.etaxonomy.dataportal.elements.BaseElement;
|
22
|
import eu.etaxonomy.dataportal.elements.GalleryImage;
|
23
|
import eu.etaxonomy.dataportal.elements.LinkElement;
|
24
|
import eu.etaxonomy.dataportal.selenium.AllTrue;
|
25
|
import eu.etaxonomy.dataportal.selenium.ChildElementVisible;
|
26
|
|
27
|
/**
|
28
|
* @author andreas
|
29
|
* @since Sep 16, 2011
|
30
|
*/
|
31
|
public class ElementUtils {
|
32
|
|
33
|
private static final Logger logger = LogManager.getLogger();
|
34
|
|
35
|
public static List<BaseElement> baseElementsFromFootNoteListElements(List<WebElement> fnListElements) {
|
36
|
List<BaseElement> footNotes = new ArrayList<>();
|
37
|
for(WebElement fn : fnListElements) {
|
38
|
footNotes.add(new BaseElement(fn));
|
39
|
}
|
40
|
return footNotes;
|
41
|
}
|
42
|
|
43
|
|
44
|
public static List<LinkElement> linkElementsFromFootNoteKeyListElements(List<WebElement> fnkListElements) {
|
45
|
List<LinkElement> footNoteKeys = new ArrayList<>();
|
46
|
for(WebElement fnk : fnkListElements) {
|
47
|
footNoteKeys.add(new LinkElement(fnk));
|
48
|
}
|
49
|
return footNoteKeys;
|
50
|
}
|
51
|
|
52
|
/**
|
53
|
* @param webElement the element containing the media gallery. The gallery must have the class attribute <code>media_gallery</code>.
|
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<>();
|
66
|
|
67
|
List<WebElement> mediaRows = gallery.findElements(By.cssSelector("tr.media-row"));
|
68
|
List<WebElement> captionRows = gallery.findElements(By.cssSelector("tr.caption-row"));
|
69
|
logger.debug("GalleryImages - media rows: " + mediaRows.size() + " caption rows: " + captionRows.size());
|
70
|
// loop table rows
|
71
|
for(int rowId = 0; rowId < mediaRows.size(); rowId++ ){
|
72
|
logger.debug("GalleryImages - gallery row " + rowId );
|
73
|
List<WebElement> imageCells = mediaRows.get(rowId).findElements(By.tagName("td"));
|
74
|
List<WebElement> captionCells = null;
|
75
|
if(rowId < captionRows.size()) {
|
76
|
captionCells = captionRows.get(rowId).findElements(By.tagName("td"));
|
77
|
logger.debug("GalleryImages - image cells: " + imageCells.size() + " caption cells "+ captionCells.size());
|
78
|
}
|
79
|
galleryImageRows.add(new ArrayList<GalleryImage>());
|
80
|
|
81
|
// loop table cells in row
|
82
|
for(int cellId = 0; cellId < imageCells.size(); cellId++) {
|
83
|
logger.debug("cellId:" + cellId);
|
84
|
WebElement imageCell = imageCells.get(cellId);
|
85
|
WebElement captionCell = null;
|
86
|
if(captionCells != null && captionCells.size() > cellId){
|
87
|
captionCell = captionCells.get(cellId);
|
88
|
wait.until(new AllTrue(
|
89
|
// image must be visible
|
90
|
new ChildElementVisible(imageCell, By.tagName("img")),
|
91
|
// metadata should be loaded but may be empty, therefore we are tesing
|
92
|
// for the existence of the element attribute data-cdm-ahah-url-loaded
|
93
|
// in the <div class="ahah-content" ...
|
94
|
new ChildElementVisible(captionCell, By.xpath("div[@data-cdm-ahah-url-loaded]"))
|
95
|
));
|
96
|
|
97
|
} else {
|
98
|
wait.until(new ChildElementVisible(imageCell, By.tagName("img")));
|
99
|
}
|
100
|
galleryImageRows.get(rowId).add(new GalleryImage(imageCell, captionCell));
|
101
|
}
|
102
|
|
103
|
}
|
104
|
|
105
|
return galleryImageRows;
|
106
|
}
|
107
|
|
108
|
/**
|
109
|
* Expected DOM:
|
110
|
*
|
111
|
* <ul class="footnotes">
|
112
|
* <li class="footnotes footnotes-taxon_relationships "><span class="footnote footnote-1">...</span></li>
|
113
|
* <li class="footnotes footnotes-taxon_relationships-annotations ">...</li>
|
114
|
* </ul>
|
115
|
*/
|
116
|
public static List<BaseElement> findFootNotes(WebElement element){
|
117
|
List<WebElement> fnListElements = element.findElements(
|
118
|
By.xpath("./ul[contains(@class,'footnotes')]/li[contains(@class, 'footnotes')]/span[contains(@class, 'footnote')]")
|
119
|
);
|
120
|
return ElementUtils.baseElementsFromFootNoteListElements(fnListElements);
|
121
|
}
|
122
|
|
123
|
/**
|
124
|
* Intended for logging purposes.
|
125
|
* <p>
|
126
|
* Creates incomplete markup of the WebElement with the attributes id and class like:
|
127
|
* {@code <tagName id="the-id" class="">}
|
128
|
*
|
129
|
* @param we the WebElement
|
130
|
* @return the markup
|
131
|
*/
|
132
|
public static String webElementTagToMarkup(WebElement we) {
|
133
|
String markup = "<" + we.getTagName();
|
134
|
String idAttr = we.getAttribute("id");
|
135
|
String classAttr = we.getAttribute("class");
|
136
|
if(StringUtils.isNotEmpty(idAttr)) {
|
137
|
markup += " id=\""+ classAttr + "\"";
|
138
|
}
|
139
|
if(StringUtils.isNotEmpty(classAttr)) {
|
140
|
markup += " class=\""+ classAttr + "\"";
|
141
|
}
|
142
|
markup += ">";
|
143
|
return markup;
|
144
|
}
|
145
|
}
|