Project

General

Profile

Download (6.53 KB) Statistics
| Branch: | Tag: | Revision:
1
package eu.etaxonomy.dataportal.pages;
2

    
3
import java.net.MalformedURLException;
4
import java.net.URL;
5
import java.util.ArrayList;
6
import java.util.List;
7

    
8
import org.apache.log4j.Logger;
9
import org.openqa.selenium.By;
10
import org.openqa.selenium.NoSuchElementException;
11
import org.openqa.selenium.WebDriver;
12
import org.openqa.selenium.WebElement;
13
import org.openqa.selenium.interactions.Actions;
14
import org.openqa.selenium.support.CacheLookup;
15
import org.openqa.selenium.support.FindBy;
16
import org.openqa.selenium.support.FindBys;
17
import org.openqa.selenium.support.PageFactory;
18

    
19
import eu.etaxonomy.dataportal.DataPortalContext;
20
import eu.etaxonomy.dataportal.elements.LinkElement;
21
import eu.etaxonomy.dataportal.selenium.JUnitWebDriverWait;
22

    
23
public abstract class  PortalPage {
24

    
25
	public static final Logger logger = Logger.getLogger(PortalPage.class);
26

    
27
	protected final static String DRUPAL_PAGE_QUERY_BASE = "?q=";
28

    
29
	protected WebDriver driver;
30

    
31
	protected final JUnitWebDriverWait wait;
32

    
33

    
34
	/**
35
	 * Implementations of this method will supply the relative
36
	 * path to the Drupal page. This path will usally have the form
37
	 * <code>cdm_dataportal/{nodetype}</code>. For example the taxon pages all
38
	 * have the page base <code>cdm_dataportal/taxon</code>
39
	 * @return
40
	 */
41
	protected abstract String getDrupalPageBase();
42

    
43
	private String drupalPagePath;
44

    
45
	protected URL pageUrl;
46

    
47
	// ==== WebElements === //
48

    
49
	@FindBy(id="cdm_dataportal.node")
50
	protected WebElement portalContent;
51

    
52
	@FindBy(tagName="title")
53
	@CacheLookup
54
	protected WebElement title;
55

    
56
	@FindBy(className="node")
57
	protected WebElement node;
58

    
59

    
60
	@FindBys({@FindBy(id="tabs-wrapper"), @FindBy(className="primary")})
61
	@CacheLookup
62
	protected WebElement primaryTabs;
63

    
64

    
65
	/**
66
	 * Creates a new PortaPage. Implementations of this class will provide the base path of the page by
67
	 * implementing the method {@link #getDrupalPageBase()}. The constructor argument <code>pagePathSuffix</code>
68
	 * specifies the specific page to navigate to. For example:
69
	 * <ol>
70
	 * <li>{@link #getDrupalPageBase()} returns <code>/cdm_dataportal/taxon</code></li>
71
	 * <li><code>pagePathSuffix</code> gives <code>7fe8a8b6-b0ba-4869-90b3-177b76c1753f</code></li>
72
	 * </ol>
73
	 * Both are combined to form the URL pathelement <code>/cdm_dataportal/taxon/7fe8a8b6-b0ba-4869-90b3-177b76c1753f</code>
74
	 *
75
	 *
76
	 * @param driver
77
	 * @param context
78
	 * @param pagePathSuffix
79
	 * @throws MalformedURLException
80
	 */
81
	public PortalPage(WebDriver driver, DataPortalContext context, String pagePathSuffix) throws MalformedURLException {
82

    
83
		this.driver = driver;
84

    
85
		this.wait = new JUnitWebDriverWait(driver, 25);
86

    
87
		this.drupalPagePath = getDrupalPageBase() + (pagePathSuffix != null ? "/" + pagePathSuffix: "");
88

    
89
		this.pageUrl = new URL(context.getBaseUri().toString() + DRUPAL_PAGE_QUERY_BASE + drupalPagePath);
90

    
91
		// tell browser to navigate to the page
92
		driver.get(pageUrl.toString());
93

    
94
	    // This call sets the WebElement fields.
95
	    PageFactory.initElements(driver, this);
96

    
97
		logger.info("loading " + pageUrl);
98

    
99
	}
100

    
101
	/**
102
	 * Creates a new PortaPage at given URL location. An Exception is thrown if
103
	 * this URL is not matching the expected URL for the specific page type.
104
	 *
105
	 * @param driver
106
	 * @param context
107
	 * @param url
108
	 * @throws Exception
109
	 */
110
	public PortalPage(WebDriver driver, DataPortalContext context, URL url) throws Exception {
111
		this.driver = driver;
112

    
113
		this.wait = new JUnitWebDriverWait(driver, 25);
114

    
115
		this.pageUrl = new URL(context.getBaseUri().toString() + DRUPAL_PAGE_QUERY_BASE + getDrupalPageBase());
116

    
117
		// tell browser to navigate to the given URL
118
		driver.get(url.toString());
119

    
120
		if(!isOnPage()){
121
			throw new Exception("Not on the expected portal page ( current: " + driver.getCurrentUrl() + ", expected: " +  pageUrl + " )");
122
		}
123

    
124
		this.pageUrl = url;
125

    
126
	    // This call sets the WebElement fields.
127
	    PageFactory.initElements(driver, this);
128

    
129
		logger.info("loading " + pageUrl);
130
	}
131

    
132
	/**
133
	 * Creates a new PortaPage at the WebDrivers current URL location. An Exception is thrown if
134
	 * driver.getCurrentUrl() is not matching the expected URL for the specific page type.
135
	 *
136
	 * @param driver
137
	 * @param context
138
	 * @throws Exception
139
	 */
140
	public PortalPage(WebDriver driver, DataPortalContext context) throws Exception {
141
		this.driver = driver;
142

    
143
		this.wait = new JUnitWebDriverWait(driver, 25);
144

    
145
		this.pageUrl = new URL(context.getBaseUri().toString() + DRUPAL_PAGE_QUERY_BASE + getDrupalPageBase());
146

    
147
		if(!isOnPage()){
148
			throw new Exception("Not on the expected portal page ( current: " + driver.getCurrentUrl() + ", expected: " +  pageUrl + " )");
149
		}
150

    
151
	    // This call sets the WebElement fields.
152
	    PageFactory.initElements(driver, this);
153

    
154
		logger.info("loading " + pageUrl);
155
	}
156

    
157
	/**
158
	 * @return
159
	 */
160
	protected boolean isOnPage() {
161
		return driver.getCurrentUrl().startsWith(pageUrl.toString());
162
	}
163

    
164
	public void get() {
165
		if(!driver.getCurrentUrl().equals(pageUrl.toString())){
166
			driver.get(pageUrl.toString());
167
			PageFactory.initElements(driver, this);
168
		}
169
	}
170

    
171
	public String getDrupalPagePath() {
172
		return drupalPagePath;
173
	}
174

    
175
	/**
176
	 * returns the string from the <code>title</code> tag.
177
	 * @return
178
	 */
179
	public String getTitle() {
180
		return title.getText();
181
	}
182

    
183
	public String getAuthorInformationText() {
184

    
185
		WebElement authorInformation = null;
186

    
187
		try {
188
			authorInformation  = node.findElement(By.className("submitted"));
189
		} catch (NoSuchElementException e) {
190
			// IGNORE //
191
		}
192

    
193

    
194
		if(authorInformation != null){
195
			return authorInformation.getText();
196
		} else {
197
			return null;
198
		}
199
	}
200

    
201
	public List<LinkElement> getPrimaryTabs(){
202
		List<LinkElement> tabs = new ArrayList<LinkElement>();
203
		List<WebElement> links = primaryTabs.findElements(By.tagName("a"));
204
		for(WebElement a : links) {
205
			WebElement renderedLink = a;
206
			if(renderedLink.isDisplayed()){
207
				tabs.add(new LinkElement(renderedLink));
208
			}
209
		}
210

    
211
		return tabs;
212
	}
213

    
214
	public void hover(WebElement element) {
215
		Actions actions = new Actions(driver);
216
		actions.moveToElement(element, 1, 1).perform();
217
		logger.debug("hovering");
218
	}
219

    
220

    
221
	/**
222
	 * Returns the current URL string from the {@link WebDriver}
223
	 * @return
224
	 */
225
	public String getURL() {
226
		return driver.getCurrentUrl();
227
	}
228

    
229

    
230
	/**
231
	 * return the <code>scheme://domain:port</code> part of the initial url of this page.
232
	 * @return
233
	 */
234
	public String getInitialUrlBase() {
235
		return pageUrl.getProtocol() + "://" + pageUrl.getHost() + pageUrl.getPort();
236
	}
237

    
238
	public boolean equals(Object obj) {
239
		if (PortalPage.class.isAssignableFrom(obj.getClass())) {
240
			PortalPage page = (PortalPage) obj;
241
			return this.getURL().equals(page.getURL());
242

    
243
		} else {
244
			return false;
245
		}
246
	}
247

    
248

    
249
}
(3-3/4)