This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 57
/
AbstractPage.java
145 lines (126 loc) · 4.67 KB
/
AbstractPage.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
* Copyright 2010, Red Hat, Inc. and individual contributors as indicated by the
* @author tags. See the copyright.txt file in the distribution for a full
* listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This software is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this software; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF
* site: http://www.fsf.org.
*/
package org.zanata.page;
import static java.util.concurrent.TimeUnit.SECONDS;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.pagefactory.AjaxElementLocatorFactory;
import org.openqa.selenium.support.ui.FluentWait;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.zanata.util.WebElementUtil;
import com.google.common.base.Function;
import com.google.common.base.Preconditions;
import com.google.common.collect.Collections2;
import com.google.common.collect.ImmutableList;
public class AbstractPage
{
protected List<String> getErrors()
{
List<WebElement> errorSpans = getDriver().findElements(By.xpath("//span[@class='errors']"));
return WebElementUtil.elementsToText(errorSpans);
}
public enum PageContext
{
jsf, webTran
}
private static final Logger LOGGER = LoggerFactory.getLogger(AbstractPage.class);
private final WebDriver driver;
private final FluentWait<WebDriver> ajaxWaitForTenSec;
private List<WebElement> navMenuItems = Collections.emptyList();
public AbstractPage(final WebDriver driver)
{
this(driver, PageContext.jsf);
}
public AbstractPage(final WebDriver driver, PageContext pageContext)
{
PageFactory.initElements(new AjaxElementLocatorFactory(driver, 30), this);
this.driver = driver;
ajaxWaitForTenSec = createWaitForAjax(driver, 10);
if (pageContext == PageContext.jsf)
{
//webTran and jsp don't share same page layout
navMenuItems = driver.findElement(By.id("tabs-menu")).findElements(By.tagName("a"));
}
}
public static FluentWait<WebDriver> createWaitForAjax(WebDriver webDriver, int durationInSec)
{
return new FluentWait<WebDriver>(webDriver).withTimeout(durationInSec, SECONDS).pollingEvery(1, SECONDS).ignoring(NoSuchElementException.class);
}
public WebDriver getDriver()
{
return driver;
}
public String getTitle()
{
return driver.getTitle();
}
public List<String> getNavigationMenuItems()
{
Collection<String> linkTexts = Collections2.transform(navMenuItems, new Function<WebElement, String>()
{
@Override
public String apply(WebElement link)
{
return link.getText();
}
});
return ImmutableList.copyOf(linkTexts);
}
public <P> P goToPage(String navLinkText, Class<P> pageClass)
{
LOGGER.info("click {} and go to page {}", navLinkText, pageClass.getName());
List<String> navigationMenuItems = getNavigationMenuItems();
int menuItemIndex = navigationMenuItems.indexOf(navLinkText);
Preconditions.checkState(menuItemIndex >= 0, navLinkText + " is not available in navigation menu");
navMenuItems.get(menuItemIndex).click();
return PageFactory.initElements(driver, pageClass);
}
// TODO this doesn't seem useful
public <P> P goToUrl(String url, P page)
{
LOGGER.info("go to url: {}", url);
driver.get(url);
PageFactory.initElements(new AjaxElementLocatorFactory(driver, 30), page);
return page;
}
public FluentWait<WebDriver> waitForTenSec()
{
return ajaxWaitForTenSec;
}
protected void clickSaveAndCheckErrors(WebElement saveButton)
{
saveButton.click();
List<String> errors = getErrors();
if (!errors.isEmpty())
{
throw new RuntimeException(StringUtils.join(errors, ";"));
}
}
}