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 DataPortalContext context;
|
32
|
|
33
|
protected final JUnitWebDriverWait wait;
|
34
|
|
35
|
|
36
|
/**
|
37
|
* Implementations of this method will supply the relative
|
38
|
* path to the Drupal page. This path will usally have the form
|
39
|
* <code>cdm_dataportal/{nodetype}</code>. For example the taxon pages all
|
40
|
* have the page base <code>cdm_dataportal/taxon</code>
|
41
|
* @return
|
42
|
*/
|
43
|
protected abstract String getDrupalPageBase();
|
44
|
|
45
|
private String drupalPagePath;
|
46
|
|
47
|
protected URL pageUrl;
|
48
|
|
49
|
// ==== WebElements === //
|
50
|
|
51
|
@FindBy(id="cdm_dataportal.node")
|
52
|
protected WebElement portalContent;
|
53
|
|
54
|
@FindBy(tagName="title")
|
55
|
@CacheLookup
|
56
|
protected WebElement title;
|
57
|
|
58
|
@FindBy(className="node")
|
59
|
protected WebElement node;
|
60
|
|
61
|
@FindBys({@FindBy(id="tabs-wrapper"), @FindBy(className="primary")})
|
62
|
@CacheLookup
|
63
|
protected WebElement primaryTabs;
|
64
|
|
65
|
@FindBy(id="block-cdm_dataportal-2")
|
66
|
@CacheLookup
|
67
|
protected WebElement searchBlockElement;
|
68
|
|
69
|
@FindBy(id="block-cdm_taxontree-cdm_tree")
|
70
|
@CacheLookup
|
71
|
protected WebElement classificationBrowserBlock;
|
72
|
|
73
|
/**
|
74
|
* Creates a new PortaPage. Implementations of this class will provide the base path of the page by
|
75
|
* implementing the method {@link #getDrupalPageBase()}. The constructor argument <code>pagePathSuffix</code>
|
76
|
* specifies the specific page to navigate to. For example:
|
77
|
* <ol>
|
78
|
* <li>{@link #getDrupalPageBase()} returns <code>/cdm_dataportal/taxon</code></li>
|
79
|
* <li><code>pagePathSuffix</code> gives <code>7fe8a8b6-b0ba-4869-90b3-177b76c1753f</code></li>
|
80
|
* </ol>
|
81
|
* Both are combined to form the URL pathelement <code>/cdm_dataportal/taxon/7fe8a8b6-b0ba-4869-90b3-177b76c1753f</code>
|
82
|
*
|
83
|
*
|
84
|
* @param driver
|
85
|
* @param context
|
86
|
* @param pagePathSuffix
|
87
|
* @throws MalformedURLException
|
88
|
*/
|
89
|
public PortalPage(WebDriver driver, DataPortalContext context, String pagePathSuffix) throws MalformedURLException {
|
90
|
|
91
|
this.driver = driver;
|
92
|
|
93
|
this.context = context;
|
94
|
|
95
|
this.wait = new JUnitWebDriverWait(driver, 25);
|
96
|
|
97
|
this.drupalPagePath = getDrupalPageBase() + (pagePathSuffix != null ? "/" + pagePathSuffix: "");
|
98
|
|
99
|
this.pageUrl = new URL(context.getBaseUri().toString() + DRUPAL_PAGE_QUERY_BASE + drupalPagePath);
|
100
|
|
101
|
// tell browser to navigate to the page
|
102
|
driver.get(pageUrl.toString());
|
103
|
|
104
|
// This call sets the WebElement fields.
|
105
|
PageFactory.initElements(driver, this);
|
106
|
|
107
|
logger.info("loading " + pageUrl);
|
108
|
|
109
|
}
|
110
|
|
111
|
/**
|
112
|
* Creates a new PortaPage at given URL location. An Exception is thrown if
|
113
|
* this URL is not matching the expected URL for the specific page type.
|
114
|
*
|
115
|
* @param driver
|
116
|
* @param context
|
117
|
* @param url
|
118
|
* @throws Exception
|
119
|
*/
|
120
|
public PortalPage(WebDriver driver, DataPortalContext context, URL url) throws Exception {
|
121
|
|
122
|
this.driver = driver;
|
123
|
|
124
|
this.context = context;
|
125
|
|
126
|
this.wait = new JUnitWebDriverWait(driver, 25);
|
127
|
|
128
|
this.pageUrl = new URL(context.getBaseUri().toString());
|
129
|
|
130
|
// tell browser to navigate to the given URL
|
131
|
driver.get(url.toString());
|
132
|
|
133
|
if(!isOnPage()){
|
134
|
throw new Exception("Not on the expected portal page ( current: " + driver.getCurrentUrl() + ", expected: " + pageUrl + " )");
|
135
|
}
|
136
|
|
137
|
this.pageUrl = url;
|
138
|
|
139
|
logger.info("loading " + pageUrl);
|
140
|
|
141
|
// This call sets the WebElement fields.
|
142
|
PageFactory.initElements(driver, this);
|
143
|
|
144
|
}
|
145
|
|
146
|
/**
|
147
|
* Creates a new PortaPage at the WebDrivers current URL location. An Exception is thrown if
|
148
|
* driver.getCurrentUrl() is not matching the expected URL for the specific page type.
|
149
|
*
|
150
|
* @param driver
|
151
|
* @param context
|
152
|
* @throws Exception
|
153
|
*/
|
154
|
public PortalPage(WebDriver driver, DataPortalContext context) throws Exception {
|
155
|
|
156
|
this.driver = driver;
|
157
|
|
158
|
this.context = context;
|
159
|
|
160
|
this.wait = new JUnitWebDriverWait(driver, 25);
|
161
|
|
162
|
// preliminary set the pageUrl to the base path of this page, this is used in the next setp to check if the
|
163
|
// driver.getCurrentUrl() is a sub path of the base path
|
164
|
this.pageUrl = new URL(context.getBaseUri().toString());
|
165
|
|
166
|
if(!isOnPage()){
|
167
|
throw new Exception("Not on the expected portal page ( current: " + driver.getCurrentUrl() + ", expected: " + pageUrl + " )");
|
168
|
}
|
169
|
|
170
|
// now set the real URL
|
171
|
this.pageUrl = new URL(driver.getCurrentUrl());
|
172
|
|
173
|
logger.info("loading " + pageUrl);
|
174
|
|
175
|
// This call sets the WebElement fields.
|
176
|
PageFactory.initElements(driver, this);
|
177
|
|
178
|
}
|
179
|
|
180
|
/**
|
181
|
* @return
|
182
|
*/
|
183
|
protected boolean isOnPage() {
|
184
|
return driver.getCurrentUrl().startsWith(pageUrl.toString());
|
185
|
}
|
186
|
|
187
|
public void get() {
|
188
|
if(!driver.getCurrentUrl().equals(pageUrl.toString())){
|
189
|
driver.get(pageUrl.toString());
|
190
|
PageFactory.initElements(driver, this);
|
191
|
}
|
192
|
}
|
193
|
|
194
|
public String getDrupalPagePath() {
|
195
|
return drupalPagePath;
|
196
|
}
|
197
|
|
198
|
/**
|
199
|
* returns the string from the <code>title</code> tag.
|
200
|
* @return
|
201
|
*/
|
202
|
public String getTitle() {
|
203
|
return title.getText();
|
204
|
}
|
205
|
|
206
|
/**
|
207
|
* returns the warning messages from the Drupal message box
|
208
|
* @return
|
209
|
*/
|
210
|
public String getWarnings() {
|
211
|
return null; //TODO unimplemented
|
212
|
}
|
213
|
|
214
|
/**
|
215
|
* returns the error messages from the Drupal message box
|
216
|
* @return
|
217
|
*/
|
218
|
public String getErrors() {
|
219
|
return null; //TODO unimplemented
|
220
|
}
|
221
|
|
222
|
public String getAuthorInformationText() {
|
223
|
|
224
|
WebElement authorInformation = null;
|
225
|
|
226
|
try {
|
227
|
authorInformation = node.findElement(By.className("submitted"));
|
228
|
} catch (NoSuchElementException e) {
|
229
|
// IGNORE //
|
230
|
}
|
231
|
|
232
|
|
233
|
if(authorInformation != null){
|
234
|
return authorInformation.getText();
|
235
|
} else {
|
236
|
return null;
|
237
|
}
|
238
|
}
|
239
|
|
240
|
public List<LinkElement> getPrimaryTabs(){
|
241
|
List<LinkElement> tabs = new ArrayList<LinkElement>();
|
242
|
List<WebElement> links = primaryTabs.findElements(By.tagName("a"));
|
243
|
for(WebElement a : links) {
|
244
|
WebElement renderedLink = a;
|
245
|
if(renderedLink.isDisplayed()){
|
246
|
tabs.add(new LinkElement(renderedLink));
|
247
|
}
|
248
|
}
|
249
|
|
250
|
return tabs;
|
251
|
}
|
252
|
|
253
|
public void hover(WebElement element) {
|
254
|
Actions actions = new Actions(driver);
|
255
|
actions.moveToElement(element, 1, 1).perform();
|
256
|
logger.debug("hovering");
|
257
|
}
|
258
|
|
259
|
|
260
|
/**
|
261
|
* Returns the current URL string from the {@link WebDriver}
|
262
|
* @return
|
263
|
*/
|
264
|
public URL getPageURL() {
|
265
|
return pageUrl;
|
266
|
}
|
267
|
|
268
|
|
269
|
/**
|
270
|
* return the <code>scheme://domain:port</code> part of the initial url of this page.
|
271
|
* @return
|
272
|
*/
|
273
|
public String getInitialUrlBase() {
|
274
|
return pageUrl.getProtocol() + "://" + pageUrl.getHost() + pageUrl.getPort();
|
275
|
}
|
276
|
|
277
|
public boolean equals(Object obj) {
|
278
|
if (PortalPage.class.isAssignableFrom(obj.getClass())) {
|
279
|
PortalPage page = (PortalPage) obj;
|
280
|
return this.getPageURL().toString().equals(page.getPageURL().toString());
|
281
|
|
282
|
} else {
|
283
|
return false;
|
284
|
}
|
285
|
}
|
286
|
|
287
|
|
288
|
}
|