Project

General

Profile

Download (6.77 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.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
import eu.etaxonomy.dataportal.ElementUtils;
27

    
28
/**
29
 * @author Andreas Kohlbecker
30
 * @since Jul 1, 2011
31
 *
32
 */
33
public class BaseElement {
34

    
35
    public static final Logger logger = Logger.getLogger(BaseElement.class);
36
    /**
37
     * Default tolerance for testing sizes and positions
38
     */
39
    protected static final double PIXEL_TOLERANCE = 0.5;
40

    
41
    private final WebElement element;
42

    
43
    private List<String> classAttributes = null;
44

    
45
    private List<LinkElement> linksInElement = null;
46

    
47
    List<String> linkTargets = null;
48

    
49
    private String text = null;
50

    
51

    
52
    public WebElement getElement() {
53
        return element;
54
    }
55

    
56
    public String getText() {
57
        if(text == null) {
58
            text = element.getText();
59
        }
60
        return text;
61
    }
62

    
63
    public List<String> getClassAttributes() {
64
        return classAttributes;
65
    }
66

    
67
    void setClassAttributes(List<String> classAttributes) {
68
        this.classAttributes = classAttributes;
69
    }
70

    
71
    public double getComputedFontSize(){
72
        return pxSizeToDouble(getElement().getCssValue("font-size") );
73
    }
74

    
75
    public static double pxSizeToDouble(String pxValue){
76
        return Double.valueOf( pxValue.replaceAll("[a-zA-Z]", "") );
77
    }
78

    
79
    /**
80
     * Finds and returns links of the form &lt;a href="LINK" /&gt; in this element or in any child element.
81
     *
82
     * @return an array of {@link LinkElement} objects, the array is empty if no link has been found
83
     */
84
    public List<LinkElement> getLinksInElement() {
85
        if(linksInElement == null){
86
            linksInElement = new ArrayList<LinkElement>();
87

    
88
            if(getElement().getTagName().equals("a") && getElement().getAttribute("href") != null && getElement().getAttribute("href").length() > 0){
89
                // BaseElement is link itself
90
                linksInElement.add(new LinkElement(getElement()));
91
            } else {
92
                // look for links in sub elements
93
                List<WebElement> links = getElement().findElements(By.xpath(".//a[@href]"));
94
                for (WebElement link : links) {
95
                    linksInElement.add(new LinkElement(link));
96
                }
97
            }
98
        }
99
        return linksInElement;
100
    }
101

    
102
    public List<LinkElement> getFootNoteKeys(){
103
        return ElementUtils.linkElementsFromFootNoteKeyListElements(
104
                getElement().findElements(By.xpath("//*[contains(@class, 'footnote-key')]/a"))
105
                );
106
    }
107

    
108
    public List<BaseElement> getFootNotes(){
109
        return ElementUtils.baseElementsFromFootNoteListElements(
110
                getElement().findElements(By.xpath("//*[contains(@class, 'footnotes')]/span[contains(@class, 'footnote')]"))
111
                );
112
    }
113

    
114
    /**
115
     *
116
     * @param driver the currently used  WebDriver instance
117
     * @return list of linktargets
118
     */
119
    public List<String> getLinkTargets(WebDriver driver){
120

    
121
        if(linkTargets == null){
122
            linkTargets = new ArrayList<String>();
123
            if(getElement().getTagName().equals("a") && getElement().getAttribute("target") != null  && getElement().getAttribute("target").length() > 0){
124
                linkTargets.add(getElement().getAttribute("target"));
125
            } else {
126
                try {
127
                    WebDriverWait wait = new WebDriverWait(driver, 5);
128

    
129
                    List<WebElement> anchorTags = wait.until(new ExpectedCondition<List<WebElement>>(){
130
                        @Override
131
                        public List<WebElement> apply(WebDriver d) {
132
                            return d.findElements(By.tagName("a"));
133
                        }
134
                      }
135
                    );
136

    
137
                    WebElement linkWithTarget = null;
138
                    if(anchorTags.size() > 0){
139
                        if(anchorTags.get(0).getAttribute("target") != null) {
140
                            linkWithTarget = anchorTags.get(0);
141
                        }
142
                    }
143

    
144
                    if(linkWithTarget != null){ // assuming that there is only one
145
                        linkTargets.add(linkWithTarget.getAttribute("target"));
146
                    }
147
                } catch (NoSuchElementException e) {
148
                    logger.debug("No target window found");
149
                    /* IGNORE */
150
                } catch (WebDriverException e) {
151
                    logger.debug("timed out", e);
152
                    /* IGNORE */
153
                }
154
            }
155
        }
156
        return linkTargets;
157
    }
158

    
159

    
160
    public BaseElement(WebElement element) {
161

    
162
        logger.setLevel(Level.TRACE);
163
        logger.trace("BaseElement() - constructor");
164
        this.element = element;
165

    
166
        // read and tokenize the class attribute
167
        if (element.getAttribute("class") != null) {
168
            String[] classTokens = element.getAttribute("class").split("\\s");
169
            setClassAttributes(Arrays.asList(classTokens));
170
            logger.trace("BaseElement() - class attribute loaded");
171
        }
172
    }
173

    
174

    
175
    @Override
176
    public String toString() {
177
        return this.getClass().getSimpleName() + "<" + this.getElement().getTagName() + ">" ;
178
    }
179

    
180
    public String toStringWithLinks() {
181

    
182
        StringBuilder links = new StringBuilder();
183
        for(LinkElement linkElement : getLinksInElement()){
184
            if(links.length() > 0){
185
                links.append(", ");
186
            }
187
            links.append(linkElement.getUrl());
188
        }
189
        String classAttribute = getElement().getAttribute("class") ;
190
        if(classAttribute == null){
191
            classAttribute = "";
192
        }
193
        String textSnipped = getText();
194
        if(textSnipped.length() > 50){
195
            textSnipped = textSnipped.substring(0, 50) + "...";
196
        }
197
        String targets = "LinkTargets UN-INITIALIZED";
198
        if(linkTargets != null) {
199
            targets = linkTargets.isEmpty()? "" : ", LinkTargets: '" + StringUtils.join(linkTargets.toArray(),	", ") + "'";
200
        }
201
        return "<" + getElement().getTagName() + " class=\""+ getElement().getAttribute("class")+ "\"/>" + textSnipped + " ;Links:" + links + targets;
202
    }
203

    
204
}
(1-1/20)