1
|
// $Id$
|
2
|
/**
|
3
|
* Copyright (C) 2011 EDIT
|
4
|
* European Distributed Institute of Taxonomy
|
5
|
* http://www.e-taxonomy.eu
|
6
|
*
|
7
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
8
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
9
|
*/
|
10
|
package eu.etaxonomy.dataportal.elements;
|
11
|
|
12
|
import static org.junit.Assert.assertEquals;
|
13
|
|
14
|
import java.util.ArrayList;
|
15
|
import java.util.HashMap;
|
16
|
import java.util.List;
|
17
|
|
18
|
import org.apache.log4j.Level;
|
19
|
import org.openqa.selenium.By;
|
20
|
import org.openqa.selenium.JavascriptExecutor;
|
21
|
import org.openqa.selenium.NoSuchElementException;
|
22
|
import org.openqa.selenium.WebDriver;
|
23
|
import org.openqa.selenium.WebElement;
|
24
|
|
25
|
/**
|
26
|
* @author andreas
|
27
|
* @date Jul 4, 2011
|
28
|
*
|
29
|
*/
|
30
|
public class FeatureBlock extends DrupalBlock {
|
31
|
|
32
|
/**
|
33
|
* JQuery Selector for footnotekeys
|
34
|
*/
|
35
|
private static final String SELECTOR_FOOTNOTE_KEY = "span.footnote-key a";
|
36
|
|
37
|
/**
|
38
|
* JQuery Selector for footnotes
|
39
|
*/
|
40
|
private static final String SELECTOR_FOOTNOTE = "span.footnote";
|
41
|
|
42
|
private final WebDriver driver;
|
43
|
|
44
|
private List<BaseElement> originalSources = null;
|
45
|
|
46
|
private String featureType = null;
|
47
|
|
48
|
private final String[] elementTags;
|
49
|
|
50
|
private final List<WebElement> descriptionItemElements;
|
51
|
|
52
|
|
53
|
/**
|
54
|
*
|
55
|
* @param index 0 based
|
56
|
* @return
|
57
|
*/
|
58
|
public LinkElement getFootNoteKey(int index) {
|
59
|
WebElement footNoteKeyElement = jsGetElement(SELECTOR_FOOTNOTE_KEY, index);
|
60
|
if(footNoteKeyElement != null) {
|
61
|
return new LinkElement(footNoteKeyElement);
|
62
|
} else {
|
63
|
return null;
|
64
|
}
|
65
|
}
|
66
|
|
67
|
public boolean hasFootNoteKeys() {
|
68
|
return countFootNoteKeys() > 0;
|
69
|
}
|
70
|
|
71
|
public long countFootNoteKeys() {
|
72
|
return jsCountElements(SELECTOR_FOOTNOTE_KEY);
|
73
|
}
|
74
|
|
75
|
/**
|
76
|
*
|
77
|
* @param index 0 based
|
78
|
* @return
|
79
|
*/
|
80
|
public BaseElement getFootNote(int index) {
|
81
|
WebElement footNoteElement = jsGetElement(SELECTOR_FOOTNOTE, index);
|
82
|
if(footNoteElement != null) {
|
83
|
return new BaseElement(footNoteElement);
|
84
|
} else {
|
85
|
return null;
|
86
|
}
|
87
|
}
|
88
|
|
89
|
|
90
|
public long countFootNotes() {
|
91
|
return jsCountElements(SELECTOR_FOOTNOTE);
|
92
|
}
|
93
|
|
94
|
public boolean hasFootNotes() {
|
95
|
return countFootNotes() > 0;
|
96
|
}
|
97
|
|
98
|
public List<BaseElement> getOriginalSourcesSections() {
|
99
|
if(originalSources == null) {
|
100
|
initSources();
|
101
|
}
|
102
|
return originalSources;
|
103
|
}
|
104
|
|
105
|
|
106
|
public DescriptionElementRepresentation getDescriptionElement(int index) {
|
107
|
WebElement descriptionElement = descriptionItemElements.get(index);
|
108
|
if(descriptionElement != null) {
|
109
|
if(elementTags.length > 1){
|
110
|
// it is a multipart element e.g. <dt></dt><dd></dd>
|
111
|
return new MultipartDescriptionElementRepresentation(
|
112
|
descriptionItemElements.toArray(new WebElement[descriptionItemElements.size()]));
|
113
|
} else {
|
114
|
return new DescriptionElementRepresentation(descriptionElement);
|
115
|
}
|
116
|
}
|
117
|
return null;
|
118
|
}
|
119
|
|
120
|
public String getFeatureType() {
|
121
|
return featureType;
|
122
|
}
|
123
|
|
124
|
|
125
|
/**
|
126
|
* @param driver TODO
|
127
|
* @param element
|
128
|
*/
|
129
|
public FeatureBlock(WebDriver driver, WebElement element, String enclosingTag, String ... elementTags) {
|
130
|
|
131
|
super(element);
|
132
|
logger.setLevel(Level.TRACE);
|
133
|
logger.trace("FeatureBlock() - constructor after super()");
|
134
|
|
135
|
this.driver = driver;
|
136
|
this.elementTags = elementTags;
|
137
|
|
138
|
WebElement descriptionElementsRepresentation = element.findElement(By.className("feature-block-elements"));
|
139
|
featureType = descriptionElementsRepresentation.getAttribute("id");
|
140
|
|
141
|
//TODO throw exception instead of making an assertion! selenium should have appropriate exceptions
|
142
|
assertEquals("Unexpected tag enclosing description element representations", enclosingTag, descriptionElementsRepresentation.getTagName());
|
143
|
|
144
|
logger.trace("FeatureBlock() - loading all elements ...");
|
145
|
descriptionItemElements = new ArrayList<WebElement>();
|
146
|
if(elementTags.length > 1){
|
147
|
|
148
|
// handle multipart elements e.g. <dt></dt><dd></dd>
|
149
|
HashMap<String, List<WebElement>> elementsByTag = new HashMap<String, List<WebElement>>();
|
150
|
Integer lastSize = null;
|
151
|
for (String elementTag : elementTags) {
|
152
|
List<WebElement> foundElements = descriptionElementsRepresentation.findElements(By.tagName(elementTag));
|
153
|
if(lastSize != null && foundElements.size() != lastSize){
|
154
|
throw new NoSuchElementException("Mulitpart element lists differ in size");
|
155
|
}
|
156
|
lastSize = foundElements.size();
|
157
|
elementsByTag.put(elementTag, foundElements);
|
158
|
}
|
159
|
|
160
|
for (int descriptionElementIndex = 0; descriptionElementIndex < lastSize; descriptionElementIndex++){
|
161
|
for (String elementTag : elementTags) {
|
162
|
descriptionItemElements.add(elementsByTag.get(elementTag).get(descriptionElementIndex));
|
163
|
}
|
164
|
// descriptionElements.add(new MultipartDescriptionElementRepresentation(descriptionItemElements.toArray(new WebElement[descriptionItemElements.size()])));
|
165
|
|
166
|
}
|
167
|
} else {
|
168
|
// handle single elements
|
169
|
String elementTag = elementTags[0];
|
170
|
for(WebElement el : descriptionElementsRepresentation.findElements(By.tagName( elementTag ))) {
|
171
|
descriptionItemElements.add(el);
|
172
|
// descriptionElements.add(new DescriptionElementRepresentation(el));
|
173
|
}
|
174
|
}
|
175
|
logger.trace("FeatureBlock() - loading all elements DONE");
|
176
|
|
177
|
}
|
178
|
|
179
|
/**
|
180
|
* @return
|
181
|
*/
|
182
|
private Long jsCountElements(String jQuerySelector) {
|
183
|
if(driver instanceof JavascriptExecutor) {
|
184
|
String blockId = getElement().getAttribute("id");
|
185
|
if(!blockId.startsWith("block-cdm-dataportal-feature-")) {
|
186
|
throw new IllegalStateException("The block with id " + blockId + " is not a proper feature block");
|
187
|
}
|
188
|
// NOTE: jQuery(document).ready() must not be used here, otherwise
|
189
|
// the executeScript() function will return null
|
190
|
String js = "var elementCnt = jQuery('#" + blockId + "')."
|
191
|
+ " find('" + jQuerySelector + "').length;"
|
192
|
// + " // console.log('count is ' + elementCnt);"
|
193
|
+ "return elementCnt;";
|
194
|
Object resultO = ((JavascriptExecutor) driver).executeScript(js);
|
195
|
logger.debug("FootNoteKeys count is " + resultO);
|
196
|
if(resultO != null) {
|
197
|
return Long.valueOf(resultO.toString());
|
198
|
}
|
199
|
return null;
|
200
|
}
|
201
|
throw new IllegalStateException("The driver must be a JavascriptExecutor");
|
202
|
}
|
203
|
|
204
|
/**
|
205
|
* @param elementIndex TODO
|
206
|
* @return
|
207
|
*/
|
208
|
private WebElement jsGetElement(String jQuerySelector, int elementIndex) {
|
209
|
if(driver instanceof JavascriptExecutor) {
|
210
|
String blockId = getElement().getAttribute("id");
|
211
|
if(!blockId.startsWith("block-cdm-dataportal-feature-")) {
|
212
|
throw new IllegalStateException("The block with id " + blockId + " is not a proper feature block");
|
213
|
}
|
214
|
// NOTE: jQuery(document).ready() must not be used here, otherwise
|
215
|
// the executeScript() function will return null
|
216
|
String js = "var elements = jQuery('#" + blockId + "')."
|
217
|
+ " find('" + jQuerySelector + "');"
|
218
|
// + "console.log(elements.length);"
|
219
|
+ "return elements[" + elementIndex +"];";
|
220
|
Object resultO = ((JavascriptExecutor) driver).executeScript(js);
|
221
|
logger.debug("FootNoteKeys count is " + resultO);
|
222
|
if(resultO instanceof WebElement) {
|
223
|
return (WebElement)resultO;
|
224
|
}
|
225
|
if(resultO instanceof List) {
|
226
|
throw new IllegalStateException("The selector '" + jQuerySelector + "' matches multiple elements, this is not allowed.");
|
227
|
}
|
228
|
return null;
|
229
|
}
|
230
|
throw new IllegalStateException("The driver must be a JavascriptExecutor");
|
231
|
}
|
232
|
|
233
|
/**
|
234
|
* @param element
|
235
|
*/
|
236
|
private void initSources() {
|
237
|
originalSources = new ArrayList<BaseElement>();
|
238
|
List<WebElement> sourcesList = getElement().findElements(By.className("sources"));
|
239
|
for(WebElement source : sourcesList) {
|
240
|
originalSources.add(new BaseElement(source));
|
241
|
}
|
242
|
}
|
243
|
|
244
|
/**
|
245
|
* @param indent
|
246
|
* @param computedFontSize
|
247
|
* @param expectedCssDisplay
|
248
|
* @param expectedListStyleType only applies if cssDisplay equals list-item
|
249
|
* @param expectedListStylePosition only applies if cssDisplay equals list-item
|
250
|
* @param expectedListStyleImage only applies if cssDisplay equals list-item
|
251
|
*/
|
252
|
public void testDescriptionElementLayout(int descriptionElementId, int indent, int computedFontSize
|
253
|
, String expectedCssDisplay, String expectedListStyleType, String expectedListStylePosition, String expectedListStyleImage) {
|
254
|
|
255
|
DescriptionElementRepresentation firstDescriptionElement = getDescriptionElement(descriptionElementId);
|
256
|
|
257
|
if(firstDescriptionElement instanceof MultipartDescriptionElementRepresentation){
|
258
|
int multipartElementIndex = 0;
|
259
|
firstDescriptionElement = ((MultipartDescriptionElementRepresentation)firstDescriptionElement).multipartElements.get(multipartElementIndex);
|
260
|
}
|
261
|
int parentX = getElement().getLocation().getX();
|
262
|
int elementX = firstDescriptionElement.getElement().getLocation().getX();
|
263
|
double elementPadLeft = pxSizeToDouble(firstDescriptionElement.getElement().getCssValue("padding-left"));
|
264
|
|
265
|
assertEquals(indent, elementX - parentX + elementPadLeft, PIXEL_TOLERANCE);
|
266
|
assertEquals("css font-size:", computedFontSize, firstDescriptionElement.getComputedFontSize(), 0.5);
|
267
|
assertEquals("css display:", expectedCssDisplay, firstDescriptionElement.getElement().getCssValue("display"));
|
268
|
|
269
|
if(expectedCssDisplay.equals("list-item")){
|
270
|
assertEquals("css list-style-position: ", expectedListStylePosition, firstDescriptionElement.getElement().getCssValue("list-style-position"));
|
271
|
assertEquals("css list-style-image: ", expectedListStyleImage, firstDescriptionElement.getElement().getCssValue("list-style-image"));
|
272
|
assertEquals("css list-style-type: ", expectedListStyleType, firstDescriptionElement.getElement().getCssValue("list-style-type"));
|
273
|
}
|
274
|
}
|
275
|
|
276
|
public List<GalleryImage> getGalleryMedia() {
|
277
|
List<GalleryImage> galleryImages = null; //getGalleryImages(getElement());
|
278
|
|
279
|
return galleryImages;
|
280
|
|
281
|
}
|
282
|
|
283
|
}
|