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 java.util.ArrayList;
|
13
|
import java.util.Arrays;
|
14
|
import java.util.List;
|
15
|
|
16
|
import org.apache.commons.lang.StringUtils;
|
17
|
import org.apache.log4j.Level;
|
18
|
import org.apache.log4j.Logger;
|
19
|
import org.openqa.selenium.By;
|
20
|
import org.openqa.selenium.NoSuchElementException;
|
21
|
import org.openqa.selenium.WebDriver;
|
22
|
import org.openqa.selenium.WebDriverException;
|
23
|
import org.openqa.selenium.WebElement;
|
24
|
import org.openqa.selenium.support.ui.ExpectedCondition;
|
25
|
import org.openqa.selenium.support.ui.WebDriverWait;
|
26
|
|
27
|
/**
|
28
|
* @author Andreas Kohlbecker
|
29
|
* @date Jul 1, 2011
|
30
|
*
|
31
|
*/
|
32
|
public class BaseElement {
|
33
|
|
34
|
public static final Logger logger = Logger.getLogger(BaseElement.class);
|
35
|
/**
|
36
|
* Default tolerance for testing sizes and positions
|
37
|
*/
|
38
|
protected static final double PIXEL_TOLERANCE = 0.5;
|
39
|
|
40
|
private final WebElement element;
|
41
|
|
42
|
private List<String> classAttributes = null;
|
43
|
|
44
|
private List<LinkElement> linksInElement = null;
|
45
|
|
46
|
List<String> linkTargets = null;
|
47
|
|
48
|
private String text = null;
|
49
|
|
50
|
|
51
|
public WebElement getElement() {
|
52
|
return element;
|
53
|
}
|
54
|
|
55
|
public String getText() {
|
56
|
if(text == null) {
|
57
|
text = element.getText();
|
58
|
}
|
59
|
return text;
|
60
|
}
|
61
|
|
62
|
public List<String> getClassAttributes() {
|
63
|
return classAttributes;
|
64
|
}
|
65
|
|
66
|
void setClassAttributes(List<String> classAttributes) {
|
67
|
this.classAttributes = classAttributes;
|
68
|
}
|
69
|
|
70
|
public double getComputedFontSize(){
|
71
|
return pxSizeToDouble(getElement().getCssValue("font-size") );
|
72
|
}
|
73
|
|
74
|
public static double pxSizeToDouble(String pxValue){
|
75
|
return Double.valueOf( pxValue.replaceAll("[a-zA-Z]", "") );
|
76
|
}
|
77
|
|
78
|
/**
|
79
|
* Finds and returns links of the form <a href="LINK" /> in this element or in any child element.
|
80
|
*
|
81
|
* @return an array of {@link LinkElement} objects, the array is empty if no link has been found
|
82
|
*/
|
83
|
public List<LinkElement> getLinksInElement() {
|
84
|
if(linksInElement == null){
|
85
|
linksInElement = new ArrayList<LinkElement>();
|
86
|
|
87
|
if(getElement().getTagName().equals("a") && getElement().getAttribute("href") != null && getElement().getAttribute("href").length() > 0){
|
88
|
// BaseElement is link itself
|
89
|
linksInElement.add(new LinkElement(getElement()));
|
90
|
} else {
|
91
|
// look for links in sub elements
|
92
|
List<WebElement> links = getElement().findElements(By.xpath("./a[@href]"));
|
93
|
for (WebElement link : links) {
|
94
|
linksInElement.add(new LinkElement(link));
|
95
|
}
|
96
|
}
|
97
|
}
|
98
|
return linksInElement;
|
99
|
}
|
100
|
|
101
|
/**
|
102
|
*
|
103
|
* @param driver the currently used WebDriver instance
|
104
|
* @return
|
105
|
*/
|
106
|
public List<String> getLinkTargets(WebDriver driver){
|
107
|
|
108
|
if(linkTargets == null){
|
109
|
linkTargets = new ArrayList<String>();
|
110
|
if(getElement().getTagName().equals("a") && getElement().getAttribute("target") != null && getElement().getAttribute("target").length() > 0){
|
111
|
linkTargets.add(getElement().getAttribute("target"));
|
112
|
} else {
|
113
|
try {
|
114
|
WebDriverWait wait = new WebDriverWait(driver, 5);
|
115
|
|
116
|
List<WebElement> anchorTags = wait.until(new ExpectedCondition<List<WebElement>>(){
|
117
|
@Override
|
118
|
public List<WebElement> apply(WebDriver d) {
|
119
|
return d.findElements(By.tagName("a"));
|
120
|
}
|
121
|
}
|
122
|
);
|
123
|
|
124
|
WebElement linkWithTarget = null;
|
125
|
if(anchorTags.size() > 0){
|
126
|
if(anchorTags.get(0).getAttribute("target") != null) {
|
127
|
linkWithTarget = anchorTags.get(0);
|
128
|
}
|
129
|
}
|
130
|
|
131
|
if(linkWithTarget != null){ // assuming that there is only one
|
132
|
linkTargets.add(linkWithTarget.getAttribute("target"));
|
133
|
}
|
134
|
} catch (NoSuchElementException e) {
|
135
|
logger.debug("No target window found");
|
136
|
/* IGNORE */
|
137
|
} catch (WebDriverException e) {
|
138
|
logger.debug("timed out", e);
|
139
|
/* IGNORE */
|
140
|
}
|
141
|
}
|
142
|
}
|
143
|
return linkTargets;
|
144
|
}
|
145
|
|
146
|
/**
|
147
|
* @param element
|
148
|
*/
|
149
|
public BaseElement(WebElement element) {
|
150
|
|
151
|
logger.setLevel(Level.TRACE);
|
152
|
logger.trace("BaseElement() - constructor");
|
153
|
this.element = element;
|
154
|
|
155
|
// read and tokenize the class attribute
|
156
|
if (element.getAttribute("class") != null) {
|
157
|
String[] classTokens = element.getAttribute("class").split("\\s");
|
158
|
setClassAttributes(Arrays.asList(classTokens));
|
159
|
logger.trace("BaseElement() - class attribute loaded");
|
160
|
}
|
161
|
}
|
162
|
|
163
|
/**
|
164
|
* @return
|
165
|
*/
|
166
|
@Override
|
167
|
public String toString() {
|
168
|
return this.getClass().getSimpleName() + "<" + this.getElement().getTagName() + ">" ;
|
169
|
}
|
170
|
|
171
|
/**
|
172
|
*
|
173
|
* @return
|
174
|
*/
|
175
|
public String toStringWithLinks() {
|
176
|
|
177
|
StringBuilder links = new StringBuilder();
|
178
|
for(LinkElement linkElement : getLinksInElement()){
|
179
|
if(links.length() > 0){
|
180
|
links.append(", ");
|
181
|
}
|
182
|
links.append(linkElement.getUrl());
|
183
|
}
|
184
|
String classAttribute = getElement().getAttribute("class") ;
|
185
|
if(classAttribute == null){
|
186
|
classAttribute = "";
|
187
|
}
|
188
|
String textSnipped = getText();
|
189
|
if(textSnipped.length() > 50){
|
190
|
textSnipped = textSnipped.substring(0, 50) + "...";
|
191
|
}
|
192
|
String targets = "LinkTargets UN-INITIALIZED";
|
193
|
if(linkTargets != null) {
|
194
|
targets = linkTargets.isEmpty()? "" : ", LinkTargets: '" + StringUtils.join(linkTargets.toArray(), ", ") + "'";
|
195
|
}
|
196
|
return "<" + getElement().getTagName() + " class=\""+ getElement().getAttribute("class")+ "\"/>" + textSnipped + " ;Links:" + links + targets;
|
197
|
}
|
198
|
|
199
|
}
|