Project

General

Profile

Download (6.23 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.support.CacheLookup;
14
import org.openqa.selenium.support.FindBy;
15
import org.openqa.selenium.support.FindBys;
16
import org.openqa.selenium.support.PageFactory;
17

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

    
22
public abstract class  PortalPage {
23

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

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

    
28
	protected WebDriver driver;
29

    
30
	protected final JUnitWebDriverWait wait;
31

    
32

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

    
42
	private String drupalPagePath;
43

    
44
	protected URL pageUrl;
45

    
46
	// ==== WebElements === //
47

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

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

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

    
58

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

    
63

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

    
82
		this.driver = driver;
83

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

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

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

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

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

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

    
98
	}
99

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

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

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

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

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

    
123
		this.pageUrl = url;
124

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

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

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

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

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

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

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

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

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

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

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

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

    
182
	public String getAuthorInformationText() {
183

    
184
		WebElement authorInformation = null;
185

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

    
192

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

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

    
210
		return tabs;
211
	}
212

    
213

    
214
	/**
215
	 * Returns the current URL string from the {@link WebDriver}
216
	 * @return
217
	 */
218
	public String getURL() {
219
		return driver.getCurrentUrl();
220
	}
221

    
222

    
223
	/**
224
	 * return the <code>scheme://domain:port</code> part of the initial url of this page.
225
	 * @return
226
	 */
227
	public String getInitialUrlBase() {
228
		return pageUrl.getProtocol() + "://" + pageUrl.getHost() + pageUrl.getPort();
229
	}
230

    
231
	public boolean equals(Object obj) {
232
		if (PortalPage.class.isAssignableFrom(obj.getClass())) {
233
			PortalPage page = (PortalPage) obj;
234
			return this.getURL().equals(page.getURL());
235

    
236
		} else {
237
			return false;
238
		}
239
	}
240

    
241

    
242
}
(3-3/4)