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 34KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042
  1. /*
  2. * Copyright 2000-2014 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.lang.annotation.ElementType;
  18. import java.lang.annotation.Retention;
  19. import java.lang.annotation.RetentionPolicy;
  20. import java.lang.annotation.Target;
  21. import java.net.URL;
  22. import java.util.Collections;
  23. import java.util.List;
  24. import com.vaadin.testbench.TestBenchElement;
  25. import org.junit.After;
  26. import org.junit.Before;
  27. import org.junit.runner.RunWith;
  28. import org.openqa.selenium.*;
  29. import org.openqa.selenium.interactions.HasInputDevices;
  30. import org.openqa.selenium.interactions.Keyboard;
  31. import org.openqa.selenium.interactions.Mouse;
  32. import org.openqa.selenium.interactions.internal.Coordinates;
  33. import org.openqa.selenium.internal.Locatable;
  34. import org.openqa.selenium.remote.BrowserType;
  35. import org.openqa.selenium.remote.DesiredCapabilities;
  36. import org.openqa.selenium.remote.RemoteWebDriver;
  37. import org.openqa.selenium.support.ui.ExpectedCondition;
  38. import org.openqa.selenium.support.ui.ExpectedConditions;
  39. import org.openqa.selenium.support.ui.WebDriverWait;
  40. import com.thoughtworks.selenium.webdriven.WebDriverBackedSelenium;
  41. import com.vaadin.server.LegacyApplication;
  42. import com.vaadin.server.UIProvider;
  43. import com.vaadin.testbench.TestBench;
  44. import com.vaadin.testbench.TestBenchTestCase;
  45. import com.vaadin.tests.components.AbstractTestUIWithLog;
  46. import com.vaadin.tests.tb3.MultiBrowserTest.Browser;
  47. import com.vaadin.ui.UI;
  48. import static com.vaadin.tests.tb3.TB3Runner.localWebDriverIsUsed;
  49. /**
  50. * Base class for TestBench 3+ tests. All TB3+ tests in the project should
  51. * extend this class.
  52. *
  53. * Provides:
  54. * <ul>
  55. * <li>Helpers for browser selection</li>
  56. * <li>Hub connection setup and teardown</li>
  57. * <li>Automatic generation of URL for a given test on the development server
  58. * using {@link #getUIClass()} or by automatically finding an enclosing UI class
  59. * and based on requested features, e.g. {@link #isDebug()}, {@link #isPush()}</li>
  60. * <li>Generic helpers for creating TB3+ tests</li>
  61. * </ul>
  62. *
  63. * @author Vaadin Ltd
  64. */
  65. @RunWith(value = TB3Runner.class)
  66. public abstract class AbstractTB3Test extends TestBenchTestCase {
  67. /**
  68. * Height of the screenshots we want to capture
  69. */
  70. private static final int SCREENSHOT_HEIGHT = 850;
  71. /**
  72. * Width of the screenshots we want to capture
  73. */
  74. private static final int SCREENSHOT_WIDTH = 1500;
  75. /**
  76. * Timeout used by the TB grid
  77. */
  78. private static final int BROWSER_TIMEOUT_IN_MS = 30 * 1000;
  79. private DesiredCapabilities desiredCapabilities;
  80. private boolean debug = false;
  81. private boolean push = false;
  82. {
  83. // Default browser to run on unless setDesiredCapabilities is called
  84. desiredCapabilities = Browser.FIREFOX.getDesiredCapabilities();
  85. }
  86. /**
  87. * Connect to the hub using a remote web driver, set the canvas size and
  88. * opens the initial URL as specified by {@link #getTestUrl()}
  89. *
  90. * @throws Exception
  91. */
  92. @Before
  93. public void setup() throws Exception {
  94. setupDriver();
  95. }
  96. /**
  97. * Creates and configure the web driver to be used for the test. By default
  98. * creates a remote web driver which connects to {@link #getHubURL()} and
  99. * selects a browser based on {@link #getDesiredCapabilities()}.
  100. *
  101. * This method MUST call {@link #setDriver(WebDriver)} with the newly
  102. * generated driver.
  103. *
  104. * @throws Exception
  105. * If something goes wrong
  106. */
  107. protected void setupDriver() throws Exception {
  108. DesiredCapabilities capabilities;
  109. Browser runLocallyBrowser = getRunLocallyBrowser();
  110. if (runLocallyBrowser != null) {
  111. if (System.getenv().containsKey("TEAMCITY_VERSION")) {
  112. throw new RuntimeException(
  113. "@RunLocally is not supported for tests run on the build server");
  114. }
  115. capabilities = runLocallyBrowser.getDesiredCapabilities();
  116. setupLocalDriver(capabilities);
  117. } else {
  118. capabilities = getDesiredCapabilities();
  119. if (localWebDriverIsUsed()) {
  120. setupLocalDriver(capabilities);
  121. } else {
  122. WebDriver dr = TestBench.createDriver(new RemoteWebDriver(
  123. new URL(getHubURL()), capabilities));
  124. setDriver(dr);
  125. }
  126. }
  127. int w = SCREENSHOT_WIDTH;
  128. int h = SCREENSHOT_HEIGHT;
  129. if (BrowserUtil.isIE8(capabilities)) {
  130. // IE8 gets size wrong, who would have guessed...
  131. w += 4;
  132. h += 4;
  133. }
  134. try {
  135. testBench().resizeViewPortTo(w, h);
  136. } catch (UnsupportedOperationException e) {
  137. // Opera does not support this...
  138. }
  139. }
  140. protected Browser getRunLocallyBrowser() {
  141. RunLocally runLocally = getClass().getAnnotation(RunLocally.class);
  142. if (runLocally != null) {
  143. return runLocally.value();
  144. } else {
  145. return null;
  146. }
  147. }
  148. protected WebElement getTooltipElement() {
  149. return getDriver().findElement(
  150. com.vaadin.testbench.By.className("v-tooltip-text"));
  151. }
  152. protected Coordinates getCoordinates(TestBenchElement element) {
  153. return ((Locatable) element.getWrappedElement()).getCoordinates();
  154. }
  155. private boolean hasDebugMessage(String message) {
  156. return getDebugMessage(message) != null;
  157. }
  158. private WebElement getDebugMessage(String message) {
  159. return driver.findElement(By.xpath(String.format(
  160. "//span[@class='v-debugwindow-message' and text()='%s']",
  161. message)));
  162. }
  163. protected void waitForDebugMessage(final String expectedMessage) {
  164. waitForDebugMessage(expectedMessage, 30);
  165. }
  166. protected void waitForDebugMessage(final String expectedMessage, int timeout) {
  167. waitUntil(new ExpectedCondition<Boolean>() {
  168. @Override
  169. public Boolean apply(WebDriver input) {
  170. return hasDebugMessage(expectedMessage);
  171. }
  172. }, timeout);
  173. }
  174. protected void clearDebugMessages() {
  175. driver.findElement(
  176. By.xpath("//button[@class='v-debugwindow-button' and @title='Clear log']"))
  177. .click();
  178. }
  179. @Retention(RetentionPolicy.RUNTIME)
  180. @Target(ElementType.TYPE)
  181. public @interface RunLocally {
  182. public Browser value() default Browser.FIREFOX;
  183. }
  184. /**
  185. * Creates a {@link WebDriver} instance used for running the test locally
  186. * for debug purposes. Used only when {@link #runLocally()} is overridden to
  187. * return true;
  188. */
  189. protected abstract void setupLocalDriver(
  190. DesiredCapabilities desiredCapabilities);
  191. /**
  192. * Opens the given test (defined by {@link #getTestUrl()}, optionally with
  193. * debug window and/or push (depending on {@link #isDebug()} and
  194. * {@link #isPush()}.
  195. */
  196. protected void openTestURL() {
  197. driver.get(getTestUrl());
  198. }
  199. /**
  200. * Returns the full URL to be used for the test
  201. *
  202. * @return the full URL for the test
  203. */
  204. protected String getTestUrl() {
  205. String baseUrl = getBaseURL();
  206. if (baseUrl.endsWith("/")) {
  207. baseUrl = baseUrl.substring(0, baseUrl.length() - 1);
  208. }
  209. return baseUrl + getDeploymentPath();
  210. }
  211. /**
  212. *
  213. * @return the location (URL) of the TB hub
  214. */
  215. protected String getHubURL() {
  216. return "http://" + getHubHostname() + ":4444/wd/hub";
  217. }
  218. /**
  219. * Used for building the hub URL to use for the test
  220. *
  221. * @return the host name of the TestBench hub
  222. */
  223. protected abstract String getHubHostname();
  224. /**
  225. * Used to determine what URL to initially open for the test
  226. *
  227. * @return the host name of development server
  228. */
  229. protected abstract String getDeploymentHostname();
  230. /**
  231. * Used to determine what port the test is running on
  232. *
  233. * @return The port teh test is running on, by default 8888
  234. */
  235. protected abstract int getDeploymentPort();
  236. /**
  237. * Produces a collection of browsers to run the test on. This method is
  238. * executed by the test runner when determining how many test methods to
  239. * invoke and with what parameters. For each returned value a test method is
  240. * ran and before running that,
  241. * {@link #setDesiredCapabilities(DesiredCapabilities)} is invoked with the
  242. * value returned by this method.
  243. *
  244. * This method is not static to allow overriding it in sub classes. By
  245. * default runs the test only on Firefox
  246. *
  247. * @return The browsers to run the test on
  248. */
  249. public List<DesiredCapabilities> getBrowsersToTest() {
  250. return Collections.singletonList(Browser.FIREFOX
  251. .getDesiredCapabilities());
  252. }
  253. /**
  254. * Used to determine which capabilities should be used when setting up a
  255. * {@link WebDriver} for this test. Typically set by a test runner or left
  256. * at its default (Firefox 24). If you want to run a test on a single
  257. * browser other than Firefox 24 you can override this method.
  258. *
  259. * @return the requested browser capabilities
  260. */
  261. protected DesiredCapabilities getDesiredCapabilities() {
  262. return desiredCapabilities;
  263. }
  264. /**
  265. * Sets the requested browser capabilities (typically browser name and
  266. * version)
  267. *
  268. * @param desiredCapabilities
  269. */
  270. public void setDesiredCapabilities(DesiredCapabilities desiredCapabilities) {
  271. this.desiredCapabilities = desiredCapabilities;
  272. }
  273. /**
  274. * Shuts down the driver after the test has been completed
  275. *
  276. * @throws Exception
  277. */
  278. @After
  279. public void tearDown() throws Exception {
  280. if (driver != null) {
  281. driver.quit();
  282. }
  283. driver = null;
  284. }
  285. /**
  286. * Finds an element based on the part of a TB2 style locator following the
  287. * :: (e.g. vaadin=runLabelModes::PID_Scheckboxaction-Enabled/domChild[0] ->
  288. * PID_Scheckboxaction-Enabled/domChild[0]).
  289. *
  290. * @param vaadinLocator
  291. * The part following :: of the vaadin locator string
  292. * @return
  293. */
  294. protected WebElement vaadinElement(String vaadinLocator) {
  295. return driver.findElement(vaadinLocator(vaadinLocator));
  296. }
  297. /**
  298. * Uses JavaScript to determine the currently focused element.
  299. *
  300. * @return Focused element or null
  301. */
  302. protected WebElement getFocusedElement() {
  303. Object focusedElement = ((JavascriptExecutor) getDriver())
  304. .executeScript("return document.activeElement");
  305. if (null != focusedElement) {
  306. return (WebElement) focusedElement;
  307. } else {
  308. return null;
  309. }
  310. }
  311. /**
  312. * Find a Vaadin element based on its id given using Component.setId
  313. *
  314. * @param id
  315. * The id to locate
  316. * @return
  317. */
  318. public WebElement vaadinElementById(String id) {
  319. return driver.findElement(vaadinLocatorById(id));
  320. }
  321. /**
  322. * Finds a {@link By} locator based on the part of a TB2 style locator
  323. * following the :: (e.g.
  324. * vaadin=runLabelModes::PID_Scheckboxaction-Enabled/domChild[0] ->
  325. * PID_Scheckboxaction-Enabled/domChild[0]).
  326. *
  327. * @param vaadinLocator
  328. * The part following :: of the vaadin locator string
  329. * @return
  330. */
  331. public org.openqa.selenium.By vaadinLocator(String vaadinLocator) {
  332. String base = getApplicationId(getDeploymentPath());
  333. base += "::";
  334. return com.vaadin.testbench.By.vaadin(base + vaadinLocator);
  335. }
  336. /**
  337. * Constructs a {@link By} locator for the id given using Component.setId
  338. *
  339. * @param id
  340. * The id to locate
  341. * @return a locator for the given id
  342. */
  343. public By vaadinLocatorById(String id) {
  344. return vaadinLocator("PID_S" + id);
  345. }
  346. /**
  347. * Waits up to 10s for the given condition to become true. Use e.g. as
  348. * {@link #waitUntil(ExpectedConditions.textToBePresentInElement(by, text))}
  349. *
  350. * @param condition
  351. * the condition to wait for to become true
  352. */
  353. protected void waitUntil(ExpectedCondition<Boolean> condition) {
  354. waitUntil(condition, 10);
  355. }
  356. /**
  357. * Waits the given number of seconds for the given condition to become true.
  358. * Use e.g. as {@link
  359. * #waitUntil(ExpectedConditions.textToBePresentInElement(by, text))}
  360. *
  361. * @param condition
  362. * the condition to wait for to become true
  363. */
  364. protected void waitUntil(ExpectedCondition<Boolean> condition,
  365. long timeoutInSeconds) {
  366. new WebDriverWait(driver, timeoutInSeconds).until(condition);
  367. }
  368. /**
  369. * Waits up to 10s for the given condition to become false. Use e.g. as
  370. * {@link #waitUntilNot(ExpectedConditions.textToBePresentInElement(by,
  371. * text))}
  372. *
  373. * @param condition
  374. * the condition to wait for to become false
  375. */
  376. protected void waitUntilNot(ExpectedCondition<Boolean> condition) {
  377. waitUntilNot(condition, 10);
  378. }
  379. /**
  380. * Waits the given number of seconds for the given condition to become
  381. * false. Use e.g. as {@link
  382. * #waitUntilNot(ExpectedConditions.textToBePresentInElement(by, text))}
  383. *
  384. * @param condition
  385. * the condition to wait for to become false
  386. */
  387. protected void waitUntilNot(ExpectedCondition<Boolean> condition,
  388. long timeoutInSeconds) {
  389. waitUntil(ExpectedConditions.not(condition), timeoutInSeconds);
  390. }
  391. protected void waitForElementToBePresent(By by) {
  392. waitUntil(ExpectedConditions.not(ExpectedConditions
  393. .invisibilityOfElementLocated(by)));
  394. }
  395. /**
  396. * For tests extending {@link AbstractTestUIWithLog}, returns the element
  397. * for the Nth log row
  398. *
  399. * @param rowNr
  400. * The log row to retrieve
  401. * @return the Nth log row
  402. */
  403. protected WebElement getLogRowElement(int rowNr) {
  404. return vaadinElementById("Log_row_" + rowNr);
  405. }
  406. /**
  407. * For tests extending {@link AbstractTestUIWithLog}, returns the text in
  408. * the Nth log row
  409. *
  410. * @param rowNr
  411. * The log row to retrieve text for
  412. * @return the text in the log row
  413. */
  414. protected String getLogRow(int rowNr) {
  415. return getLogRowElement(rowNr).getText();
  416. }
  417. /**
  418. * Asserts that {@literal a} is &gt;= {@literal b}
  419. *
  420. * @param message
  421. * The message to include in the {@link AssertionError}
  422. * @param a
  423. * @param b
  424. * @throws AssertionError
  425. * If comparison fails
  426. */
  427. public static final <T> void assertGreaterOrEqual(String message,
  428. Comparable<T> a, T b) throws AssertionError {
  429. if (a.compareTo(b) >= 0) {
  430. return;
  431. }
  432. throw new AssertionError(decorate(message, a, b));
  433. }
  434. /**
  435. * Asserts that {@literal a} is &gt; {@literal b}
  436. *
  437. * @param message
  438. * The message to include in the {@link AssertionError}
  439. * @param a
  440. * @param b
  441. * @throws AssertionError
  442. * If comparison fails
  443. */
  444. public static final <T> void assertGreater(String message, Comparable<T> a,
  445. T b) throws AssertionError {
  446. if (a.compareTo(b) > 0) {
  447. return;
  448. }
  449. throw new AssertionError(decorate(message, a, b));
  450. }
  451. /**
  452. * Asserts that {@literal a} is &lt;= {@literal b}
  453. *
  454. * @param message
  455. * The message to include in the {@link AssertionError}
  456. * @param a
  457. * @param b
  458. * @throws AssertionError
  459. * If comparison fails
  460. */
  461. public static final <T> void assertLessThanOrEqual(String message,
  462. Comparable<T> a, T b) throws AssertionError {
  463. if (a.compareTo(b) <= 0) {
  464. return;
  465. }
  466. throw new AssertionError(decorate(message, a, b));
  467. }
  468. /**
  469. * Asserts that {@literal a} is &lt; {@literal b}
  470. *
  471. * @param message
  472. * The message to include in the {@link AssertionError}
  473. * @param a
  474. * @param b
  475. * @throws AssertionError
  476. * If comparison fails
  477. */
  478. public static final <T> void assertLessThan(String message,
  479. Comparable<T> a, T b) throws AssertionError {
  480. if (a.compareTo(b) < 0) {
  481. return;
  482. }
  483. throw new AssertionError(decorate(message, a, b));
  484. }
  485. private static <T> String decorate(String message, Comparable<T> a, T b) {
  486. message = message.replace("{0}", a.toString());
  487. message = message.replace("{1}", b.toString());
  488. return message;
  489. }
  490. /**
  491. * Returns the path that should be used for the test. The path contains the
  492. * full path (appended to hostname+port) and must start with a slash.
  493. *
  494. * @param push
  495. * true if "?debug" should be added
  496. * @param debug
  497. * true if /run-push should be used instead of /run
  498. *
  499. * @return The URL path to the UI class to test
  500. */
  501. protected String getDeploymentPath() {
  502. Class<?> uiClass = getUIClass();
  503. if (uiClass != null) {
  504. return getDeploymentPath(uiClass);
  505. }
  506. throw new IllegalArgumentException("Unable to determine path for "
  507. + getClass().getCanonicalName());
  508. }
  509. /**
  510. * Returns the UI class the current test is connected to (or in special
  511. * cases UIProvider or LegacyApplication). Uses the enclosing class if the
  512. * test class is a static inner class to a UI class.
  513. *
  514. * Test which are not enclosed by a UI class must implement this method and
  515. * return the UI class they want to test.
  516. *
  517. * Note that this method will update the test name to the enclosing class to
  518. * be compatible with TB2 screenshot naming
  519. *
  520. * @return the UI class the current test is connected to
  521. */
  522. protected Class<?> getUIClass() {
  523. try {
  524. // Convention: SomeUITest uses the SomeUI UI class
  525. String uiClassName = getClass().getName().replaceFirst("Test$", "");
  526. Class<?> cls = Class.forName(uiClassName);
  527. if (isSupportedRunnerClass(cls)) {
  528. return cls;
  529. }
  530. } catch (Exception e) {
  531. }
  532. throw new RuntimeException(
  533. "Could not determine UI class. Ensure the test is named UIClassTest and is in the same package as the UIClass");
  534. }
  535. /**
  536. * @return true if the given class is supported by ApplicationServletRunner
  537. */
  538. @SuppressWarnings("deprecation")
  539. private boolean isSupportedRunnerClass(Class<?> cls) {
  540. if (UI.class.isAssignableFrom(cls)) {
  541. return true;
  542. }
  543. if (UIProvider.class.isAssignableFrom(cls)) {
  544. return true;
  545. }
  546. if (LegacyApplication.class.isAssignableFrom(cls)) {
  547. return true;
  548. }
  549. return false;
  550. }
  551. /**
  552. * Returns whether to run the test in debug mode (with the debug console
  553. * open) or not
  554. *
  555. * @return true to run with the debug window open, false by default
  556. */
  557. protected final boolean isDebug() {
  558. return debug;
  559. }
  560. /**
  561. * Sets whether to run the test in debug mode (with the debug console open)
  562. * or not.
  563. *
  564. * @param debug
  565. * true to open debug window, false otherwise
  566. */
  567. protected final void setDebug(boolean debug) {
  568. this.debug = debug;
  569. }
  570. /**
  571. * Returns whether to run the test with push enabled (using /run-push) or
  572. * not. Note that push tests can and should typically be created using @Push
  573. * on the UI instead of overriding this method
  574. *
  575. * @return true if /run-push is used, false otherwise
  576. */
  577. protected final boolean isPush() {
  578. return push;
  579. }
  580. /**
  581. * Sets whether to run the test with push enabled (using /run-push) or not.
  582. * Note that push tests can and should typically be created using @Push on
  583. * the UI instead of overriding this method
  584. *
  585. * @param push
  586. * true to use /run-push in the test, false otherwise
  587. */
  588. protected final void setPush(boolean push) {
  589. this.push = push;
  590. }
  591. /**
  592. * Returns the path for the given UI class when deployed on the test server.
  593. * The path contains the full path (appended to hostname+port) and must
  594. * start with a slash.
  595. *
  596. * This method takes into account {@link #isPush()} and {@link #isDebug()}
  597. * when the path is generated.
  598. *
  599. * @param uiClass
  600. * @param push
  601. * true if "?debug" should be added
  602. * @param debug
  603. * true if /run-push should be used instead of /run
  604. * @return The path to the given UI class
  605. */
  606. private String getDeploymentPath(Class<?> uiClass) {
  607. String runPath = "/run";
  608. if (isPush()) {
  609. runPath = "/run-push";
  610. }
  611. if (UI.class.isAssignableFrom(uiClass)) {
  612. return runPath + "/" + uiClass.getCanonicalName()
  613. + (isDebug() ? "?debug" : "");
  614. } else if (LegacyApplication.class.isAssignableFrom(uiClass)) {
  615. return runPath + "/" + uiClass.getCanonicalName()
  616. + "?restartApplication" + (isDebug() ? "&debug" : "");
  617. } else {
  618. throw new IllegalArgumentException(
  619. "Unable to determine path for enclosing class "
  620. + uiClass.getCanonicalName());
  621. }
  622. }
  623. /**
  624. * Used to determine what URL to initially open for the test
  625. *
  626. * @return The base URL for the test. Does not include a trailing slash.
  627. */
  628. protected String getBaseURL() {
  629. return "http://" + getDeploymentHostname() + ":" + getDeploymentPort();
  630. }
  631. /**
  632. * Generates the application id based on the URL in a way compatible with
  633. * VaadinServletService.
  634. *
  635. * @param pathWithQueryParameters
  636. * The path part of the URL, possibly still containing query
  637. * parameters
  638. * @return The application ID string used in Vaadin locators
  639. */
  640. private String getApplicationId(String pathWithQueryParameters) {
  641. // Remove any possible URL parameters
  642. String pathWithoutQueryParameters = pathWithQueryParameters.replaceAll(
  643. "\\?.*", "");
  644. if ("".equals(pathWithoutQueryParameters)) {
  645. return "ROOT";
  646. }
  647. // Retain only a-z and numbers
  648. return pathWithoutQueryParameters.replaceAll("[^a-zA-Z0-9]", "");
  649. }
  650. /**
  651. * Sleeps for the given number of ms but ensures that the browser connection
  652. * does not time out.
  653. *
  654. * @param timeoutMillis
  655. * Number of ms to wait
  656. * @throws InterruptedException
  657. */
  658. protected void sleep(int timeoutMillis) throws InterruptedException {
  659. while (timeoutMillis > 0) {
  660. int d = Math.min(BROWSER_TIMEOUT_IN_MS, timeoutMillis);
  661. Thread.sleep(d);
  662. timeoutMillis -= d;
  663. // Do something to keep the connection alive
  664. getDriver().getTitle();
  665. }
  666. }
  667. /**
  668. * Provides helper method for selecting the browser to run on
  669. *
  670. * @author Vaadin Ltd
  671. */
  672. public static class BrowserUtil {
  673. /**
  674. * Gets the capabilities for Safari of the given version
  675. *
  676. * @param version
  677. * the major version
  678. * @return an object describing the capabilities required for running a
  679. * test on the given Safari version
  680. */
  681. public static DesiredCapabilities safari(int version) {
  682. DesiredCapabilities c = DesiredCapabilities.safari();
  683. c.setPlatform(Platform.MAC);
  684. c.setVersion("" + version);
  685. return c;
  686. }
  687. /**
  688. * Gets the capabilities for Chrome of the given version
  689. *
  690. * @param version
  691. * the major version
  692. * @return an object describing the capabilities required for running a
  693. * test on the given Chrome version
  694. */
  695. public static DesiredCapabilities chrome(int version) {
  696. DesiredCapabilities c = DesiredCapabilities.chrome();
  697. c.setVersion("" + version);
  698. c.setPlatform(Platform.XP);
  699. return c;
  700. }
  701. /**
  702. * Gets the capabilities for Opera of the given version
  703. *
  704. * @param version
  705. * the major version
  706. * @return an object describing the capabilities required for running a
  707. * test on the given Opera version
  708. */
  709. public static DesiredCapabilities opera(int version) {
  710. DesiredCapabilities c = DesiredCapabilities.opera();
  711. c.setVersion("" + version);
  712. c.setPlatform(Platform.XP);
  713. return c;
  714. }
  715. /**
  716. * Gets the capabilities for Firefox of the given version
  717. *
  718. * @param version
  719. * the major version
  720. * @return an object describing the capabilities required for running a
  721. * test on the given Firefox version
  722. */
  723. public static DesiredCapabilities firefox(int version) {
  724. DesiredCapabilities c = DesiredCapabilities.firefox();
  725. c.setVersion("" + version);
  726. c.setPlatform(Platform.XP);
  727. return c;
  728. }
  729. /**
  730. * Gets the capabilities for Internet Explorer of the given version
  731. *
  732. * @param version
  733. * the major version
  734. * @return an object describing the capabilities required for running a
  735. * test on the given Internet Explorer version
  736. */
  737. public static DesiredCapabilities ie(int version) {
  738. DesiredCapabilities c = DesiredCapabilities.internetExplorer();
  739. c.setVersion("" + version);
  740. return c;
  741. }
  742. /**
  743. * Gets the capabilities for PhantomJS of the given version
  744. *
  745. * @param version
  746. * the major version
  747. * @return an object describing the capabilities required for running a
  748. * test on the given PhantomJS version
  749. */
  750. public static DesiredCapabilities phantomJS(int version) {
  751. DesiredCapabilities c = DesiredCapabilities.phantomjs();
  752. c.setPlatform(Platform.LINUX);
  753. c.setVersion("" + version);
  754. return c;
  755. }
  756. /**
  757. * Checks if the given capabilities refer to Internet Explorer 8
  758. *
  759. * @param capabilities
  760. * @return true if the capabilities refer to IE8, false otherwise
  761. */
  762. public static boolean isIE8(DesiredCapabilities capabilities) {
  763. return isIE(capabilities) && "8".equals(capabilities.getVersion());
  764. }
  765. /**
  766. * @param capabilities
  767. * The capabilities to check
  768. * @return true if the capabilities refer to Internet Explorer, false
  769. * otherwise
  770. */
  771. public static boolean isIE(DesiredCapabilities capabilities) {
  772. return BrowserType.IE.equals(capabilities.getBrowserName());
  773. }
  774. /**
  775. * @param capabilities
  776. * The capabilities to check
  777. * @return true if the capabilities refer to Chrome, false otherwise
  778. */
  779. public static boolean isChrome(DesiredCapabilities capabilities) {
  780. return BrowserType.CHROME.equals(capabilities.getBrowserName());
  781. }
  782. /**
  783. * @param capabilities
  784. * The capabilities to check
  785. * @return true if the capabilities refer to Opera, false otherwise
  786. */
  787. public static boolean isOpera(DesiredCapabilities capabilities) {
  788. return BrowserType.OPERA.equals(capabilities.getBrowserName());
  789. }
  790. /**
  791. * @param capabilities
  792. * The capabilities to check
  793. * @return true if the capabilities refer to Safari, false otherwise
  794. */
  795. public static boolean isSafari(DesiredCapabilities capabilities) {
  796. return BrowserType.SAFARI.equals(capabilities.getBrowserName());
  797. }
  798. /**
  799. * @param capabilities
  800. * The capabilities to check
  801. * @return true if the capabilities refer to Firefox, false otherwise
  802. */
  803. public static boolean isFirefox(DesiredCapabilities capabilities) {
  804. return BrowserType.FIREFOX.equals(capabilities.getBrowserName());
  805. }
  806. /**
  807. * @param capabilities
  808. * The capabilities to check
  809. * @return true if the capabilities refer to PhantomJS, false otherwise
  810. */
  811. public static boolean isPhantomJS(DesiredCapabilities capabilities) {
  812. return BrowserType.PHANTOMJS.equals(capabilities.getBrowserName());
  813. }
  814. /**
  815. * Returns a human readable identifier of the given browser. Used for
  816. * test naming and screenshots
  817. *
  818. * @param capabilities
  819. * @return a human readable string describing the capabilities
  820. */
  821. public static String getBrowserIdentifier(
  822. DesiredCapabilities capabilities) {
  823. if (isIE(capabilities)) {
  824. return "InternetExplorer";
  825. } else if (isFirefox(capabilities)) {
  826. return "Firefox";
  827. } else if (isChrome(capabilities)) {
  828. return "Chrome";
  829. } else if (isSafari(capabilities)) {
  830. return "Safari";
  831. } else if (isOpera(capabilities)) {
  832. return "Opera";
  833. } else if (isPhantomJS(capabilities)) {
  834. return "PhantomJS";
  835. }
  836. return capabilities.getBrowserName();
  837. }
  838. /**
  839. * Returns a human readable identifier of the platform described by the
  840. * given capabilities. Used mainly for screenshots
  841. *
  842. * @param capabilities
  843. * @return a human readable string describing the platform
  844. */
  845. public static String getPlatform(DesiredCapabilities capabilities) {
  846. if (capabilities.getPlatform() == Platform.WIN8
  847. || capabilities.getPlatform() == Platform.WINDOWS
  848. || capabilities.getPlatform() == Platform.VISTA
  849. || capabilities.getPlatform() == Platform.XP) {
  850. return "Windows";
  851. } else if (capabilities.getPlatform() == Platform.MAC) {
  852. return "Mac";
  853. }
  854. return capabilities.getPlatform().toString();
  855. }
  856. /**
  857. * Returns a string which uniquely (enough) identifies this browser.
  858. * Used mainly in screenshot names.
  859. *
  860. * @param capabilities
  861. *
  862. * @return a unique string for each browser
  863. */
  864. public static String getUniqueIdentifier(
  865. DesiredCapabilities capabilities) {
  866. return getUniqueIdentifier(getPlatform(capabilities),
  867. getBrowserIdentifier(capabilities),
  868. capabilities.getVersion());
  869. }
  870. /**
  871. * Returns a string which uniquely (enough) identifies this browser.
  872. * Used mainly in screenshot names.
  873. *
  874. * @param capabilities
  875. *
  876. * @return a unique string for each browser
  877. */
  878. public static String getUniqueIdentifier(
  879. DesiredCapabilities capabilities, String versionOverride) {
  880. return getUniqueIdentifier(getPlatform(capabilities),
  881. getBrowserIdentifier(capabilities), versionOverride);
  882. }
  883. private static String getUniqueIdentifier(String platform,
  884. String browser, String version) {
  885. return platform + "_" + browser + "_" + version;
  886. }
  887. }
  888. /**
  889. * Called by the test runner whenever there is an exception in the test that
  890. * will cause termination of the test
  891. *
  892. * @param t
  893. * the throwable which caused the termination
  894. */
  895. public void onUncaughtException(Throwable t) {
  896. // Do nothing by default
  897. }
  898. /**
  899. * Returns the mouse object for doing mouse commands
  900. *
  901. * @return Returns the mouse
  902. */
  903. public Mouse getMouse() {
  904. return ((HasInputDevices) getDriver()).getMouse();
  905. }
  906. /**
  907. * Returns the keyboard object for controlling keyboard events
  908. *
  909. * @return Return the keyboard
  910. */
  911. public Keyboard getKeyboard() {
  912. return ((HasInputDevices) getDriver()).getKeyboard();
  913. }
  914. public void hitButton(String id) {
  915. if (BrowserUtil.isPhantomJS(getDesiredCapabilities())) {
  916. driver.findElement(By.id(id)).click();
  917. } else {
  918. WebDriverBackedSelenium selenium = new WebDriverBackedSelenium(
  919. driver, driver.getCurrentUrl());
  920. selenium.keyPress("id=" + id, "\\13");
  921. }
  922. }
  923. protected void openDebugLogTab() {
  924. waitUntil(new ExpectedCondition<Boolean>() {
  925. @Override
  926. public Boolean apply(WebDriver input) {
  927. WebElement element = getDebugLogButton();
  928. return element != null;
  929. }
  930. }, 15);
  931. getDebugLogButton().click();
  932. }
  933. private WebElement getDebugLogButton() {
  934. return findElement(By.xpath("//button[@title='Debug message log']"));
  935. }
  936. }