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