You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AbstractTB3Test.java 41KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267
  1. /*
  2. * Copyright 2000-2016 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.tests.tb3;
  17. import java.io.IOException;
  18. import java.io.InputStream;
  19. import java.io.StringWriter;
  20. import java.lang.reflect.Field;
  21. import java.net.URL;
  22. import java.util.ArrayList;
  23. import java.util.Arrays;
  24. import java.util.Collections;
  25. import java.util.HashSet;
  26. import java.util.List;
  27. import java.util.Set;
  28. import java.util.logging.Level;
  29. import org.apache.commons.io.IOUtils;
  30. import org.apache.commons.lang3.StringUtils;
  31. import org.apache.http.HttpHost;
  32. import org.apache.http.HttpResponse;
  33. import org.apache.http.impl.client.DefaultHttpClient;
  34. import org.apache.http.message.BasicHttpEntityEnclosingRequest;
  35. import org.junit.Assert;
  36. import org.junit.Rule;
  37. import org.junit.rules.TestName;
  38. import org.junit.runner.RunWith;
  39. import org.openqa.selenium.By;
  40. import org.openqa.selenium.Dimension;
  41. import org.openqa.selenium.JavascriptExecutor;
  42. import org.openqa.selenium.NoSuchElementException;
  43. import org.openqa.selenium.WebDriver;
  44. import org.openqa.selenium.WebElement;
  45. import org.openqa.selenium.interactions.Actions;
  46. import org.openqa.selenium.interactions.HasInputDevices;
  47. import org.openqa.selenium.interactions.Keyboard;
  48. import org.openqa.selenium.interactions.Mouse;
  49. import org.openqa.selenium.interactions.internal.Coordinates;
  50. import org.openqa.selenium.internal.Locatable;
  51. import org.openqa.selenium.internal.WrapsElement;
  52. import org.openqa.selenium.remote.DesiredCapabilities;
  53. import org.openqa.selenium.remote.HttpCommandExecutor;
  54. import org.openqa.selenium.remote.RemoteWebDriver;
  55. import org.openqa.selenium.support.ui.ExpectedCondition;
  56. import org.openqa.selenium.support.ui.ExpectedConditions;
  57. import org.openqa.selenium.support.ui.WebDriverWait;
  58. import com.vaadin.server.LegacyApplication;
  59. import com.vaadin.server.UIProvider;
  60. import com.vaadin.testbench.TestBenchDriverProxy;
  61. import com.vaadin.testbench.TestBenchElement;
  62. import com.vaadin.testbench.annotations.BrowserConfiguration;
  63. import com.vaadin.testbench.elements.CheckBoxElement;
  64. import com.vaadin.testbench.elements.LabelElement;
  65. import com.vaadin.testbench.elements.TableElement;
  66. import com.vaadin.testbench.elements.VerticalLayoutElement;
  67. import com.vaadin.testbench.parallel.Browser;
  68. import com.vaadin.testbench.parallel.BrowserUtil;
  69. import com.vaadin.testbench.parallel.ParallelTest;
  70. import com.vaadin.ui.UI;
  71. import elemental.json.JsonObject;
  72. import elemental.json.impl.JsonUtil;
  73. /**
  74. * Base class for TestBench 3+ tests. All TB3+ tests in the project should
  75. * extend this class.
  76. *
  77. * Provides:
  78. * <ul>
  79. * <li>Helpers for browser selection</li>
  80. * <li>Hub connection setup and teardown</li>
  81. * <li>Automatic generation of URL for a given test on the development server
  82. * using {@link #getUIClass()} or by automatically finding an enclosing UI class
  83. * and based on requested features, e.g. {@link #isDebug()},
  84. * {@link #isPush()}</li>
  85. * <li>Generic helpers for creating TB3+ tests</li>
  86. * </ul>
  87. *
  88. * @author Vaadin Ltd
  89. */
  90. @RunWith(TB3Runner.class)
  91. public abstract class AbstractTB3Test extends ParallelTest {
  92. @Rule
  93. public TestName testName = new TestName();
  94. @Rule
  95. public RetryOnFail retry = new RetryOnFail();
  96. /**
  97. * Height of the screenshots we want to capture
  98. */
  99. private static final int SCREENSHOT_HEIGHT = 850;
  100. /**
  101. * Width of the screenshots we want to capture
  102. */
  103. private static final int SCREENSHOT_WIDTH = 1500;
  104. /**
  105. * Timeout used by the TB grid
  106. */
  107. private static final int BROWSER_TIMEOUT_IN_MS = 30 * 1000;
  108. protected static DesiredCapabilities PHANTOMJS2() {
  109. DesiredCapabilities phantomjs2 = new VaadinBrowserFactory()
  110. .create(Browser.PHANTOMJS, "2");
  111. // Hack for the test cluster
  112. phantomjs2.setCapability("phantomjs.binary.path",
  113. "/usr/bin/phantomjs2");
  114. return phantomjs2;
  115. }
  116. private boolean debug = false;
  117. private boolean push = false;
  118. static {
  119. com.vaadin.testbench.Parameters
  120. .setScreenshotComparisonCursorDetection(true);
  121. }
  122. /**
  123. * Connect to the hub using a remote web driver, set the canvas size and
  124. * opens the initial URL as specified by {@link #getTestUrl()}
  125. *
  126. * @throws Exception
  127. */
  128. @Override
  129. public void setup() throws Exception {
  130. super.setup();
  131. int w = SCREENSHOT_WIDTH;
  132. int h = SCREENSHOT_HEIGHT;
  133. try {
  134. testBench().resizeViewPortTo(w, h);
  135. } catch (UnsupportedOperationException e) {
  136. // Opera does not support this...
  137. }
  138. }
  139. /**
  140. * Method for closing the tested application.
  141. */
  142. protected void closeApplication() {
  143. if (driver != null) {
  144. try {
  145. openTestURL("closeApplication");
  146. } catch (Exception e) {
  147. e.printStackTrace();
  148. }
  149. }
  150. }
  151. protected WebElement getTooltipErrorElement() {
  152. WebElement tooltip = getDriver()
  153. .findElement(com.vaadin.testbench.By.className("v-tooltip"));
  154. return tooltip.findElement(By.className("v-errormessage"));
  155. }
  156. protected WebElement getTooltipElement() {
  157. return getDriver().findElement(
  158. com.vaadin.testbench.By.className("v-tooltip-text"));
  159. }
  160. protected Coordinates getCoordinates(TestBenchElement element) {
  161. return ((Locatable) element.getWrappedElement()).getCoordinates();
  162. }
  163. private boolean hasDebugMessage(String message) {
  164. return getDebugMessage(message) != null;
  165. }
  166. private WebElement getDebugMessage(String message) {
  167. return driver.findElement(By.xpath(String.format(
  168. "//span[@class='v-debugwindow-message' and text()='%s']",
  169. message)));
  170. }
  171. protected void waitForDebugMessage(final String expectedMessage) {
  172. waitForDebugMessage(expectedMessage, 30);
  173. }
  174. protected void waitForDebugMessage(final String expectedMessage,
  175. int timeout) {
  176. waitUntil(new ExpectedCondition<Boolean>() {
  177. @Override
  178. public Boolean apply(WebDriver input) {
  179. return hasDebugMessage(expectedMessage);
  180. }
  181. }, timeout);
  182. }
  183. protected void clearDebugMessages() {
  184. driver.findElement(By
  185. .xpath("//button[@class='v-debugwindow-button' and @title='Clear log']"))
  186. .click();
  187. }
  188. protected void waitUntilRowIsVisible(final TableElement table,
  189. final int row) {
  190. waitUntil(new ExpectedCondition<Object>() {
  191. @Override
  192. public Object apply(WebDriver input) {
  193. try {
  194. return table.getCell(row, 0) != null;
  195. } catch (NoSuchElementException e) {
  196. return false;
  197. }
  198. }
  199. });
  200. }
  201. protected void scrollTable(TableElement table, int rows, int rowToWait) {
  202. testBenchElement(table.findElement(By.className("v-scrollable")))
  203. .scroll(rows * 30);
  204. waitUntilRowIsVisible(table, rowToWait);
  205. }
  206. /**
  207. * Opens the given test (defined by {@link #getTestUrl()}, optionally with
  208. * debug window and/or push (depending on {@link #isDebug()} and
  209. * {@link #isPush()}.
  210. */
  211. protected void openTestURL() {
  212. openTestURL(new String[0]);
  213. }
  214. /**
  215. * Opens the given test (defined by {@link #getTestUrl()}, optionally with
  216. * debug window and/or push (depending on {@link #isDebug()} and
  217. * {@link #isPush()}.
  218. */
  219. protected void openTestURL(String... parameters) {
  220. openTestURL(getUIClass(), parameters);
  221. }
  222. /**
  223. * Opens the given test (defined by {@link #getTestUrl()}, optionally with
  224. * debug window and/or push (depending on {@link #isDebug()} and
  225. * {@link #isPush()}.
  226. */
  227. protected void openTestURL(Class<?> uiClass, String... parameters) {
  228. openTestURL(uiClass, new HashSet<>(Arrays.asList(parameters)));
  229. }
  230. private void openTestURL(Class<?> uiClass, Set<String> parameters) {
  231. String url = getTestURL(uiClass);
  232. if (isDebug()) {
  233. parameters.add("debug");
  234. }
  235. if (LegacyApplication.class.isAssignableFrom(uiClass)) {
  236. parameters.add("restartApplication");
  237. }
  238. if (parameters.size() > 0) {
  239. url += "?" + StringUtils.join(parameters, "&");
  240. }
  241. driver.get(url);
  242. }
  243. /**
  244. * Returns the full URL to be used for the test
  245. *
  246. * @return the full URL for the test
  247. */
  248. protected String getTestUrl() {
  249. return StringUtils.strip(getBaseURL(), "/") + getDeploymentPath();
  250. }
  251. /**
  252. * Returns the full URL to be used for the test for the provided UI class.
  253. *
  254. * @return the full URL for the test
  255. */
  256. protected String getTestURL(Class<?> uiClass) {
  257. return StringUtils.strip(getBaseURL(), "/")
  258. + getDeploymentPath(uiClass);
  259. }
  260. /**
  261. * Used to determine what URL to initially open for the test
  262. *
  263. * @return the host name of development server
  264. */
  265. protected abstract String getDeploymentHostname();
  266. /**
  267. * Used to determine what port the test is running on
  268. *
  269. * @return The port teh test is running on, by default 8888
  270. */
  271. protected abstract int getDeploymentPort();
  272. /**
  273. * Produces a collection of browsers to run the test on. This method is
  274. * executed by the test runner when determining how many test methods to
  275. * invoke and with what parameters. For each returned value a test method is
  276. * ran and before running that,
  277. * {@link #setDesiredCapabilities(DesiredCapabilities)} is invoked with the
  278. * value returned by this method.
  279. *
  280. * This method is not static to allow overriding it in sub classes. By
  281. * default runs the test only on Firefox
  282. *
  283. * @return The browsers to run the test on
  284. */
  285. @BrowserConfiguration
  286. public List<DesiredCapabilities> getBrowsersToTest() {
  287. return Collections
  288. .singletonList(Browser.FIREFOX.getDesiredCapabilities());
  289. }
  290. /**
  291. * Finds an element based on the part of a TB2 style locator following the
  292. * :: (e.g. vaadin=runLabelModes::PID_Scheckboxaction-Enabled/domChild[0] ->
  293. * PID_Scheckboxaction-Enabled/domChild[0]).
  294. *
  295. * @param vaadinLocator
  296. * The part following :: of the vaadin locator string
  297. * @return
  298. */
  299. protected WebElement vaadinElement(String vaadinLocator) {
  300. return driver.findElement(vaadinLocator(vaadinLocator));
  301. }
  302. /**
  303. * Uses JavaScript to determine the currently focused element.
  304. *
  305. * @return Focused element or null
  306. */
  307. protected WebElement getFocusedElement() {
  308. Object focusedElement = executeScript("return document.activeElement");
  309. if (null != focusedElement) {
  310. return (WebElement) focusedElement;
  311. } else {
  312. return null;
  313. }
  314. }
  315. /**
  316. * Executes the given Javascript
  317. *
  318. * @param script
  319. * the script to execute
  320. * @return whatever
  321. * {@link org.openqa.selenium.JavascriptExecutor#executeScript(String, Object...)}
  322. * returns
  323. */
  324. protected Object executeScript(String script, Object... args) {
  325. return ((JavascriptExecutor) getDriver()).executeScript(script, args);
  326. }
  327. /**
  328. * Find a Vaadin element based on its id given using Component.setId
  329. *
  330. * @param id
  331. * The id to locate
  332. * @return
  333. */
  334. public WebElement vaadinElementById(String id) {
  335. return driver.findElement(By.id(id));
  336. }
  337. /**
  338. * Finds a {@link By} locator based on the part of a TB2 style locator
  339. * following the :: (e.g.
  340. * vaadin=runLabelModes::PID_Scheckboxaction-Enabled/domChild[0] ->
  341. * PID_Scheckboxaction-Enabled/domChild[0]).
  342. *
  343. * @param vaadinLocator
  344. * The part following :: of the vaadin locator string
  345. * @return
  346. */
  347. public org.openqa.selenium.By vaadinLocator(String vaadinLocator) {
  348. String base = getApplicationId(getDeploymentPath());
  349. base += "::";
  350. return com.vaadin.testbench.By.vaadin(base + vaadinLocator);
  351. }
  352. /**
  353. * Constructs a {@link By} locator for the id given using Component.setId
  354. *
  355. * @param id
  356. * The id to locate
  357. * @return a locator for the given id
  358. */
  359. public By vaadinLocatorById(String id) {
  360. return vaadinLocator("PID_S" + id);
  361. }
  362. /**
  363. * Waits up to 10s for the given condition to become true. Use e.g. as
  364. * {@link #waitUntil(ExpectedConditions.textToBePresentInElement(by, text))}
  365. *
  366. * @param condition
  367. * the condition to wait for to become true
  368. */
  369. protected <T> void waitUntil(ExpectedCondition<T> condition) {
  370. waitUntil(condition, 10);
  371. }
  372. /**
  373. * Waits the given number of seconds for the given condition to become true.
  374. * Use e.g. as
  375. * {@link #waitUntil(ExpectedConditions.textToBePresentInElement(by, text))}
  376. *
  377. * @param condition
  378. * the condition to wait for to become true
  379. */
  380. protected <T> void waitUntil(ExpectedCondition<T> condition,
  381. long timeoutInSeconds) {
  382. new WebDriverWait(driver, timeoutInSeconds).until(condition);
  383. }
  384. /**
  385. * Waits up to 10s for the given condition to become false. Use e.g. as
  386. * {@link #waitUntilNot(ExpectedConditions.textToBePresentInElement(by,
  387. * text))}
  388. *
  389. * @param condition
  390. * the condition to wait for to become false
  391. */
  392. protected <T> void waitUntilNot(ExpectedCondition<T> condition) {
  393. waitUntilNot(condition, 10);
  394. }
  395. /**
  396. * Waits the given number of seconds for the given condition to become
  397. * false. Use e.g. as
  398. * {@link #waitUntilNot(ExpectedConditions.textToBePresentInElement(by,
  399. * text))}
  400. *
  401. * @param condition
  402. * the condition to wait for to become false
  403. */
  404. protected <T> void waitUntilNot(ExpectedCondition<T> condition,
  405. long timeoutInSeconds) {
  406. waitUntil(ExpectedConditions.not(condition), timeoutInSeconds);
  407. }
  408. protected void waitForElementPresent(final By by) {
  409. waitUntil(ExpectedConditions.presenceOfElementLocated(by));
  410. }
  411. protected void waitForElementNotPresent(final By by) {
  412. waitUntil(new ExpectedCondition<Boolean>() {
  413. @Override
  414. public Boolean apply(WebDriver input) {
  415. return input.findElements(by).isEmpty();
  416. }
  417. });
  418. }
  419. protected void waitForElementVisible(final By by) {
  420. waitUntil(ExpectedConditions.visibilityOfElementLocated(by));
  421. }
  422. /**
  423. * Checks if the given element has the given class name.
  424. *
  425. * Matches only full class names, i.e. has ("foo") does not match
  426. * class="foobar"
  427. *
  428. * @param element
  429. * @param className
  430. * @return
  431. */
  432. protected boolean hasCssClass(WebElement element, String className) {
  433. String classes = element.getAttribute("class");
  434. if (classes == null || classes.isEmpty()) {
  435. return (className == null || className.isEmpty());
  436. }
  437. for (String cls : classes.split(" ")) {
  438. if (className.equals(cls)) {
  439. return true;
  440. }
  441. }
  442. return false;
  443. }
  444. /**
  445. * For tests extending AbstractTestUIWithLog, returns the element for the
  446. * Nth log row
  447. *
  448. * @param rowNr
  449. * The log row to retrieve
  450. * @return the Nth log row
  451. */
  452. protected WebElement getLogRowElement(int rowNr) {
  453. return vaadinElementById("Log_row_" + rowNr);
  454. }
  455. /**
  456. * For tests extending AbstractTestUIWithLog, returns the text in the Nth
  457. * log row
  458. *
  459. * @param rowNr
  460. * The log row to retrieve text for
  461. * @return the text in the log row
  462. */
  463. protected String getLogRow(int rowNr) {
  464. return getLogRowElement(rowNr).getText();
  465. }
  466. /**
  467. * Asserts that {@literal a} is &gt;= {@literal b}
  468. *
  469. * @param message
  470. * The message to include in the {@link AssertionError}
  471. * @param a
  472. * @param b
  473. * @throws AssertionError
  474. * If comparison fails
  475. */
  476. public static final <T> void assertGreaterOrEqual(String message,
  477. Comparable<T> a, T b) throws AssertionError {
  478. if (a.compareTo(b) >= 0) {
  479. return;
  480. }
  481. throw new AssertionError(decorate(message, a, b));
  482. }
  483. /**
  484. * Asserts that {@literal a} is &gt; {@literal b}
  485. *
  486. * @param message
  487. * The message to include in the {@link AssertionError}
  488. * @param a
  489. * @param b
  490. * @throws AssertionError
  491. * If comparison fails
  492. */
  493. public static final <T> void assertGreater(String message, Comparable<T> a,
  494. T b) throws AssertionError {
  495. if (a.compareTo(b) > 0) {
  496. return;
  497. }
  498. throw new AssertionError(decorate(message, a, b));
  499. }
  500. /**
  501. * Asserts that {@literal a} is &lt;= {@literal b}
  502. *
  503. * @param message
  504. * The message to include in the {@link AssertionError}
  505. * @param a
  506. * @param b
  507. * @throws AssertionError
  508. * If comparison fails
  509. */
  510. public static final <T> void assertLessThanOrEqual(String message,
  511. Comparable<T> a, T b) throws AssertionError {
  512. if (a.compareTo(b) <= 0) {
  513. return;
  514. }
  515. throw new AssertionError(decorate(message, a, b));
  516. }
  517. /**
  518. * Asserts that {@literal a} is &lt; {@literal b}
  519. *
  520. * @param message
  521. * The message to include in the {@link AssertionError}
  522. * @param a
  523. * @param b
  524. * @throws AssertionError
  525. * If comparison fails
  526. */
  527. public static final <T> void assertLessThan(String message, Comparable<T> a,
  528. T b) throws AssertionError {
  529. if (a.compareTo(b) < 0) {
  530. return;
  531. }
  532. throw new AssertionError(decorate(message, a, b));
  533. }
  534. private static <T> String decorate(String message, Comparable<T> a, T b) {
  535. message = message.replace("{0}", a.toString());
  536. message = message.replace("{1}", b.toString());
  537. return message;
  538. }
  539. /**
  540. * Returns the path that should be used for the test. The path contains the
  541. * full path (appended to hostname+port) and must start with a slash.
  542. *
  543. * @param push
  544. * true if "?debug" should be added
  545. * @param debug
  546. * true if /run-push should be used instead of /run
  547. *
  548. * @return The URL path to the UI class to test
  549. */
  550. protected String getDeploymentPath() {
  551. Class<?> uiClass = getUIClass();
  552. if (uiClass != null) {
  553. return getDeploymentPath(uiClass);
  554. }
  555. throw new IllegalArgumentException("Unable to determine path for "
  556. + getClass().getCanonicalName());
  557. }
  558. /**
  559. * Returns the UI class the current test is connected to (or in special
  560. * cases UIProvider or LegacyApplication). Uses the enclosing class if the
  561. * test class is a static inner class to a UI class.
  562. *
  563. * Test which are not enclosed by a UI class must implement this method and
  564. * return the UI class they want to test.
  565. *
  566. * Note that this method will update the test name to the enclosing class to
  567. * be compatible with TB2 screenshot naming
  568. *
  569. * @return the UI class the current test is connected to
  570. */
  571. protected Class<?> getUIClass() {
  572. try {
  573. // Convention: SomeUITest uses the SomeUI UI class
  574. String uiClassName = getClass().getName().replaceFirst("Test$", "");
  575. Class<?> cls = Class.forName(uiClassName);
  576. if (isSupportedRunnerClass(cls)) {
  577. return cls;
  578. }
  579. } catch (Exception e) {
  580. }
  581. throw new RuntimeException(
  582. "Could not determine UI class. Ensure the test is named UIClassTest and is in the same package as the UIClass");
  583. }
  584. /**
  585. * @return true if the given class is supported by ApplicationServletRunner
  586. */
  587. @SuppressWarnings("deprecation")
  588. private boolean isSupportedRunnerClass(Class<?> cls) {
  589. if (UI.class.isAssignableFrom(cls)) {
  590. return true;
  591. }
  592. if (UIProvider.class.isAssignableFrom(cls)) {
  593. return true;
  594. }
  595. if (LegacyApplication.class.isAssignableFrom(cls)) {
  596. return true;
  597. }
  598. return false;
  599. }
  600. /**
  601. * Returns whether to run the test in debug mode (with the debug console
  602. * open) or not
  603. *
  604. * @return true to run with the debug window open, false by default
  605. */
  606. protected final boolean isDebug() {
  607. return debug;
  608. }
  609. /**
  610. * Sets whether to run the test in debug mode (with the debug console open)
  611. * or not.
  612. *
  613. * @param debug
  614. * true to open debug window, false otherwise
  615. */
  616. protected final void setDebug(boolean debug) {
  617. this.debug = debug;
  618. }
  619. /**
  620. * Returns whether to run the test with push enabled (using /run-push) or
  621. * not. Note that push tests can and should typically be created using @Push
  622. * on the UI instead of overriding this method
  623. *
  624. * @return true if /run-push is used, false otherwise
  625. */
  626. protected final boolean isPush() {
  627. return push;
  628. }
  629. /**
  630. * Sets whether to run the test with push enabled (using /run-push) or not.
  631. * Note that push tests can and should typically be created using @Push on
  632. * the UI instead of overriding this method
  633. *
  634. * @param push
  635. * true to use /run-push in the test, false otherwise
  636. */
  637. protected final void setPush(boolean push) {
  638. this.push = push;
  639. }
  640. /**
  641. * Returns the path for the given UI class when deployed on the test server.
  642. * The path contains the full path (appended to hostname+port) and must
  643. * start with a slash.
  644. *
  645. * This method takes into account {@link #isPush()} and {@link #isDebug()}
  646. * when the path is generated.
  647. *
  648. * @param uiClass
  649. * @param push
  650. * true if "?debug" should be added
  651. * @param debug
  652. * true if /run-push should be used instead of /run
  653. * @return The path to the given UI class
  654. */
  655. protected String getDeploymentPath(Class<?> uiClass) {
  656. String runPath = "/run";
  657. if (isPush()) {
  658. runPath = "/run-push";
  659. }
  660. if (UI.class.isAssignableFrom(uiClass)
  661. || UIProvider.class.isAssignableFrom(uiClass)
  662. || LegacyApplication.class.isAssignableFrom(uiClass)) {
  663. return runPath + "/" + uiClass.getCanonicalName();
  664. } else {
  665. throw new IllegalArgumentException(
  666. "Unable to determine path for enclosing class "
  667. + uiClass.getCanonicalName());
  668. }
  669. }
  670. /**
  671. * Used to determine what URL to initially open for the test
  672. *
  673. * @return The base URL for the test. Does not include a trailing slash.
  674. */
  675. protected String getBaseURL() {
  676. return "http://" + getDeploymentHostname() + ":" + getDeploymentPort();
  677. }
  678. /**
  679. * Generates the application id based on the URL in a way compatible with
  680. * VaadinServletService.
  681. *
  682. * @param pathWithQueryParameters
  683. * The path part of the URL, possibly still containing query
  684. * parameters
  685. * @return The application ID string used in Vaadin locators
  686. */
  687. private String getApplicationId(String pathWithQueryParameters) {
  688. // Remove any possible URL parameters
  689. String pathWithoutQueryParameters = pathWithQueryParameters
  690. .replaceAll("\\?.*", "");
  691. if ("".equals(pathWithoutQueryParameters)) {
  692. return "ROOT";
  693. }
  694. // Retain only a-z and numbers
  695. return pathWithoutQueryParameters.replaceAll("[^a-zA-Z0-9]", "");
  696. }
  697. /**
  698. * Sleeps for the given number of ms but ensures that the browser connection
  699. * does not time out.
  700. *
  701. * @param timeoutMillis
  702. * Number of ms to wait
  703. */
  704. protected void sleep(int timeoutMillis) {
  705. while (timeoutMillis > 0) {
  706. int d = Math.min(BROWSER_TIMEOUT_IN_MS, timeoutMillis);
  707. try {
  708. Thread.sleep(d);
  709. } catch (InterruptedException e) {
  710. throw new RuntimeException(e);
  711. }
  712. timeoutMillis -= d;
  713. // Do something to keep the connection alive
  714. getDriver().getTitle();
  715. }
  716. }
  717. /**
  718. * Called by the test runner whenever there is an exception in the test that
  719. * will cause termination of the test
  720. *
  721. * @param t
  722. * the throwable which caused the termination
  723. */
  724. public void onUncaughtException(Throwable t) {
  725. // Do nothing by default
  726. }
  727. /**
  728. * Returns the mouse object for doing mouse commands
  729. *
  730. * @return Returns the mouse
  731. */
  732. public Mouse getMouse() {
  733. return ((HasInputDevices) getDriver()).getMouse();
  734. }
  735. /**
  736. * Returns the keyboard object for controlling keyboard events
  737. *
  738. * @return Return the keyboard
  739. */
  740. public Keyboard getKeyboard() {
  741. return ((HasInputDevices) getDriver()).getKeyboard();
  742. }
  743. public void hitButton(String id) {
  744. driver.findElement(By.id(id)).click();
  745. }
  746. protected void openDebugLogTab() {
  747. waitUntil(new ExpectedCondition<Boolean>() {
  748. @Override
  749. public Boolean apply(WebDriver input) {
  750. WebElement element = getDebugLogButton();
  751. return element != null;
  752. }
  753. }, 15);
  754. getDebugLogButton().click();
  755. }
  756. private WebElement getDebugLogButton() {
  757. return findElement(By.xpath("//button[@title='Debug message log']"));
  758. }
  759. protected void assertNoDebugMessage(Level level) {
  760. // class="v-debugwindow-row Level.getName()"
  761. List<WebElement> logElements = driver.findElements(By.xpath(String
  762. .format("//div[@class='v-debugwindow-row %s']/span[@class='v-debugwindow-message']",
  763. level.getName())));
  764. if (!logElements.isEmpty()) {
  765. String logRows = "";
  766. for (WebElement e : logElements) {
  767. logRows += "\n" + e.getText();
  768. }
  769. Assert.fail("Found debug messages with level " + level.getName()
  770. + ": " + logRows);
  771. }
  772. }
  773. /**
  774. * Should the "require window focus" be enabled for Internet Explorer.
  775. * RequireWindowFocus makes tests more stable but seems to be broken with
  776. * certain commands such as sendKeys. Therefore it is not enabled by default
  777. * for all tests
  778. *
  779. * @return true, to use the "require window focus" feature, false otherwise
  780. */
  781. protected boolean requireWindowFocusForIE() {
  782. return false;
  783. }
  784. /**
  785. * Should the "enable persistent hover" be enabled for Internet Explorer.
  786. *
  787. * Persistent hovering causes continuous firing of mouse over events at the
  788. * last location the mouse cursor has been moved to. This is to avoid
  789. * problems where the real mouse cursor is inside the browser window and
  790. * Internet Explorer uses that location for some undefined operation
  791. * (http://
  792. * jimevansmusic.blogspot.fi/2012/06/whats-wrong-with-internet-explorer
  793. * .html)
  794. *
  795. * @return true, to use the "persistent hover" feature, false otherwise
  796. */
  797. protected boolean usePersistentHoverForIE() {
  798. return true;
  799. }
  800. /**
  801. * Should the "native events" be enabled for Internet Explorer.
  802. * <p>
  803. * Native events sometimes cause failure in clicking on buttons/checkboxes
  804. * but are possibly needed for some operations.
  805. *
  806. * @return true, to use "native events", false to use generated Javascript
  807. * events
  808. */
  809. protected boolean useNativeEventsForIE() {
  810. return true;
  811. }
  812. // FIXME: Remove this once TB4 getRemoteControlName works properly
  813. private RemoteWebDriver getRemoteDriver() {
  814. WebDriver d = getDriver();
  815. if (d instanceof TestBenchDriverProxy) {
  816. try {
  817. Field f = TestBenchDriverProxy.class
  818. .getDeclaredField("actualDriver");
  819. f.setAccessible(true);
  820. return (RemoteWebDriver) f.get(d);
  821. } catch (Exception e) {
  822. e.printStackTrace();
  823. }
  824. }
  825. if (d instanceof RemoteWebDriver) {
  826. return (RemoteWebDriver) d;
  827. }
  828. return null;
  829. }
  830. // FIXME: Remove this once TB4 getRemoteControlName works properly
  831. protected String getRemoteControlName() {
  832. try {
  833. RemoteWebDriver d = getRemoteDriver();
  834. if (d == null) {
  835. return null;
  836. }
  837. HttpCommandExecutor ce = (HttpCommandExecutor) d
  838. .getCommandExecutor();
  839. String hostName = ce.getAddressOfRemoteServer().getHost();
  840. int port = ce.getAddressOfRemoteServer().getPort();
  841. HttpHost host = new HttpHost(hostName, port);
  842. DefaultHttpClient client = new DefaultHttpClient();
  843. URL sessionURL = new URL("http://" + hostName + ":" + port
  844. + "/grid/api/testsession?session=" + d.getSessionId());
  845. BasicHttpEntityEnclosingRequest r = new BasicHttpEntityEnclosingRequest(
  846. "POST", sessionURL.toExternalForm());
  847. HttpResponse response = client.execute(host, r);
  848. JsonObject object = extractObject(response);
  849. URL myURL = new URL(object.getString("proxyId"));
  850. if ((myURL.getHost() != null) && (myURL.getPort() != -1)) {
  851. return myURL.getHost();
  852. }
  853. } catch (Exception e) {
  854. e.printStackTrace();
  855. }
  856. return null;
  857. }
  858. protected boolean logContainsText(String string) {
  859. List<String> logs = getLogs();
  860. for (String text : logs) {
  861. if (text.contains(string)) {
  862. return true;
  863. }
  864. }
  865. return false;
  866. }
  867. protected List<String> getLogs() {
  868. VerticalLayoutElement log = $(VerticalLayoutElement.class).id("Log");
  869. List<LabelElement> logLabels = log.$(LabelElement.class).all();
  870. List<String> logTexts = new ArrayList<>();
  871. for (LabelElement label : logLabels) {
  872. logTexts.add(label.getText());
  873. }
  874. return logTexts;
  875. }
  876. private static JsonObject extractObject(HttpResponse resp)
  877. throws IOException {
  878. InputStream contents = resp.getEntity().getContent();
  879. StringWriter writer = new StringWriter();
  880. IOUtils.copy(contents, writer, "UTF8");
  881. return JsonUtil.parse(writer.toString());
  882. }
  883. protected void click(CheckBoxElement checkbox) {
  884. WebElement cb = checkbox.findElement(By.xpath("input"));
  885. if (BrowserUtil.isChrome(getDesiredCapabilities())) {
  886. testBenchElement(cb).click(0, 0);
  887. } else if (BrowserUtil.isFirefox(getDesiredCapabilities())) {
  888. // Firefox workaround
  889. getCommandExecutor().executeScript("arguments[0].click()", cb);
  890. } else {
  891. cb.click();
  892. }
  893. }
  894. protected void clickElement(WebElement element) {
  895. if (BrowserUtil.isFirefox(getDesiredCapabilities())) {
  896. // Workaround for Selenium/TB and Firefox 45 issue
  897. ((TestBenchElement) (element)).clickHiddenElement();
  898. } else {
  899. element.click();
  900. }
  901. }
  902. protected void contextClickElement(WebElement element) {
  903. if (BrowserUtil.isFirefox(getDesiredCapabilities())) {
  904. // Workaround for Selenium/TB and Firefox 45 issue
  905. getCommandExecutor().executeScript(
  906. "var ev = document.createEvent('HTMLEvents'); ev.initEvent('contextmenu', true, false); arguments[0].dispatchEvent(ev);",
  907. element);
  908. } else {
  909. new Actions(getDriver()).contextClick(element).perform();
  910. }
  911. }
  912. protected boolean isLoadingIndicatorVisible() {
  913. WebElement loadingIndicator = findElement(
  914. By.className("v-loading-indicator"));
  915. return loadingIndicator.isDisplayed();
  916. }
  917. protected void waitUntilLoadingIndicatorVisible() {
  918. waitUntil(input -> isLoadingIndicatorVisible());
  919. }
  920. protected void waitUntilLoadingIndicatorNotVisible() {
  921. waitUntil(input -> !isLoadingIndicatorVisible());
  922. }
  923. /**
  924. * Selects a menu item. By default, this will click on the menu item.
  925. *
  926. * @param menuCaption
  927. * caption of the menu item
  928. */
  929. protected void selectMenu(String menuCaption) {
  930. selectMenu(menuCaption, true);
  931. }
  932. /**
  933. * Selects a menu item.
  934. *
  935. * @param menuCaption
  936. * caption of the menu item
  937. * @param click
  938. * <code>true</code> if should click the menu item;
  939. * <code>false</code> if not
  940. */
  941. protected void selectMenu(String menuCaption, boolean click) {
  942. WebElement menuElement = getMenuElement(menuCaption);
  943. Dimension size = menuElement.getSize();
  944. new Actions(getDriver())
  945. .moveToElement(menuElement, size.width - 10, size.height / 2)
  946. .perform();
  947. if (click) {
  948. new Actions(getDriver()).click().perform();
  949. }
  950. }
  951. /**
  952. * Finds the menu item from the DOM based on menu item caption.
  953. *
  954. * @param menuCaption
  955. * caption of the menu item
  956. * @return the found menu item
  957. * @throws NoSuchElementException
  958. * if menu item is not found
  959. */
  960. protected WebElement getMenuElement(String menuCaption)
  961. throws NoSuchElementException {
  962. return getDriver().findElement(
  963. By.xpath("//span[text() = '" + menuCaption + "']"));
  964. }
  965. /**
  966. * Selects a submenu described by a path of menus from the first MenuBar in
  967. * the UI.
  968. *
  969. * @param menuCaptions
  970. * array of menu captions
  971. */
  972. protected void selectMenuPath(String... menuCaptions) {
  973. selectMenu(menuCaptions[0], true);
  974. // Move to the menu item opened below the menu bar.
  975. new Actions(getDriver())
  976. .moveByOffset(0,
  977. getMenuElement(menuCaptions[0]).getSize().getHeight())
  978. .perform();
  979. for (int i = 1; i < menuCaptions.length - 1; i++) {
  980. selectMenu(menuCaptions[i]);
  981. new Actions(getDriver()).moveByOffset(40, 0).build().perform();
  982. }
  983. selectMenu(menuCaptions[menuCaptions.length - 1], true);
  984. }
  985. /**
  986. * Asserts that an element is present
  987. *
  988. * @param by
  989. * the locatore for the element
  990. */
  991. protected void assertElementPresent(By by) {
  992. Assert.assertTrue("Element is not present", isElementPresent(by));
  993. }
  994. /**
  995. * Asserts that an element is not present
  996. *
  997. * @param by
  998. * the locatore for the element
  999. */
  1000. protected void assertElementNotPresent(By by) {
  1001. Assert.assertFalse("Element is present", isElementPresent(by));
  1002. }
  1003. /**
  1004. * Asserts that no error notifications are shown. Requires the use of
  1005. * "?debug" as exceptions are otherwise not shown as notifications.
  1006. */
  1007. protected void assertNoErrorNotifications() {
  1008. Assert.assertFalse(
  1009. "Error notification with client side exception is shown",
  1010. isNotificationPresent("error"));
  1011. }
  1012. /**
  1013. * Asserts that no system notifications are shown.
  1014. */
  1015. protected void assertNoSystemNotifications() {
  1016. Assert.assertFalse(
  1017. "Error notification with system error exception is shown",
  1018. isNotificationPresent("system"));
  1019. }
  1020. /**
  1021. * Asserts that a system notification is shown.
  1022. */
  1023. protected void assertSystemNotification() {
  1024. Assert.assertTrue(
  1025. "Error notification with system error exception is not shown",
  1026. isNotificationPresent("system"));
  1027. }
  1028. private boolean isNotificationPresent(String type) {
  1029. if ("error".equals(type)) {
  1030. Assert.assertTrue(
  1031. "Debug window must be open to be able to see error notifications",
  1032. isDebugWindowOpen());
  1033. }
  1034. return isElementPresent(By.className("v-Notification-" + type));
  1035. }
  1036. private boolean isDebugWindowOpen() {
  1037. return isElementPresent(By.className("v-debugwindow"));
  1038. }
  1039. protected void assertNoHorizontalScrollbar(WebElement element,
  1040. String errorMessage) {
  1041. // IE rounds clientWidth/clientHeight down and scrollHeight/scrollWidth
  1042. // up, so using clientWidth/clientHeight will fail if the element height
  1043. // is not an integer
  1044. int clientWidth = getClientWidth(element);
  1045. int scrollWidth = getScrollWidth(element);
  1046. boolean hasScrollbar = scrollWidth > clientWidth;
  1047. Assert.assertFalse(
  1048. "The element should not have a horizontal scrollbar (scrollWidth: "
  1049. + scrollWidth + ", clientWidth: " + clientWidth + "): "
  1050. + errorMessage,
  1051. hasScrollbar);
  1052. }
  1053. protected void assertNoVerticalScrollbar(WebElement element,
  1054. String errorMessage) {
  1055. // IE rounds clientWidth/clientHeight down and scrollHeight/scrollWidth
  1056. // up, so using clientWidth/clientHeight will fail if the element height
  1057. // is not an integer
  1058. int clientHeight = getClientHeight(element);
  1059. int scrollHeight = getScrollHeight(element);
  1060. boolean hasScrollbar = scrollHeight > clientHeight;
  1061. Assert.assertFalse(
  1062. "The element should not have a vertical scrollbar (scrollHeight: "
  1063. + scrollHeight + ", clientHeight: " + clientHeight
  1064. + "): " + errorMessage,
  1065. hasScrollbar);
  1066. }
  1067. protected int getScrollHeight(WebElement element) {
  1068. return ((Number) executeScript("return arguments[0].scrollHeight;",
  1069. element)).intValue();
  1070. }
  1071. protected int getScrollWidth(WebElement element) {
  1072. return ((Number) executeScript("return arguments[0].scrollWidth;",
  1073. element)).intValue();
  1074. }
  1075. /**
  1076. * Returns client height rounded up instead of as double because of IE9
  1077. * issues: https://dev.vaadin.com/ticket/18469
  1078. */
  1079. protected int getClientHeight(WebElement e) {
  1080. String script = "var cs = window.getComputedStyle(arguments[0]);"
  1081. + "return Math.ceil(parseFloat(cs.height)+parseFloat(cs.paddingTop)+parseFloat(cs.paddingBottom));";
  1082. return ((Number) executeScript(script, e)).intValue();
  1083. }
  1084. /**
  1085. * Returns client width rounded up instead of as double because of IE9
  1086. * issues: https://dev.vaadin.com/ticket/18469
  1087. */
  1088. protected int getClientWidth(WebElement e) {
  1089. String script = "var cs = window.getComputedStyle(arguments[0]);"
  1090. + "var h = parseFloat(cs.width)+parseFloat(cs.paddingLeft)+parseFloat(cs.paddingRight);"
  1091. + "return Math.ceil(h);";
  1092. return ((Number) executeScript(script, e)).intValue();
  1093. }
  1094. protected void assertElementsEquals(WebElement expectedElement,
  1095. WebElement actualElement) {
  1096. while (expectedElement instanceof WrapsElement) {
  1097. expectedElement = ((WrapsElement) expectedElement)
  1098. .getWrappedElement();
  1099. }
  1100. while (actualElement instanceof WrapsElement) {
  1101. actualElement = ((WrapsElement) actualElement).getWrappedElement();
  1102. }
  1103. Assert.assertEquals(expectedElement, actualElement);
  1104. }
  1105. protected WebElement getActiveElement() {
  1106. return (WebElement) executeScript("return document.activeElement;");
  1107. }
  1108. protected void waitForThemeToChange(final String theme) {
  1109. final WebElement rootDiv = findElement(
  1110. By.xpath("//div[contains(@class,'v-app')]"));
  1111. waitUntil(new ExpectedCondition<Boolean>() {
  1112. @Override
  1113. public Boolean apply(WebDriver input) {
  1114. String rootClass = rootDiv.getAttribute("class").trim();
  1115. return rootClass.contains(theme);
  1116. }
  1117. }, 30);
  1118. }
  1119. }