Project

General

Profile

Download (6.93 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.Logger;
17
import org.openqa.selenium.By;
18
import org.openqa.selenium.NoSuchElementException;
19
import org.openqa.selenium.WebDriver;
20
import org.openqa.selenium.WebDriverException;
21
import org.openqa.selenium.WebElement;
22
import org.openqa.selenium.support.ui.ExpectedCondition;
23
import org.openqa.selenium.support.ui.WebDriverWait;
24

    
25
import eu.etaxonomy.dataportal.ElementUtils;
26

    
27
/**
28
 * @author Andreas Kohlbecker
29
 * @since 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 &lt;a href="LINK" /&gt; 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
    public List<LinkElement> getFootNoteKeys(){
102
        return ElementUtils.linkElementsFromFootNoteKeyListElements(
103
                getElement().findElements(By.xpath(".//*[contains(@class, 'footnote-key')]/a"))
104
                );
105
    }
106

    
107
    public List<BaseElement> getFootNotes(){
108
        return ElementUtils.baseElementsFromFootNoteListElements(
109
                // NOTE: the training space character in 'footnote ' is important. Without it would also match the footnote-anchor!
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.trace("BaseElement() - constructor");
163
        this.element = element;
164

    
165
        logger.debug("wrapping " + ElementUtils.webElementTagToMarkup(getElement()));
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() + ":" + ElementUtils.webElementTagToMarkup(getElement()) ;
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/23)