1 package org.itracker.selenium;
2
3 import com.google.common.base.Function;
4 import org.apache.commons.io.FileUtils;
5 import org.apache.log4j.Logger;
6 import org.itracker.AbstractDependencyInjectionTest;
7 import org.junit.Before;
8 import org.openqa.selenium.*;
9 import org.openqa.selenium.support.ui.FluentWait;
10 import org.subethamail.wiser.Wiser;
11
12 import java.io.File;
13 import java.util.concurrent.TimeUnit;
14
15 import static org.itracker.Assert.*;
16
17
18
19
20
21
22
23
24
25 @SuppressWarnings("WeakerAccess")
26 public abstract class AbstractSeleniumTestCase
27 extends AbstractDependencyInjectionTest {
28 protected final static String SE_TIMEOUT = "2000";
29 protected static final Wiser wiser;
30
31 protected WebDriver driver;
32 protected String applicationHost;
33 protected int applicationPort;
34 protected String applicationPath;
35 protected String applicationURL;
36 static final Logger log = Logger.getLogger(AbstractSeleniumTestCase.class);
37
38 static {
39 wiser = new Wiser(SeleniumManager.getSMTPPort());
40 wiser.start();
41 Logger.getLogger(AbstractSeleniumTestCase.class).info("started wiser on " + wiser.getServer().getPort());
42
43 Runtime.getRuntime().addShutdownHook(new Thread(
44 new Runnable() {
45 @Override
46 public void run() {
47
48 try {
49 wiser.stop();
50 Logger.getLogger(getClass()).info("stopped wiser " + wiser);
51 } catch (RuntimeException e) {
52 Logger.getLogger(getClass()).warn("could not stop running wiser: " + wiser);
53 Logger.getLogger(getClass()).debug("exception caught", e);
54 }
55 }
56 }
57 ));
58 }
59
60 @Before
61 public void seleniumStart() throws Exception {
62 driver = SeleniumManager.getWebDriver();
63 assertNotNull("driver", driver);
64 driver.manage().timeouts().implicitlyWait(Long.parseLong(SE_TIMEOUT), TimeUnit.MILLISECONDS);
65 applicationHost = SeleniumManager.getApplicationHost();
66 applicationPort = SeleniumManager.getApplicationPort();
67 applicationPath = SeleniumManager.getApplicationPath();
68 applicationURL = "http://" + applicationHost + ":" + applicationPort + "/"
69 + applicationPath;
70 }
71
72 public final WebElement assertElementPresent(By by) {
73 if (log.isDebugEnabled()) {
74 log.debug("element present by " + by);
75 }
76 assertTrue(driver.getCurrentUrl() + " " + by + " present", isElementPresent(by));
77 return driver.findElement(by);
78 }
79
80 public final void assertElementNotPresent(By by) {
81 if (log.isDebugEnabled()) {
82 log.debug("element not present by " + by);
83 }
84 assertFalse(driver.getCurrentUrl() + " " + by + " present", isElementPresent(by));
85 }
86
87 public final WebElement assertElementTextEquals(String expected, By by) {
88 WebElement el = assertElementPresent(by);
89 if (log.isDebugEnabled()) {
90 log.debug("text equals '" + expected + "' by " + by);
91 }
92 assertEquals(driver.getCurrentUrl() + " " + by, expected, el.getText());
93 return el;
94 }
95
96 public final void assertElementCountEquals(int expected, By by) {
97 if (log.isDebugEnabled()) {
98 log.debug("element count '" + expected + "' by " + by);
99 }
100 assertEquals(driver.getCurrentUrl() + " " + by + " size", expected, driver.findElements(by).size());
101 }
102
103 public final WebElement assertElementPresent(By by, SearchContext searchContext) {
104 if (log.isDebugEnabled()) {
105 log.debug("element present in " + searchContext + " by " + by);
106 }
107 assertTrue(driver.getCurrentUrl() + " " + by + " present in " + searchContext, isElementPresent(by, searchContext));
108 return searchContext.findElement(by);
109 }
110
111 public final void assertElementNotPresent(By by, SearchContext searchContext) {
112 if (log.isDebugEnabled()) {
113 log.debug("element not present in " + searchContext + " by " + by);
114 }
115 assertFalse(driver.getCurrentUrl() + " " + by + " present in " + searchContext, isElementPresent(by, searchContext));
116 }
117
118 public final WebElement assertElementTextEquals(String expected, By by, SearchContext searchContext) {
119 WebElement el = assertElementPresent(by, searchContext);
120 if (log.isDebugEnabled()) {
121 log.debug("text equals '" + expected + " in " + searchContext + "' by " + by);
122 }
123 assertEquals(driver.getCurrentUrl() + " in " + searchContext + " ny " + by, expected, el.getText());
124 return el;
125 }
126
127 public final void assertElementCountEquals(int expected, By by, SearchContext searchContext) {
128 if (log.isDebugEnabled()) {
129 log.debug("element count '" + expected + "' in " + searchContext + " by " + by);
130 }
131 assertEquals(driver.getCurrentUrl() + " " + by + " size in " + searchContext, expected, searchContext.findElements(by).size());
132 }
133
134
135
136
137
138 protected void closeSession() {
139 SeleniumManager.closeSession(driver);
140 }
141
142
143
144
145
146 protected final void login(final String username, final String password) {
147
148 log.debug("login called with " + username + ", " + password);
149
150 assertElementPresent(By.name("login")).sendKeys(username);
151 assertElementPresent(By.name("password")).sendKeys(password);
152 assertElementPresent(By.id("btn-login")).click();
153 waitForPageToLoad();
154 assertElementPresent(By.name("lookupForm"));
155
156 log.debug("loginUser, logged in " + username + ", cookies: " + driver.manage().getCookies());
157 }
158
159 private boolean isElementPresent(By by) {
160 return isElementPresent(by, driver);
161 }
162
163 private boolean isElementPresent(By by, SearchContext context) {
164 try {
165 driver.manage().timeouts().implicitlyWait(200, TimeUnit.MILLISECONDS);
166 context.findElement(by);
167 return true;
168 } catch (NoSuchElementException e) {
169 if (log.isDebugEnabled())
170 log.debug("isElementPresent: not present " + by + "\n in:\n<"
171 + driver.getCurrentUrl() + ">\n"
172 + driver.getPageSource());
173 return false;
174 } finally {
175 driver.manage().timeouts().implicitlyWait(Long.parseLong(SE_TIMEOUT), TimeUnit.MILLISECONDS);
176 }
177
178 }
179
180 protected void waitForPageToLoad() {
181 waitForPageToLoad(Long.parseLong(SE_TIMEOUT));
182 }
183
184 protected void waitForPageToLoad(long timeout) {
185
186 new FluentWait<>(driver)
187 .withTimeout(timeout, TimeUnit.MILLISECONDS)
188 .pollingEvery(200, TimeUnit.MILLISECONDS)
189 .until(new Function<WebDriver, Boolean>() {
190 @Override
191 public Boolean apply(WebDriver webDriver) {
192 try {
193 Object result = ((JavascriptExecutor) driver).executeScript(
194 "return 'complete' == document.readyState;");
195
196 if (Boolean.TRUE.equals(result)) {
197 return true;
198 }
199 } catch (Exception e) {
200
201 }
202 return false;
203 }
204 });
205 }
206
207 public static void takeSnapShot(WebDriver webdriver, String fileWithPath) throws Exception {
208
209 if (!(webdriver instanceof TakesScreenshot)) {
210 log.info("webdriver is not TakesScreenshot: " + webdriver);
211 return;
212 }
213
214
215 TakesScreenshot scrShot = ((TakesScreenshot) webdriver);
216
217
218
219 File SrcFile = scrShot.getScreenshotAs(OutputType.FILE);
220
221
222
223 File DestFile = new File(fileWithPath);
224
225
226
227 FileUtils.copyFile(SrcFile, DestFile);
228
229 }
230
231 }