Project

General

Profile

Download (7.48 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
import org.openqa.selenium.support.ui.WebDriverWait;
19

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

    
24
public abstract class  PortalPage {
25

    
26
	/**
27
	 *
28
	 */
29
	public static final int WAIT_SECONDS = 25;
30

    
31
	public static final Logger logger = Logger.getLogger(PortalPage.class);
32

    
33
	protected final static String DRUPAL_PAGE_QUERY_BASE = "?q=";
34

    
35
	protected WebDriver driver;
36

    
37
	protected DataPortalContext context;
38

    
39
	protected final JUnitWebDriverWait wait;
40

    
41
	public WebDriverWait getWait() {
42
		return wait;
43
	}
44

    
45

    
46
	/**
47
	 * Implementations of this method will supply the relative
48
	 * path to the Drupal page. This path will usally have the form
49
	 * <code>cdm_dataportal/{nodetype}</code>. For example the taxon pages all
50
	 * have the page base <code>cdm_dataportal/taxon</code>
51
	 * @return
52
	 */
53
	protected abstract String getDrupalPageBase();
54

    
55
	private String drupalPagePath;
56

    
57
	protected URL pageUrl;
58

    
59
	// ==== WebElements === //
60

    
61
	@FindBy(id="cdm_dataportal.node")
62
	protected WebElement portalContent;
63

    
64
	@FindBy(tagName="title")
65
	@CacheLookup
66
	protected WebElement title;
67

    
68
	@FindBy(className="node")
69
	protected WebElement node;
70

    
71
	@FindBys({@FindBy(id="tabs-wrapper"), @FindBy(className="primary")})
72
	@CacheLookup
73
	protected WebElement primaryTabs;
74

    
75
	@FindBy(id="block-cdm_dataportal-2")
76
	@CacheLookup
77
	protected WebElement searchBlockElement;
78

    
79
	@FindBy(id="block-cdm_taxontree-cdm_tree")
80
	@CacheLookup
81
	protected WebElement classificationBrowserBlock;
82

    
83
	/**
84
	 * Creates a new PortaPage. Implementations of this class will provide the base path of the page by
85
	 * implementing the method {@link #getDrupalPageBase()}. The constructor argument <code>pagePathSuffix</code>
86
	 * specifies the specific page to navigate to. For example:
87
	 * <ol>
88
	 * <li>{@link #getDrupalPageBase()} returns <code>/cdm_dataportal/taxon</code></li>
89
	 * <li><code>pagePathSuffix</code> gives <code>7fe8a8b6-b0ba-4869-90b3-177b76c1753f</code></li>
90
	 * </ol>
91
	 * Both are combined to form the URL pathelement <code>/cdm_dataportal/taxon/7fe8a8b6-b0ba-4869-90b3-177b76c1753f</code>
92
	 *
93
	 *
94
	 * @param driver
95
	 * @param context
96
	 * @param pagePathSuffix
97
	 * @throws MalformedURLException
98
	 */
99
	public PortalPage(WebDriver driver, DataPortalContext context, String pagePathSuffix) throws MalformedURLException {
100

    
101
		this.driver = driver;
102

    
103
		this.context = context;
104

    
105
		this.wait = new JUnitWebDriverWait(driver, WAIT_SECONDS);
106

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

    
109
		this.pageUrl = new URL(context.getBaseUri().toString() + DRUPAL_PAGE_QUERY_BASE + drupalPagePath);
110

    
111
		// tell browser to navigate to the page
112
		driver.get(pageUrl.toString());
113

    
114
	    // This call sets the WebElement fields.
115
	    PageFactory.initElements(driver, this);
116

    
117
		logger.info("loading " + pageUrl);
118

    
119
	}
120

    
121
	/**
122
	 * Creates a new PortaPage at given URL location. An Exception is thrown if
123
	 * this URL is not matching the expected URL for the specific page type.
124
	 *
125
	 * @param driver
126
	 * @param context
127
	 * @param url
128
	 * @throws Exception
129
	 */
130
	public PortalPage(WebDriver driver, DataPortalContext context, URL url) throws Exception {
131

    
132
		this.driver = driver;
133

    
134
		this.context = context;
135

    
136
		this.wait = new JUnitWebDriverWait(driver, 25);
137

    
138
		this.pageUrl = new URL(context.getBaseUri().toString());
139

    
140
		// tell browser to navigate to the given URL
141
		driver.get(url.toString());
142

    
143
		if(!isOnPage()){
144
			throw new Exception("Not on the expected portal page ( current: " + driver.getCurrentUrl() + ", expected: " +  pageUrl + " )");
145
		}
146

    
147
		this.pageUrl = url;
148

    
149
		logger.info("loading " + pageUrl);
150

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

    
154
	}
155

    
156
	/**
157
	 * Creates a new PortaPage at the WebDrivers current URL location. An Exception is thrown if
158
	 * driver.getCurrentUrl() is not matching the expected URL for the specific page type.
159
	 *
160
	 * @param driver
161
	 * @param context
162
	 * @throws Exception
163
	 */
164
	public PortalPage(WebDriver driver, DataPortalContext context) throws Exception {
165

    
166
		this.driver = driver;
167

    
168
		this.context = context;
169

    
170
		this.wait = new JUnitWebDriverWait(driver, 25);
171

    
172
		// preliminary set the pageUrl to the base path of this page, this is used in the next setp to check if the
173
		// driver.getCurrentUrl() is a sub path of the base path
174
		this.pageUrl = new URL(context.getBaseUri().toString());
175

    
176
		if(!isOnPage()){
177
			throw new Exception("Not on the expected portal page ( current: " + driver.getCurrentUrl() + ", expected: " +  pageUrl + " )");
178
		}
179

    
180
		// now set the real URL
181
		this.pageUrl = new URL(driver.getCurrentUrl());
182

    
183
		logger.info("loading " + pageUrl);
184

    
185
	    // This call sets the WebElement fields.
186
	    PageFactory.initElements(driver, this);
187

    
188
	}
189

    
190
	/**
191
	 * @return
192
	 */
193
	protected boolean isOnPage() {
194
		return driver.getCurrentUrl().startsWith(pageUrl.toString());
195
	}
196

    
197
	public void get() {
198
		if(!driver.getCurrentUrl().equals(pageUrl.toString())){
199
			driver.get(pageUrl.toString());
200
			PageFactory.initElements(driver, this);
201
		}
202
	}
203

    
204
	public String getDrupalPagePath() {
205
		return drupalPagePath;
206
	}
207

    
208
	/**
209
	 * returns the string from the <code>title</code> tag.
210
	 * @return
211
	 */
212
	public String getTitle() {
213
		return title.getText();
214
	}
215

    
216
	/**
217
	 * returns the warning messages from the Drupal message box
218
	 * @return
219
	 */
220
	public String getWarnings() {
221
		return null; //TODO unimplemented
222
	}
223

    
224
	/**
225
	 * returns the error messages from the Drupal message box
226
	 * @return
227
	 */
228
	public String getErrors() {
229
		return null; //TODO unimplemented
230
	}
231

    
232
	public String getAuthorInformationText() {
233

    
234
		WebElement authorInformation = null;
235

    
236
		try {
237
			authorInformation  = node.findElement(By.className("submitted"));
238
		} catch (NoSuchElementException e) {
239
			// IGNORE //
240
		}
241

    
242

    
243
		if(authorInformation != null){
244
			return authorInformation.getText();
245
		} else {
246
			return null;
247
		}
248
	}
249

    
250
	public List<LinkElement> getPrimaryTabs(){
251
		List<LinkElement> tabs = new ArrayList<LinkElement>();
252
		List<WebElement> links = primaryTabs.findElements(By.tagName("a"));
253
		for(WebElement a : links) {
254
			WebElement renderedLink = a;
255
			if(renderedLink.isDisplayed()){
256
				tabs.add(new LinkElement(renderedLink));
257
			}
258
		}
259

    
260
		return tabs;
261
	}
262

    
263
	public void hover(WebElement element) {
264
		Actions actions = new Actions(driver);
265
		actions.moveToElement(element, 1, 1).perform();
266
		logger.debug("hovering");
267
	}
268

    
269

    
270
	/**
271
	 * Returns the current URL string from the {@link WebDriver}
272
	 * @return
273
	 */
274
	public URL getPageURL() {
275
		return pageUrl;
276
	}
277

    
278

    
279
	/**
280
	 * return the <code>scheme://domain:port</code> part of the initial url of this page.
281
	 * @return
282
	 */
283
	public String getInitialUrlBase() {
284
		return pageUrl.getProtocol() + "://" + pageUrl.getHost() + pageUrl.getPort();
285
	}
286

    
287
	public boolean equals(Object obj) {
288
		if (PortalPage.class.isAssignableFrom(obj.getClass())) {
289
			PortalPage page = (PortalPage) obj;
290
			return this.getPageURL().toString().equals(page.getPageURL().toString());
291

    
292
		} else {
293
			return false;
294
		}
295
	}
296

    
297

    
298
}
(3-3/6)