Project

General

Profile

Download (2.35 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.openqa.selenium.By;
17
import org.openqa.selenium.WebElement;
18

    
19
/**
20
 * @author andreas
21
 * @date Jul 1, 2011
22
 *
23
 */
24
public class BaseElement {
25

    
26

    
27
	/**
28
	 * Default tolerance for testing sizes and positions
29
	 */
30
	protected static final double PIXEL_TOLERANCE = 0.5;
31

    
32
	private WebElement element;
33

    
34
	private List<String> classAttributes = null;
35

    
36
	private List<LinkElement> linksInElement = null;
37

    
38
	private String text = null;
39

    
40

    
41
	public WebElement getElement() {
42
		return element;
43
	}
44

    
45
	public String getText() {
46
		return text;
47
	}
48

    
49
	public List<String> getClassAttributes() {
50
		return classAttributes;
51
	}
52

    
53
	void setClassAttributes(List<String> classAttributes) {
54
		this.classAttributes = classAttributes;
55
	}
56

    
57
	public double getComputedFontSize(){
58
		return pxSizeToDouble(getElement().getCssValue("font-size") );
59
	}
60

    
61
	public static double pxSizeToDouble(String pxValue){
62
		return Double.valueOf( pxValue.replaceAll("[a-zA-Z]", "") );
63
	}
64

    
65
	public List<LinkElement> getLinksInElement() {
66
		if(linksInElement == null){
67
			linksInElement = new ArrayList<LinkElement>();
68

    
69
			if(getElement().getTagName().equals("a") && getElement().getAttribute("href") == null){
70
				// BaseElement is link itself
71
				linksInElement.add(new LinkElement(getElement()));
72
			} else {
73
				// look for links in subelements
74
				List<WebElement> links = getElement().findElements(By.xpath("./a[@href]"));
75
				for (WebElement link : links) {
76
					linksInElement.add(new LinkElement(link));
77
				}
78

    
79
			}
80
		}
81
		return linksInElement;
82
	}
83

    
84
	/**
85
	 * @param element
86
	 */
87
	public BaseElement(WebElement element) {
88

    
89
		this.element = element;
90

    
91
		// read text
92
		text = element.getText();
93

    
94
		// read and tokenize the class attribute
95
		if (element.getAttribute("class") != null) {
96
			String[] classTokens = element.getAttribute("class").split("\\s");
97
			setClassAttributes(Arrays.asList(classTokens));
98
		}
99
	}
100

    
101
	/**
102
	 * @return
103
	 */
104
	public String toSting() {
105
		return this.getClass().getSimpleName() + "<" + this.getElement().getTagName() + ">" ;
106
	}
107

    
108

    
109
}
(1-1/13)