Project

General

Profile

Download (6.11 KB) Statistics
| Branch: | Tag: | Revision:
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.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
 * @date 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 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
        return text;
56
    }
57

    
58
    public List<String> getClassAttributes() {
59
        return classAttributes;
60
    }
61

    
62
    void setClassAttributes(List<String> classAttributes) {
63
        this.classAttributes = classAttributes;
64
    }
65

    
66
    public double getComputedFontSize(){
67
        return pxSizeToDouble(getElement().getCssValue("font-size") );
68
    }
69

    
70
    public static double pxSizeToDouble(String pxValue){
71
        return Double.valueOf( pxValue.replaceAll("[a-zA-Z]", "") );
72
    }
73

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

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

    
97
    /**
98
     *
99
     * @param driver the currently used  WebDriver instance
100
     * @return
101
     */
102
    public List<String> getLinkTargets(WebDriver driver){
103

    
104
        if(linkTargets == null){
105
            linkTargets = new ArrayList<String>();
106
            if(getElement().getTagName().equals("a") && getElement().getAttribute("target") != null  && getElement().getAttribute("target").length() > 0){
107
                linkTargets.add(getElement().getAttribute("target"));
108
            } else {
109
                try {
110
                    WebDriverWait wait = new WebDriverWait(driver, 5);
111

    
112
                    List<WebElement> anchorTags = wait.until(new ExpectedCondition<List<WebElement>>(){
113
                        @Override
114
                        public List<WebElement> apply(WebDriver d) {
115
                            return d.findElements(By.tagName("a"));
116
                        }
117
                      }
118
                    );
119

    
120
                    WebElement linkWithTarget = null;
121
                    if(anchorTags.size() > 0){
122
                        if(anchorTags.get(0).getAttribute("target") != null) {
123
                            linkWithTarget = anchorTags.get(0);
124
                        }
125
                    }
126

    
127
                    if(linkWithTarget != null){ // assuming that there is only one
128
                        linkTargets.add(linkWithTarget.getAttribute("target"));
129
                    }
130
                } catch (NoSuchElementException e) {
131
                    logger.debug("No target window found");
132
                    /* IGNORE */
133
                } catch (WebDriverException e) {
134
                    logger.debug("timed out", e);
135
                    /* IGNORE */
136
                }
137
            }
138
        }
139
        return linkTargets;
140
    }
141

    
142
    /**
143
     * @param element
144
     */
145
    public BaseElement(WebElement element) {
146

    
147
        this.element = element;
148

    
149
        // read text
150
        text = element.getText();
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
        }
157
    }
158

    
159
    /**
160
     * @return
161
     */
162
    @Override
163
    public String toString() {
164
        return this.getClass().getSimpleName() + "<" + this.getElement().getTagName() + ">" ;
165
    }
166

    
167
    /**
168
     *
169
     * @return
170
     */
171
    public String toStringWithLinks() {
172

    
173
        StringBuilder links = new StringBuilder();
174
        for(LinkElement linkElement : getLinksInElement()){
175
            if(links.length() > 0){
176
                links.append(", ");
177
            }
178
            links.append(linkElement.getUrl());
179
        }
180
        String classAttribute = getElement().getAttribute("class") ;
181
        if(classAttribute == null){
182
            classAttribute = "";
183
        }
184
        String textSnipped = getText();
185
        if(textSnipped.length() > 50){
186
            textSnipped = textSnipped.substring(0, 50) + "...";
187
        }
188
        String targets = "LinkTargets UN-INITIALIZED";
189
        if(linkTargets != null) {
190
            targets = linkTargets.isEmpty()? "" : ", LinkTargets: '" + StringUtils.join(linkTargets.toArray(),	", ") + "'";
191
        }
192
        return "<" + getElement().getTagName() + " class=\""+ getElement().getAttribute("class")+ "\"/>" + textSnipped + " ;Links:" + links + targets;
193
    }
194

    
195
}
(1-1/15)