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

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